Some user-made C functions and their D equivalents

pascal111 judas.the.messiah.111 at gmail.com
Thu Jul 28 20:20:27 UTC 2022


On Thursday, 28 July 2022 at 17:46:49 UTC, frame wrote:
> On Thursday, 28 July 2022 at 16:45:55 UTC, pascal111 wrote:
>
>> Aha! "In theory, someone could inject bad code", you admit my 
>> theory.
>
> The code would need to work and pass merge tests too. The merge 
> reason must match in review. If someone fixes a task and 
> additionally adds 100 LOC some should, will ask what this is 
> about.
>
> It's a extrem unlikely scenario. You may heard of linux kernel 
> source that contains code that no one exactly knows about. But 
> this some kind of bait. It's old code, reviewed years ago, not 
> needed anymore but not knowing to be harmful. Completely 
> different.
>
> Anyway, code old or new may be harmful if it allows UB 
> (undefined behaviour) and that is what hackers primarily use, 
> not secret backdoors. This is why it's important to write 
> CORRECT software that doesn't allow and cannot fall in a state 
> of UB.

I agree with you in some points.

I retyped again some function of C library I made before, but 
with D code:


module dcollect;

import std.stdio;
import std.conv;
import std.ascii;

/****************************************/

string strleft(const string ch, int n)
{

         string ch_sub;

         ch_sub=ch[0..n];

         return ch_sub;

}

/************************************/

string strreverse(const string ch)
{

         string ch_rev;

         for(int i=to!int(ch.length-1); i>=0; i--)
             ch_rev~=ch[i];


         return ch_rev;


}

/*********************************************/

string strright(const string ch, int n)
{

         string ch_sub1,
         ch_sub2;

         ch_sub1=strreverse(ch);

         ch_sub2=strleft(ch_sub1, n);

         ch_sub1=strreverse(ch_sub2);

         return ch_sub1;

}

/*********************************************/

string strmid(const string ch, int x, int l)
{

         string ch_sub;

         ch_sub=ch[x..(x+l)];

         return ch_sub;

}

/*********************************************/

string strtolower(const string ch)
{

         string ch_cpy;

         for(int i=0; i<ch.length; i++)
             ch_cpy~=toLower(ch[i]);

         return ch_cpy;

}

/*********************************************/

string strtoupper(const string ch)
{

         string ch_cpy;

         for(int i=0; i<ch.length; i++)
             ch_cpy~=toUpper(ch[i]);

         return ch_cpy;

}


More information about the Digitalmars-d-learn mailing list