Re: please help me to reverse a function call order
On Monday, 25 July 2022 at 04:23:16 UTC, test123 wrote: not match by: 1) the element is not repeat 2) the element is position related I got it now. So what good are such repetitive probabilities? Thanks... SDB@79
Re: "string" data type with readln
The version of readln you are using is[0]. This works by taking in a buffer of memory to write out, and returns how many codepoints were stored. Because you are not reusing memory, not using this form you can of course use string[1] instead, rather than ``char[]``. ```d string s = readln(); ``` The definition of string is[2]: ```d alias string = immutable(char)[]; ``` Note the immutable there, which means that each value in the slice cannot be modified. Hence why it can't be used as a buffer. [0] https://dlang.org/phobos/std_stdio.html#.readln.2 [1] https://dlang.org/phobos/std_stdio.html#.readln [2] https://github.com/dlang/dmd/blob/master/druntime/src/object.d#L69
Re: "string" data type with readln
On Monday, 25 July 2022 at 19:55:40 UTC, pascal111 wrote: I tried to type small program, and tried to use "string" data type with "readln", but the compiler refused it, and I had to use "char[]". the overload you used modifies the array you give it try string s = readln();
"string" data type with readln
I tried to type small program, and tried to use "string" data type with "readln", but the compiler refused it, and I had to use "char[]". So, if we can't use "string" with "readln", so what's its benefit? why we need to use it? Code source: https://github.com/pascal111-fra/D/blob/main/proj01.d
Re: char* pointers between C and D
This is how to do it the D way: ``` int main(string[] args) { string ch1 = "Hello World!"; char[] ch2="Hello World!".dup; string s1=ch1[1..$]; char[] s2=ch2[1..$]; writeln(s1); writeln(s2); return 0; } ```
Re: char* pointers between C and D
On Mon, Jul 25, 2022 at 05:30:14PM +, pascal111 via Digitalmars-d-learn wrote: [...] > int main(string[] args) > { > > > const(char)[] ch1 = "Hello World!"; > char[] ch2="Hello World!".dup; > > const(char) *p1; > char *p2; > > ch2~="\0"; > > p1=ch1.ptr; > p2=ch2.ptr; > > writeln(p1[0..strlen(p1)]); > writeln(p2[0..strlen(p2)]); Unless you are passing the string back to C code as char*, there is no need to use strlen here. You can just use `writeln(p1);` to output the entire string, or slice it with `p1[0 .. $]` to get the entire string. T -- Being able to learn is a great learning; being able to unlearn is a greater learning.
Re: char* pointers between C and D
On Monday, 25 July 2022 at 13:51:35 UTC, ryuukk_ wrote: On Monday, 25 July 2022 at 11:14:56 UTC, pascal111 wrote: On Monday, 25 July 2022 at 09:36:05 UTC, ryuukk_ wrote: [...] I tried your advice with two ways; once with a constant and other with an array, but the result isn't the same. The array case has more letters in the output. module main; import std.stdio; import core.stdc.stdio; import core.stdc.string; int main(string[] args) { const(char)[] ch1 = "Hello World!"; char[] ch2="Hello World!".dup; const(char) *p1; char *p2; p1=ch1.ptr; p2=ch2.ptr; writeln(p1[0..strlen(p1)]); writeln(p2[0..strlen(p2)]); return 0; } Runtime output: https://i.postimg.cc/sfnkJ4GM/Screenshot-from-2022-07-25-13-12-03.png `ch1`is a string literal, just like in C, it is null terminated `ch2` is a GC allocated char array, it is NOT null terminated `strlen` is the lib C function, it counts strings up to `\O` for `p1` it'll print correctly, it is a pointer from the null terminated string for `p2` strlen doesn't make sense, since it is a pointer from a string that is NOT null terminated Yes, I have to add "\0": module main; import std.stdio; import core.stdc.stdio; import core.stdc.string; int main(string[] args) { const(char)[] ch1 = "Hello World!"; char[] ch2="Hello World!".dup; const(char) *p1; char *p2; ch2~="\0"; p1=ch1.ptr; p2=ch2.ptr; writeln(p1[0..strlen(p1)]); writeln(p2[0..strlen(p2)]); return 0; }
Re: char* pointers between C and D
On Monday, 25 July 2022 at 11:14:56 UTC, pascal111 wrote: On Monday, 25 July 2022 at 09:36:05 UTC, ryuukk_ wrote: Here is the way to do it with `writefln` (i think) ```D import std; import core.stdc.string; void main() { const(char)[] ch = "Hello World!"; const(char)[] ch2 = "abc"; const(char)* p; p = [0]; p++; auto str = p[0 .. strlen(p)]; writefln("%s", str); } ``` Note that i removed your `.dup` it is unecessary, string literals needs `const(char)` After looking at the doc, `writefln` needs a slice, so we just do that and pass it I tried your advice with two ways; once with a constant and other with an array, but the result isn't the same. The array case has more letters in the output. module main; import std.stdio; import core.stdc.stdio; import core.stdc.string; int main(string[] args) { const(char)[] ch1 = "Hello World!"; char[] ch2="Hello World!".dup; const(char) *p1; char *p2; p1=ch1.ptr; p2=ch2.ptr; writeln(p1[0..strlen(p1)]); writeln(p2[0..strlen(p2)]); return 0; } Runtime output: https://i.postimg.cc/sfnkJ4GM/Screenshot-from-2022-07-25-13-12-03.png `ch1`is a string literal, just like in C, it is null terminated `ch2` is a GC allocated char array, it is NOT null terminated `strlen` is the lib C function, it counts strings up to `\O` for `p1` it'll print correctly, it is a pointer from the null terminated string for `p2` strlen doesn't make sense, since it is a pointer from a string that is NOT null terminated
does the format of coverage files have a name?
or was the .lst extension chosen arbitrarily? my text editor [notepad++] thinks it's COBOL for some reason but that's obviously not correct, so i'm wondering if it has an official spec or anything. knowing the name of it would help - maybe my editor already has syntax highlighting for it.
Re: Expanding CTFE code during compilation
On 7/23/22 2:27 PM, Azi Hassan wrote: On Saturday, 23 July 2022 at 00:56:39 UTC, Steven Schveighoffer wrote: On 7/22/22 3:22 PM, Azi Hassan wrote: Oh, interesting syntax. I was thinking something along the lines of ```D template printEnum(...) { version(debug) { ... // everything we already did } else { enum printEnum(alias x) = x; } } ``` But I like yours better. `version(debug)` isn't valid syntax, `debug` is a keyword. I stand corrected, I must've got it confused with version(unittest) And *I* stand corrected! `unittest` is also a keyword, so clearly that's not disqualifying. Indeed, the grammar specifically includes `version(unittest)` and `version(assert)`. However, because `debug` is exactly like `version` in how it operates, `version(debug)` would make little sense. -Steve
Re: char* pointers between C and D
On Monday, 25 July 2022 at 11:14:56 UTC, pascal111 wrote: module main; import std.stdio; import core.stdc.stdio; import core.stdc.string; int main(string[] args) { const(char)[] ch1 = "Hello World!"; char[] ch2="Hello World!".dup; const(char) *p1; char *p2; p1=ch1.ptr; p2=ch2.ptr; writeln(p1[0..strlen(p1)]); writeln(p2[0..strlen(p2)]); return 0; } Runtime output: https://i.postimg.cc/sfnkJ4GM/Screenshot-from-2022-07-25-13-12-03.png Do not post screenshots of text. `strlen` only works on null-terminated strings. The result of `.dup` is not null-terminated, so `strlen` doesn't work on it.
Re: char* pointers between C and D
On Monday, 25 July 2022 at 09:36:05 UTC, ryuukk_ wrote: Here is the way to do it with `writefln` (i think) ```D import std; import core.stdc.string; void main() { const(char)[] ch = "Hello World!"; const(char)[] ch2 = "abc"; const(char)* p; p = [0]; p++; auto str = p[0 .. strlen(p)]; writefln("%s", str); } ``` Note that i removed your `.dup` it is unecessary, string literals needs `const(char)` After looking at the doc, `writefln` needs a slice, so we just do that and pass it I tried your advice with two ways; once with a constant and other with an array, but the result isn't the same. The array case has more letters in the output. module main; import std.stdio; import core.stdc.stdio; import core.stdc.string; int main(string[] args) { const(char)[] ch1 = "Hello World!"; char[] ch2="Hello World!".dup; const(char) *p1; char *p2; p1=ch1.ptr; p2=ch2.ptr; writeln(p1[0..strlen(p1)]); writeln(p2[0..strlen(p2)]); return 0; } Runtime output: https://i.postimg.cc/sfnkJ4GM/Screenshot-from-2022-07-25-13-12-03.png
Re: char* pointers between C and D
On Monday, 25 July 2022 at 09:04:29 UTC, pascal111 wrote: I have small C program that uses a pointer to change the start address of a string, and when I tried to do the same code but with D, the D code printed the address of the string after I increased it one step instead of printing the string the pointer pointing to. Is there a difference between "char *" pointers between C and D. No, no difference. Pointers are the same in both languages. What's different is the behavior of `%s` in `writeln` vs `printf`. See the documentation on format strings at: https://dlang.org/phobos/std_format.html Essentially, `%s` tells the formatter to output something appropriate for the given type. For an actual D string, you see the text. For an integral or floating point type, you see the number. For a pointer, you see the the address. And so on. Do in your case, to get `writefln` to print the text instead of the pointer address, you could import `std.string` and use `fromStringz`:`fromStringz(p)`. This will give you a D string without allocating any memory. Basically an immutable slice of the memory pointed at by `p`. That's fine for this use case, but if you wanted to hang on to the string beyond the lifetime of the pointer, you'd have to use `std.conv.to` instead (e.g., `to!string(p)`).
Re: char* pointers between C and D
Here is the way to do it with `writefln` (i think) ```D import std; import core.stdc.string; void main() { const(char)[] ch = "Hello World!"; const(char)[] ch2 = "abc"; const(char)* p; p = [0]; p++; auto str = p[0 .. strlen(p)]; writefln("%s", str); } ``` Note that i removed your `.dup` it is unecessary, string literals needs `const(char)` After looking at the doc, `writefln` needs a slice, so we just do that and pass it
Re: char* pointers between C and D
I don't know what `writefln` is doing, but this following D code is the exact similar to your C code ``` import core.stdc.stdio; void main() { const(char)[] ch = "Hello World!"; const(char)* p; p = [0]; p++; printf("%s", p); } ``` `ello World!`
char* pointers between C and D
I have small C program that uses a pointer to change the start address of a string, and when I tried to do the same code but with D, the D code printed the address of the string after I increased it one step instead of printing the string the pointer pointing to. Is there a difference between "char *" pointers between C and D. #include #include int main() { char ch[]="Hello World!"; char *p; p= p++; printf("%s\n", p); return 0; } module main; import std.stdio; int main(string[] args) { char[] ch="Hello World!".dup; char *p; p=[0]; p++; writefln("%s", p); return 0; }
Re: Choosing the correct compiler version
On Thursday, 21 July 2022 at 05:44:41 UTC, Alexander Zhirov wrote: I will report on the successes a little later. Result: I downloaded and unpacked the binary version of the `dmd` compiler version [2.097.2](http://downloads.dlang.org/releases/2021/dmd.2.097.2.linux.tar.xz), which runs without problems with my `glibc` set. Then I compiled the `GCC` compiler version 9.5.0 from the source code: ```sh wget https://ftp.mpi-inf.mpg.de/mirrors/gnu/mirror/gcc.gnu.org/pub/gcc/releases/gcc-9.5.0/gcc-9.5.0.tar.gz mkdir build-gcc && cd build-gcc ../gcc-9.5.0/configure --prefix=$PWD/../install-gcc --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c,c++ make -j16 make install ``` Then I built `LLVM` using this compiler, according to the instructions on the [LDC website](https://wiki.dlang.org/Building_LDC_from_source#Building_LLVM_from_source): ```sh export CC=/root/source/gcc/gcc-install/bin/gcc export CXX=/root/source/gcc/gcc-install/bin/g++ mkdir build-llvm && cd build-llvm cmake ../llvm-10.0.1.src -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$PWD/../install-llvm -DLLVM_BINUTILS_INCDIR=/usr/include -DLLVM_TARGETS_TO_BUILD='X86' -DCOMPILER_RT_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_TESTS=OFF make -j16 make install ``` And then I built the `ldc` compiler myself, also according to the instructions on the [website](https://wiki.dlang.org/Building_LDC_from_source#Building_LDC_from_source): ```sh git clone --recursive https://github.com/ldc-developers/ldc.git mkdir build-ldc && cd build-ldc cmake ../ldc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$PWD/../install-ldc -DLLVM_ROOT_DIR= -DD_COMPILER= make -j16 make install ``` As a result, I built the `ldc` compiler for my `x32` machine: ```sh # ldc2 --version LDC - the LLVM D compiler (1.30.0): based on DMD v2.100.1 and LLVM 10.0.1 built with DMD32 D Compiler v2.097.2 Default target: i686-pc-linux-gnu Host CPU: broadwell http://dlang.org - http://wiki.dlang.org/LDC Registered Targets: x86- 32-bit X86: Pentium-Pro and above x86-64 - 64-bit X86: EM64T and AMD64 ``` I hope my guide will be useful to someone who will face the same task. Thank you for your help!