Some user-made C functions and their D equivalents

pascal111 judas.the.messiah.111 at gmail.com
Thu Jul 28 12:05:59 UTC 2022


On Thursday, 28 July 2022 at 11:13:19 UTC, Dennis wrote:
> On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:
>> The library link:
>> https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H
>
> It would help if the functions had a comment explaining what 
> they're supposed to do, but it looks like most of them are 
> string functions. In D, you can concatenate strings with the 
> `~` operator, and utility functions like `strip` and `replace` 
> are in the `std.string` module:
>
> https://dlang.org/phobos/std_string.html
>
> I also think you defined the equivalent of these functions:
> ```D
> import std.algorithm: swap;
> import std.math: sgn, trunc;
> ```

As you mentioned, I retyped some of 'em:

module dcollect;

import std.stdio;
import std.conv;

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

string strleft(const string ch, int n)
{

         string ch_sub;

         for(int i=0; i<n; i++)
             ch_sub~=ch[i];

         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;

}

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

void swap(T)(ref T x,ref T y)
{

         T z;

         z=x;
         x=y;
         y=z;

}

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

int sgn(T)(T x)
{

         if(x<0)
             return -1;
         else if(x>0)
             return 1;
         else
             return 0;

}



More information about the Digitalmars-d-learn mailing list