Re: std.file: read, readText and UTF-8 decoding
On Friday, September 22, 2023 12:28:39 AM MDT Uranuz via Digitalmars-d-learn wrote: > OK. Thanks for response. I wish that there it was some API to > handle it "out of the box". Do I need to write some issue or > something in order to not forget about this? You can open an issue if you want, though I don't know how much that will help it be remembered given how many issues ther are to sort through. I'll probably get around to writing something eventually (particularly since this issue is more likely to come up when using dxml than with many other use cases), but I have a variety of items on my todo list. - Jonathan M Davis
Re: C to D: please help translate this weird macro
On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven wrote: The 1st argument of `getMember` can just be T, like the original macro. The 2nd argument needs to be a compile-time string. Also the `char*` cast needs to apply to `ptr` before subtracting the offset AFAICS. So reordering to keep type inference of `ptr`: ```d auto nk_container_of(T, string member, P)(P ptr) { return cast(T*)(cast(void*)(cast(char*)ptr - __traits(getMember, T, member).offsetof))); } ``` (Untested) I will test it:)
Re: C to D: please help translate this weird macro
On Thursday, 21 September 2023 at 16:50:51 UTC, Imperatorn wrote: On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote: Here is the macro: ```C #define NK_CONTAINER_OF(ptr,type,member)\ (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member))) ``` I'm trying to translate the Nuklear GUI library to D [here](https://github.com/rillki/nuklear-d/tree/nuklear-d-translation). When you're done, will you put it on dub? Yes, I will.
Re: change object class
On Friday, 22 September 2023 at 03:33:08 UTC, Vitaliy Fadeev wrote: On Friday, 22 September 2023 at 02:51:10 UTC, Vitaliy Fadeev wrote: ... ``` Chip id name Sense() Draw() ``` instance ``` chip = new Chip(); ``` compiled to ``` chip __vtbl -> Chip __monitor Sense() idDraw() name ``` I want ``` chip __vtbl --+ id | name | |-> Chip_Hovered | Sense() | Draw() | +-> Chip_Hovered Sense() Draw() ``` What I mean is, why not use other language constructs like mixins or inheritance with some mapping for example?
Re: parallelism with delegate
On Friday, 22 September 2023 at 04:24:19 UTC, Vitaliy Fadeev wrote: able ? how to use correctly? ```d import std.parallelism; auto async_task = task!fn( args ); // error // Error: no property `opCall` for type `app.A`, did you mean `new A`? async_task.executeInNewThread(); ``` where ```d auto a = new A(); auto fn = &a.download; class A { void fn( string url ) { // DO } } ``` Playground: https://run.dlang.io/is/HvhtoP gist: https://gist.github.com/run-dlang/218b69e1afd79e5944ea10aa7ca61e1b Also check out std.concurrency
Re: change object class
On Friday, 22 September 2023 at 12:53:28 UTC, Imperatorn wrote: On Friday, 22 September 2023 at 03:33:08 UTC, Vitaliy Fadeev wrote: On Friday, 22 September 2023 at 02:51:10 UTC, Vitaliy Fadeev wrote: ... ``` Chip id name Sense() Draw() ``` instance ``` chip = new Chip(); ``` compiled to ``` chip __vtbl -> Chip __monitor Sense() idDraw() name ``` I want ``` chip __vtbl --+ id | name | |-> Chip_Hovered | Sense() | Draw() | +-> Chip_Hovered Sense() Draw() ``` What I mean is, why not use other language constructs like mixins or inheritance with some mapping for example? Can you give an example?
odd bit optimized function
x86 first bit check (odd check): ```asm AND EAX, EAX ; update FLAG OF // odd flag JO Label ``` Is there D-Lang operator of optimized library function ?
Request you advise : isValidFilename
Hi All, Request you help in understanding why the below code is always returning true when it should return false as per the documentation. Documentation ``` Checks that the given file or directory name is valid. The maximum length of filename is given by the constant core.stdc.stdio.FILENAME_MAX. (On Windows, this number is defined as the maximum number of UTF-16 code points, and the test will therefore only yield strictly correct results when filename is a string of wchars.) On Windows, the following criteria must be satisfied (source): ** filename must not contain any characters whose integer representation is in the range 0-31. ** filename must not contain any of the following reserved characters: <>:"/\|?* filename may not end with a space (' ') or a period ('.'). On POSIX, filename may not contain a forward slash ('/') or the null character ('\0'). ``` Code: ``` import std.stdio: writefln; import std.path: baseName, isValidFilename; import std.utf : byCodeUnit; void main () { string st1 = "C:\\Windows\\System32\\whoami1"; string st2 = "C:\\Windows\\System32\\whoami*"; string st3 = "C:\\Windows\\System32\\whoami."; string st4 = "C:\\Windows\\System32\\whoami "; writefln("(isValidFilename : %s)", isValidFilename(baseName(st1).byCodeUnit)); writefln("(isValidFilename : %s)", isValidFilename(baseName(st2).byCodeUnit)); writefln("(isValidFilename : %s)", isValidFilename(baseName(st3).byCodeUnit)); writefln("(isValidFilename : %s)", isValidFilename(baseName(st4).byCodeUnit)); } ``` Output: ``` (isValidFilename : true) // shouldn't it be false as it contain's the number whoami1 (isValidFilename : false) (isValidFilename : false) (isValidFilename : false) ``` From, Vino
Re: Request you advise : isValidFilename
On Friday, 22 September 2023 at 17:44:50 UTC, Vino wrote: Hi All, Request you help in understanding why the below code is always returning true when it should return false as per the documentation. Documentation ``` filename must not contain any characters whose integer representation is in the range 0-31. ``` "Integer representation" here refers to the ANSI index or Unicode codepoint, ie. the filename must not contain `\x00` to `\x31`.
Re: Request you advise : isValidFilename
On Friday, 22 September 2023 at 17:52:51 UTC, FeepingCreature wrote: "Integer representation" here refers to the ANSI index or Unicode codepoint, ie. the filename must not contain `\x00` to `\x31`. Er oops, make that `\x00` to `\x1f`.
Re: change object class
On 17.09.23 17:05, Vitaliy Fadeev wrote: Hi! I want to change a method ```Draw``` on a custom object when the ```MouseIn``` event occurs. This is known as "Change State" of the object: ```Init``` -> ```Hovered```. I want to change the state of an object by changing its class, like this: ```d this.__vptr = typeid(CLS).vtbl.ptr; ``` I have read the ABI and am confident in replacing ```__vptr``` as long as the classes contain the same fields and the same interfaces. Example: ```d // O // to!state // State_Init : O // Draw // State_Hovered : O // Draw // // o.to!State_Hovered // o.to!State_Init class O { void to(CLS)() { // if (same fields && same interfaces && same instance size) this.__vptr = cast(immutable(void*)*)typeid(CLS).vtbl.ptr; } } State_Init : O void Draw() { /* ... */ } State_Hovered : O void Draw() { /* ... */ } ``` when MouseIn: ```d ... o.to!State_Hovered(); ... ``` when MouseOut: ```d ... o.to!State_Init(); ... ``` It works! But I want to ask how to make this 100% the best of the best? What should I consider before changing ```__vptr``` ? You could model it oop style like this: https://run.dlang.io/is/MJb5Fk This solution might not be to your taste, as it involves interfaces, and classes and objects and garbage (all the news) ... another option could be to model your own VTable in a struct like this: https://run.dlang.io/is/3LTjP5 Kind regards, Christian
Re: change object class
On Friday, 22 September 2023 at 14:03:40 UTC, Vitaliy Fadeev wrote: On Friday, 22 September 2023 at 12:53:28 UTC, Imperatorn wrote: On Friday, 22 September 2023 at 03:33:08 UTC, Vitaliy Fadeev wrote: [...] What I mean is, why not use other language constructs like mixins or inheritance with some mapping for example? Can you give an example? You're basically just describing polymorphism. I can post an example tomorrow, it's midnight here now.
Re: Help on array pointers
On Monday, 18 September 2023 at 02:49:37 UTC, vino wrote: On Sunday, 17 September 2023 at 18:28:36 UTC, Joe wrote: On Friday, 15 September 2023 at 16:55:34 UTC, Vino wrote: [...] [...] char[] invalid = (cast(char*)malloc(char.sizeof * len))[0..len]; This is not the way to go about it. You are mixing "pointer arrays" with "arrays". [...] Thank you very much, I am still newbie for programming and currently concentrating on Arrays/Struct/Pointers/Memory management. It will make more sense over time. It's actually really simple but it takes time to get used to thinking along the lines of the computer languages and such. My suggestion is simply do a lot of examples. Just type them in. As you type you think about what you are typing and how it relates. Then you will start to notice the patterns and such. I wouldn't try to create new programs from scratch because it's easy to make mistakes that can be very hard to debug. D might not be the best language to start learning programming. E.g., might be better to go with javascript, lua, or python and get a nice rapid development environment. Even though D is fast to compile it's not that fast. Maybe use Repl which lets you code line by line essentially. All languages more or less are the same but just look different so it you want to find out one that has the most convenience. you don't want to bite off too much as it will slow you down. Ultimately it's just like anything else though in that you are learning a language and all it's nuances. You ultimately have to learn how to translate your thoughts and desires in to the correct syntax so you can make the language useful and act as a tool. If you try to us it as a tool before you learn to mimic others it will be very slow and frustrating. Because most languages are 99% the same with just different syntax(just about every major programming language can do what ever other one can, it's just some are better/easier at doing certain things than others). You might try learning several languages at once as it can actually speed things up. Learn how to write simple programs(hello worlds) then modify those to do more and then learn control structures(branching(if's), loops(for)), variables, etc. Build up slowly and everything will make sense. Build up fast and you'll get lost at some point and then spend a lot of time trying to figure out how to make sense of it. Ideally you want an ide and language that gives you precise info on errors that you make else it can be hard to track down mistakes because sometimes errors can be hidden. D is not good for. Even Q-basis might be better to start with or try to find Turbo Pascal if it still exists. What makes D great is it's generic/template programming and that is a more complex topic that you probably won't get much in to for a few years(although it is not really hard it will seem like it at first). D has a lot of features that hard core programmers like and so you are not going to be using those features for a while so it might not be worth using D to learn off of. Not saying you can't do it but it might be slow. You could always do it in parallel. E.g., learn to write simple programs in many languages then build up. E.g., for i=1:10 // Matlab for(auto i = 1; i < 10; i++) // D for(int i = 1; i < 10; i++) // C for(var i = 1; i < 10; i++) // JS for i from 1 to 10 // maple loop(50, ...) // JSFX for i = 1,10 // lua etc. The underlying idea is that one wants a way to do the same thing over and over and so loops exist to handle that pattern. Each language uses different ways to express a loop. Just like languages, all languages uses nouns, verbs, etc but express them differently.
Re: odd bit optimized function
On Friday, 22 September 2023 at 14:29:08 UTC, Vitaliy Fadeev wrote: x86 first bit check (odd check): ```asm AND EAX, EAX ; update FLAG OF // odd flag JO Label ``` Is there D-Lang operator of optimized library function ? There's no "odd" flag, theres the overflow flag "O", and "JO" is jump on overflow. Or there's the parity flag "P", which signals an odd or even number of bits in the lowest byte of the result (on x86 anyway). If you just want to test if an int is odd or even, just do if (x & 1) writeln("is odd"); else writeln("is even"); that'll compile to an AND and a jmp, or conditional move maybe. If you want to access the parity flag I think you're out of luck aside from using inline asm.
Re: odd bit optimized function
On Friday, 22 September 2023 at 22:48:34 UTC, claptrap wrote: On Friday, 22 September 2023 at 14:29:08 UTC, Vitaliy Fadeev wrote: x86 first bit check (odd check): ```asm AND EAX, EAX ; update FLAG OF // odd flag JO Label ``` Is there D-Lang operator of optimized library function ? There's no "odd" flag, theres the overflow flag "O", and "JO" is jump on overflow. Or there's the parity flag "P", which signals an odd or even number of bits in the lowest byte of the result (on x86 anyway). If you just want to test if an int is odd or even, just do if (x & 1) writeln("is odd"); else writeln("is even"); that'll compile to an AND and a jmp, or conditional move maybe. If you want to access the parity flag I think you're out of luck aside from using inline asm. Thank, claptrap! Yes, P FLAG, of course! JP instruction. ```asm if (x & 1) ``` I am using the code above. using inline asm OK, may be.
Re: change object class
On Friday, 22 September 2023 at 19:50:17 UTC, Christian Köstlin wrote: another option could be to model your own VTable in a struct like this: https://run.dlang.io/is/3LTjP5 Kind regards, Christian Thank, Christian ! True nice tasty solution with ```VTable```! And further... the project is growing. ``` void Draw() { DrawBG(); DrawFG(); } ``` And we want use ```DrawBG()``` code from ```initial``` in other states, like ```Selected```. How to use some functions from ```initial``` via ```VTable``` ? I see solution in ```classes``` and methods with ```override``` keyword. ```VTable``` does the same thing as ```__vptr``` ?
Re: change object class
On Friday, 22 September 2023 at 19:50:17 UTC, Christian Köstlin wrote: On 17.09.23 17:05, Vitaliy Fadeev wrote: Hi! You could model it oop style like this: https://run.dlang.io/is/MJb5Fk This solution might not be to your taste, as it involves interfaces, and classes and objects and garbage (all the news) ... another option could be to model your own VTable in a struct like this: https://run.dlang.io/is/3LTjP5 Kind regards, Christian ```Behavior``` is beautiful code! But it contains a second ```new``` when ```Chip``` is created. One ```new``` is possible? Christian, really nice code! Does ```__vptr``` do the same thing ?
Re: change object class
On Friday, 22 September 2023 at 21:37:37 UTC, Imperatorn wrote: On Friday, 22 September 2023 at 14:03:40 UTC, Vitaliy Fadeev wrote: On Friday, 22 September 2023 at 12:53:28 UTC, Imperatorn wrote: You're basically just describing polymorphism. I can post an example tomorrow, it's midnight here now. Thank you. Of course! It's interesting to look at the solutions to choose the best one.
At D-Conf, Mike Shah's students presented a project. Is it in GitHub? Cant find it.
Wanted to study code. I watched the video talk. But i couldn't see any URL etc.. Believe it was called Draw.
Re: change object class
On 23.09.23 05:25, Vitaliy Fadeev wrote: On Friday, 22 September 2023 at 19:50:17 UTC, Christian Köstlin wrote: On 17.09.23 17:05, Vitaliy Fadeev wrote: Hi! You could model it oop style like this: https://run.dlang.io/is/MJb5Fk This solution might not be to your taste, as it involves interfaces, and classes and objects and garbage (all the news) ... another option could be to model your own VTable in a struct like this: https://run.dlang.io/is/3LTjP5 Kind regards, Christian ```Behavior``` is beautiful code! But it contains a second ```new``` when ```Chip``` is created. One ```new``` is possible? Christian, really nice code! Here a solution with less `new`s: https://run.dlang.io/is/iV1qVq. It really depends on the program that you are doing if creating those news is a problem. Kind regards, Christian