Re: Some user-made C functions and their D equivalents

2022-07-30 Thread frame via Digitalmars-d-learn

On Saturday, 30 July 2022 at 17:55:02 UTC, pascal111 wrote:


I don't understand much the posting details of this forum.



https://forum.dlang.org/help#about


It's simple: if you want to format/style your posts rather then 
just using plain text, enable the Markdown option. It's similar 
to Github-formatting.


Otherwise don't, because some tokens trigger a format where it 
makes no sense. To avoid that, just post in plain text mode by 
unchecking the option and make use of the preview option too.





Re: Some user-made C functions and their D equivalents

2022-07-30 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 23:08:15 UTC, frame wrote:

On Thursday, 28 July 2022 at 20:20:27 UTC, pascal111 wrote:

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


It's a start but you need to learn.


Thanks!

I made the equivalent of my C library "collect":
https://github.com/pascal111-fra/D/blob/main/dcollect.d



- these functions can run into UB if you compile it without 
bound checking enabled


You are right, but I'm still in the beginning to learn all 
necessary concepts and features I need.




- when working with arrays or ranges it's better to use 
unsigned integers or just use `size_t` which represents 
unsigned integer for 32 or 64 bit. This avoids negative values 
and enables the maxmium value which can be provided on the 
plattform to access the highest element in the array.




I tried to use now "uint".

- It's sure advanced topic but you should start to check your 
input from the beginning. Try what happens if you apply 
negative numbers or invalid offsets ;-)


I don't know which client you are using but please have an eye 
on proper format of your posts - see the "Markdown formatting" 
or disable the Markdown option below your input.


I don't understand much the posting details of this forum.



https://forum.dlang.org/help#about





Re: Some user-made C functions and their D equivalents

2022-07-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 July 2022 at 20:20:27 UTC, pascal111 wrote:

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


It's a start but you need to learn.

- these functions can run into UB if you compile it without bound 
checking enabled


- when working with arrays or ranges it's better to use unsigned 
integers or just use `size_t` which represents unsigned integer 
for 32 or 64 bit. This avoids negative values and enables the 
maxmium value which can be provided on the plattform to access 
the highest element in the array.


- It's sure advanced topic but you should start to check your 
input from the beginning. Try what happens if you apply negative 
numbers or invalid offsets ;-)


I don't know which client you are using but please have an eye on 
proper format of your posts - see the "Markdown formatting" or 
disable the Markdown option below your input.


https://forum.dlang.org/help#about




Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

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

Re: Some user-made C functions and their D equivalents

2022-07-28 Thread frame via Digitalmars-d-learn

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.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 17:21:57 UTC, H. S. Teoh wrote:
On Thu, Jul 28, 2022 at 04:45:55PM +, pascal111 via 
Digitalmars-d-learn wrote:

[...]


In theory, Ken Thompson's compromised compiler hack could be at 
work[1].


[...]


I think you say advanced technical information. My information is 
not at that level.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jul 28, 2022 at 04:45:55PM +, pascal111 via Digitalmars-d-learn 
wrote:
> On Thursday, 28 July 2022 at 16:37:35 UTC, frame wrote:
> > On Thursday, 28 July 2022 at 16:17:16 UTC, pascal111 wrote:
> > 
> > > My friend, there is a wide deep secret world for hackers. We have
> > > no any idea about that world. Look, there is nothing called a 100%
> > > fact in our world. Believe me, what we see in software is just
> > > what "THEY" want us to see.
> > 
> > I think you have no idea how some processes work.
> > 
> > We have cryptographic digest methods to verify source code and final
> > builds. In theory, someone could inject bad code if nobody would
> > review it properly of course. But especially for compilers such code
> > would be detected soon and no insane person in such projects would
> > just merge code without reviewing it.
> > 
> > That applies for open source - not if you just download a compiled
> > binary from a ftp server in the open web of course :D
> 
> Aha! "In theory, someone could inject bad code", you admit my theory.

In theory, Ken Thompson's compromised compiler hack could be at work[1].

In practice, though, especially for open-source projects where you can
take the code and compile it with any of number of 3rd party compilers
(at least one of which would be unlikely to have been compiled by a
compromised compiler, so would be "clean"), or, for that matter, you can
freely *modify* the code to replace arbitrary parts of it with
semantically-equivalent code that no longer matches the hack-triggering
pattern, it would take an unreal amount of influence over the entire
world to be able to pull off such a hack.

If somebody actually wielded that much influence over your software, you
already have far bigger problems to worry about; whether or not your
software is being compiled with hidden backdoors is already a moot
question. :-D  (And your efforts to write only "purely" your own code
would also be futile anyway, esp. in a Thompson's-hack scenario.)


[1] 
https://www.cs.cmu.edu/~rdriley/487/papers/Thompson_1984_ReflectionsonTrustingTrust.pdf


T

-- 
The early bird gets the worm. Moral: ewww...


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 16:37:35 UTC, frame wrote:

On Thursday, 28 July 2022 at 16:17:16 UTC, pascal111 wrote:

My friend, there is a wide deep secret world for hackers. We 
have no any idea about that world. Look, there is nothing 
called a 100% fact in our world. Believe me, what we see in 
software is just what "THEY" want us to see.


I think you have no idea how some processes work.

We have cryptographic digest methods to verify source code and 
final builds. In theory, someone could inject bad code if 
nobody would review it properly of course. But especially for 
compilers such code would be detected soon and no insane person 
in such projects would just merge code without reviewing it.


That applies for open source - not if you just download a 
compiled binary from a ftp server in the open web of course :D


Aha! "In theory, someone could inject bad code", you admit my 
theory.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 July 2022 at 16:17:16 UTC, pascal111 wrote:

My friend, there is a wide deep secret world for hackers. We 
have no any idea about that world. Look, there is nothing 
called a 100% fact in our world. Believe me, what we see in 
software is just what "THEY" want us to see.


I think you have no idea how some processes work.

We have cryptographic digest methods to verify source code and 
final builds. In theory, someone could inject bad code if nobody 
would review it properly of course. But especially for compilers 
such code would be detected soon and no insane person in such 
projects would just merge code without reviewing it.


That applies for open source - not if you just download a 
compiled binary from a ftp server in the open web of course :D


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 16:13:17 UTC, frame wrote:

On Thursday, 28 July 2022 at 14:57:36 UTC, pascal111 wrote:

well between US and some other countries like "Russia", and 
they are using US products like C compilers, so with some way 
we have a doubt that US developed compilers with a way to 
accept kind of messages or something like that, so my comment 
has no meaning to the compiler except if I knew the secret 
patterns or I do it accidentally.


Wait. You mean asterisks in comments may trigger some secret 
backdoor function in C-compilers? Come on :D


My friend, there is a wide deep secret world for hackers. We have 
no any idea about that world. Look, there is nothing called a 
100% fact in our world. Believe me, what we see in software is 
just what "THEY" want us to see.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 July 2022 at 14:57:36 UTC, pascal111 wrote:

well between US and some other countries like "Russia", and 
they are using US products like C compilers, so with some way 
we have a doubt that US developed compilers with a way to 
accept kind of messages or something like that, so my comment 
has no meaning to the compiler except if I knew the secret 
patterns or I do it accidentally.


Wait. You mean asterisks in comments may trigger some secret 
backdoor function in C-compilers? Come on :D


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 15:55:04 UTC, kdevel wrote:

On Thursday, 28 July 2022 at 14:57:36 UTC, pascal111 wrote:

[...]
Sure. What effect do YOU hope to causes or prevent by writing

```
/**/
```

between all of your functions?


I'm normal programmer, by mean that I'm not so expert in C 
matters to know really if there are particular patterns that 
have specific meanings for the compiler or not.


I was asking why /you/ put



I'm trying to answer you; remember that your question isn't 
simple as it seemed, so it needs rich explaining.



```
/**/
```

between function definitions. You cannot or do not want to 
answer that question. I think that this pattern or habit is not 
your invention. You borrowed that style from someone else and 
did not ask him or her for its purpose. Right?


Really I want to answer you, but yes, maybe you are right that I 
saw this pattern before, but I don't remember where? really I 
don't remember where I saw it. I thought it's useless pattern.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread kdevel via Digitalmars-d-learn

On Thursday, 28 July 2022 at 14:57:36 UTC, pascal111 wrote:

[...]
Sure. What effect do YOU hope to causes or prevent by writing

```
/**/
```

between all of your functions?


I'm normal programmer, by mean that I'm not so expert in C 
matters to know really if there are particular patterns that 
have specific meanings for the compiler or not.


I was asking why /you/ put

```
/**/
```

between function definitions. You cannot or do not want to answer 
that question. I think that this pattern or habit is not your 
invention. You borrowed that style from someone else and did not 
ask him or her for its purpose. Right?





Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 14:44:53 UTC, kdevel wrote:

On Thursday, 28 July 2022 at 13:58:24 UTC, pascal111 wrote:
[...]
Precisely in what way? I am not kidding. I am seriously 
asking the question: In what way may a C or C++ compiler 
benefit from lines between functions which contain only 
comments consisting of nothing else than asterisks?


Seriously and accurately, if you meant that, we don't know how 
developers of C compilers programmed their compilers, for 
example, maybe there's a secret way the developers programmed 
their compiler so that a special pattern of comments can have 
a particular meaning to the compilers. We don't know! maybe 
it's true.


Sure. What effect do YOU hope to causes or prevent by writing

```
/**/
```

between all of your functions?


I'm normal programmer, by mean that I'm not so expert in C 
matters to know really if there are particular patterns that have 
specific meanings for the compiler or not. I know this forum is 
not for political topics, but by the way I would like to mention 
something: everyone knows that the affairs are not well between 
US and some other countries like "Russia", and they are using US 
products like C compilers, so with some way we have a doubt that 
US developed compilers with a way to accept kind of messages or 
something like that, so my comment has no meaning to the compiler 
except if I knew the secret patterns or I do it accidentally.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread kdevel via Digitalmars-d-learn

On Thursday, 28 July 2022 at 13:58:24 UTC, pascal111 wrote:
[...]
Precisely in what way? I am not kidding. I am seriously asking 
the question: In what way may a C or C++ compiler benefit from 
lines between functions which contain only comments consisting 
of nothing else than asterisks?


Seriously and accurately, if you meant that, we don't know how 
developers of C compilers programmed their compilers, for 
example, maybe there's a secret way the developers programmed 
their compiler so that a special pattern of comments can have a 
particular meaning to the compilers. We don't know! maybe it's 
true.


Sure. What effect do YOU hope to causes or prevent by writing

```
/**/
```

between all of your functions?



Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 13:29:01 UTC, kdevel wrote:

On Thursday, 28 July 2022 at 12:44:19 UTC, pascal111 wrote:

[...]
Do you think it helps the compiler if you put these

`/**/`

between your functions? Or is there anybody else who benefits 
from it?


"Do you think it helps the compiler if you put these"
Are you serious? maybe it's useful for compilers with some way.


Precisely in what way? I am not kidding. I am seriously asking 
the question: In what way may a C or C++ compiler benefit from 
lines between functions which contain only comments consisting 
of nothing else than asterisks?


Seriously and accurately, if you meant that, we don't know how 
developers of C compilers programmed their compilers, for 
example, maybe there's a secret way the developers programmed 
their compiler so that a special pattern of comments can have a 
particular meaning to the compilers. We don't know! maybe it's 
true.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 13:06:03 UTC, kdevel wrote:

On Thursday, 28 July 2022 at 12:25:05 UTC, pascal111 wrote:

[...]

ofix.c: In function 'fix':
ofix.c:7:3: warning: 'z' is used uninitialized 
[-Wuninitialized]

7 | y=modf(x,z);
  |   ^
ofix.c:5:12: note: 'z' was declared here
5 | double y,* z;
  |^
```

I would also like to complain about the double assignment to 
`y`.


"fix" function is the C version of BASIC standard one I made, 
it's the equivalent of "trunc" in D, but the code I programmed 
for "fix" was with TC++ , and it worked in DOS emulators with 
no problem,


"Code works" and "Code is correct" are two distinct categories. 
The compiler's warnings show that your code is not correct and 
not that it will necessarily not work. Incorrect code may work 
accidentally.




for that we are - guys of C - prefer C of other languages like 
Pascal, because C allow us to have working programs although they 
are not 100% correct, but something like Pascal is so hard and 
difficult in its compiling, it'll give us an error for even a so 
small expression, so we don't like the much complaints of 
compilers, and C is so forgiving language.


As a (future) software developer you are required to not just 
produce code that "works" "with no problem" but also code that 
is correct. "Correct" here means that the code adheres to the 
specifications of the language.


Writing incorrect code is like "fixing" a "broken" fuse with 
tin foil.


I have no idea how gcc will treat my code, but I think you are 
right that some other compilers will refuse such code, because 
VC++ 6 refused many of these functions codes I programmed with 
TC++ compiler.


The incorrectness of your C code does not depend on the 
compiler brand.


I have to change the compiler, not the code!



Re: Some user-made C functions and their D equivalents

2022-07-28 Thread kdevel via Digitalmars-d-learn

On Thursday, 28 July 2022 at 12:44:19 UTC, pascal111 wrote:

[...]
Do you think it helps the compiler if you put these

`/**/`

between your functions? Or is there anybody else who benefits 
from it?


"Do you think it helps the compiler if you put these"
Are you serious? maybe it's useful for compilers with some way.


Precisely in what way? I am not kidding. I am seriously asking 
the question: In what way may a C or C++ compiler benefit from 
lines between functions which contain only comments consisting of 
nothing else than asterisks?




Re: Some user-made C functions and their D equivalents

2022-07-28 Thread kdevel via Digitalmars-d-learn

On Thursday, 28 July 2022 at 12:25:05 UTC, pascal111 wrote:

[...]

ofix.c: In function 'fix':
ofix.c:7:3: warning: 'z' is used uninitialized 
[-Wuninitialized]

7 | y=modf(x,z);
  |   ^
ofix.c:5:12: note: 'z' was declared here
5 | double y,* z;
  |^
```

I would also like to complain about the double assignment to 
`y`.


"fix" function is the C version of BASIC standard one I made, 
it's the equivalent of "trunc" in D, but the code I programmed 
for "fix" was with TC++ , and it worked in DOS emulators with 
no problem,


"Code works" and "Code is correct" are two distinct categories. 
The compiler's warnings show that your code is not correct and 
not that it will necessarily not work. Incorrect code may work 
accidentally.


As a (future) software developer you are required to not just 
produce code that "works" "with no problem" but also code that is 
correct. "Correct" here means that the code adheres to the 
specifications of the language.


Writing incorrect code is like "fixing" a "broken" fuse with tin 
foil.


I have no idea how gcc will treat my code, but I think you are 
right that some other compilers will refuse such code, because 
VC++ 6 refused many of these functions codes I programmed with 
TC++ compiler.


The incorrectness of your C code does not depend on the compiler 
brand.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 12:36:59 UTC, kdevel wrote:

On Thursday, 28 July 2022 at 12:26:50 UTC, pascal111 wrote:

[...]
Aha! you mean "/**/", 
it has no job, just to separate between functions codes.


Do you think it helps the compiler if you put these

`/**/`

between your functions? Or is there anybody else who benefits 
from it?


"Do you think it helps the compiler if you put these"
Are you serious? maybe it's useful for compilers with some way.

"between your functions? Or is there anybody else who benefits
 from it?"

Your question isn't clear, but the code is free for learning 
purposes. I shared it in public for everyone thinks it useful to 
him.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread kdevel via Digitalmars-d-learn

On Thursday, 28 July 2022 at 12:26:50 UTC, pascal111 wrote:

[...]
Aha! you mean "/**/", 
it has no job, just to separate between functions codes.


Do you think it helps the compiler if you put these

`/**/`

between your functions? Or is there anybody else who benefits 
from it?


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 12:15:19 UTC, kdevel wrote:

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:

[...]


1. What exact purpose do these

```
/**/
```

[...]


Aha! you mean "/**/", it 
has no job, just to separate between functions codes.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 12:15:19 UTC, kdevel wrote:

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:
I made a library of some useful functions while I was studying 
C, and I'm curious to know if D has equivalents or some ones 
for some of my functions, or I have to retype 'em again in D.


The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H


1. What exact purpose do these

```
/**/
```

have?

2.
```
double fix(double x)
{

double y,* z;

y=modf(x,z);
y=*z;

return y;

}
```
Besides the remarkable formatting there is an issue with the 
code. According to the manual the modf function


```
double modf(double x, double *iptr);
```

stores "The integral part [of x] [...] in the location pointed 
to by iptr." If you ask the compiler for help (e.g. `gcc -Wall 
-pedantic`) it will tell you what's wrong:


```
ofix.c: In function 'fix':
ofix.c:7:3: warning: 'z' is used uninitialized [-Wuninitialized]
7 | y=modf(x,z);
  |   ^
ofix.c:5:12: note: 'z' was declared here
5 | double y,* z;
  |^
```

I would also like to complain about the double assignment to 
`y`.


"fix" function is the C version of BASIC standard one I made, 
it's the equivalent of "trunc" in D, but the code I programmed 
for "fix" was with TC++ , and it worked in DOS emulators with no 
problem, I have no idea how gcc will treat my code, but I think 
you are right that some other compilers will refuse such code, 
because VC++ 6 refused many of these functions codes I programmed 
with TC++ compiler.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread kdevel via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:
I made a library of some useful functions while I was studying 
C, and I'm curious to know if D has equivalents or some ones 
for some of my functions, or I have to retype 'em again in D.


The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H


1. What exact purpose do these

```
/**/
```

have?

2.
```
double fix(double x)
{

double y,* z;

y=modf(x,z);
y=*z;

return y;

}
```
Besides the remarkable formatting there is an issue with the 
code. According to the manual the modf function


```
double modf(double x, double *iptr);
```

stores "The integral part [of x] [...] in the location pointed to 
by iptr." If you ask the compiler for help (e.g. `gcc -Wall 
-pedantic`) it will tell you what's wrong:


```
ofix.c: In function 'fix':
ofix.c:7:3: warning: 'z' is used uninitialized [-Wuninitialized]
7 | y=modf(x,z);
  |   ^
ofix.c:5:12: note: 'z' was declared here
5 | double y,* z;
  |^
```

I would also like to complain about the double assignment to `y`.


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread pascal111 via Digitalmars-d-learn

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=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;

}



Re: Some user-made C functions and their D equivalents

2022-07-28 Thread Dennis via Digitalmars-d-learn

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;
```


Re: Some user-made C functions and their D equivalents

2022-07-28 Thread kdevel via Digitalmars-d-learn

On Thursday, 28 July 2022 at 00:46:19 UTC, pascal111 wrote:

On Thursday, 28 July 2022 at 00:36:54 UTC, ryuukk_ wrote:
I don't remember the exact syntax for GDC, but it should be 
pretty similar to DMD


You need to pass the module to the compiler

gdc main.d dcollect.d


I'm using CODE::BLOCKS IDE. How can I do it through it?


You should probably ask this question in a CODE::BLOCKS forum.

Further reading:

- Benefits of Not Using an IDE
  

- Why You Shouldn’t Always Use an IDE
  



Re: Some user-made C functions and their D equivalents

2022-07-27 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 00:36:54 UTC, ryuukk_ wrote:
I don't remember the exact syntax for GDC, but it should be 
pretty similar to DMD


You need to pass the module to the compiler

gdc main.d dcollect.d


I'm using CODE::BLOCKS IDE. How can I do it through it?


Re: Some user-made C functions and their D equivalents

2022-07-27 Thread ryuukk_ via Digitalmars-d-learn
I don't remember the exact syntax for GDC, but it should be 
pretty similar to DMD


You need to pass the module to the compiler

gdc main.d dcollect.d


Re: Some user-made C functions and their D equivalents

2022-07-27 Thread pascal111 via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 19:07:26 UTC, ryuukk_ wrote:

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:
I made a library of some useful functions while I was studying 
C, and I'm curious to know if D has equivalents or some ones 
for some of my functions, or I have to retype 'em again in D.


The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H


D ships with libc so everything in there is available

```C
#include
#include
#include
#include
#include
```


In d the equivalent imports:

```D
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import core.stdc.ctype;
import core.stdc.math;
```


Ok, but before that, I faced a problem when I tried to make my 
module. I tried to make a test. I made a testing module called 
"dcollect.d" and I put it in this path on my Ubuntu 
"/usr/lib/gcc/x86_64-linux-gnu/11/include/d".


module dcollect;

import std.stdio;

int foo22()
{

return 5;

}

Then I called "foo22" in a testing program and found an error 
like (this one in another try by telling code::blocks about local 
searching folder for the compiler I put the module in) "||=== 
Build: Debug in temp (compiler: GDC D Compiler) ===|
/home/pascal111/My Projects/D/temp/hello.d|23|undefined reference 
to `_D8dcollect5foo22FZi'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 
second(s)) ===|"


The testing program:

module main;

import std.stdio;
import std.string;
import std.conv;
import dcollect;

int main(string[] args)
{

string ch;

try{
ch=readln();
ch=strip(ch);

if(to!int(ch)<0)
throw new Exception ("You entered negative number!");}

catch (Exception e){
writeln(e.msg);}

writeln(foo22());

return 0;
}




Re: Some user-made C functions and their D equivalents

2022-07-27 Thread ryuukk_ via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:
I made a library of some useful functions while I was studying 
C, and I'm curious to know if D has equivalents or some ones 
for some of my functions, or I have to retype 'em again in D.


The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H


D ships with libc so everything in there is available

```C
#include
#include
#include
#include
#include
```


In d the equivalent imports:

```D
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import core.stdc.ctype;
import core.stdc.math;
```


Some user-made C functions and their D equivalents

2022-07-27 Thread pascal111 via Digitalmars-d-learn
I made a library of some useful functions while I was studying C, 
and I'm curious to know if D has equivalents or some ones for 
some of my functions, or I have to retype 'em again in D.


The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H