Re: line terminators
On Wednesday, 28 September 2022 at 21:17:16 UTC, rassoc wrote: On 9/28/22 21:36, NonNull via Digitalmars-d-learn wrote: [...] If you have structured data, you can use byRecord [1] to read the important parts right into a tuple. [...] Thanks --- very helpful.
Re: line terminators
On 9/28/22 21:36, NonNull via Digitalmars-d-learn wrote: If I want to read a text file line by line, treating any one of these things as a newline, there would seem to be no canned way to do that in std.stdio . 1.) What is the best way to achieve this result in D? If you have structured data, you can use byRecord [1] to read the important parts right into a tuple. Should it be unstructured data, then there's lineSplitter [2] which handles all of the above newline specifics, I think. Sadly, it's working on text and not on files. So, for small files you could just read the whole file via readText [3] and process it via lineSplitter. If the file is rather large, then there's the option to use memory-mapped files instead: ```d import std; void main() { scope mmfile = new MmFile("largefile.txt"); auto data = cast(string) mmfile[]; foreach (line; data.lineSplitter) { // process line, os handles memory-mapped file buffering } } ``` Hope that helps. [1] https://dlang.org/library/std/stdio/file.by_record.html [2] https://dlang.org/library/std/string/line_splitter.html [3] https://dlang.org/library/std/file/read_text.html
Re: Interfacing with basic C++ class
On 9/28/22 12:57, Riccardo M wrote: > class MyClass { > public: > int field; > MyClass(int a) : field(a) {} Make the following function 'virtual': > int add(int asd) { virtual int add(int asd) { I think the C++ class does not get a vptr without a virtual function and apparently D expects this. > final int add(int asd); You don't need that 'final' anymore. > This is a very simplified version of the dlang official example of > interfacing with C++ classes. The example there uses virtual functions. That must be the difference. > I need to declare every class method as > "final" otherwise I get undefined references during linking. Is this > expected behaviour? Apparently, not with that added 'virtual'. Ali
Interfacing with basic C++ class
I think I am stuck in the easiest of the issues and yet it seems I cannot get around this. I have a C++ file: ``` class MyClass { public: int field; MyClass(int a) : field(a) {} int add(int asd) { return asd + 1; } }; MyClass* instantiate(int asd) { return new MyClass(asd); } ``` and a D file: ``` extern(C++) { class MyClass { public: int field; @disable this(); final int add(int asd); } MyClass instantiate(int asd); } void main() { import std : writeln; auto myclass = instantiate(100); writeln(myclass.field); } ``` This is a very simplified version of the dlang official example of interfacing with C++ classes. When I compile, I can't correctly read 'field'. What I do: ``` g++ -c cpp.cpp dmd app.d cpp.o -L-lstdc++ ./app ``` What I get: ``` 0 ``` Instead of the expected 100. Care to help me understand what am I doing wrong? Side question: it seems that I need to declare every class method as "final" otherwise I get undefined references during linking. Is this expected behaviour? Thanks
line terminators
Hello, I notice that readln from std.stdio has '\n' as the default line terminator. What about multiple line terminators in UTF-8 being used in one input file, such as '\n', NEL, LS, PS? And in Windows "\r\n" is a line terminator, and what if NEL, LS, PS exist in a Windows UTF-8 text file as well? The following explains the details when reading Unicode. https://en.wikipedia.org/wiki/Newline#Unicode If I want to read a text file line by line, treating any one of these things as a newline, there would seem to be no canned way to do that in std.stdio . 1.) What is the best way to achieve this result in D? It is convenient to read text files of unknown origin in this fashion. Additionally discarding the newlines however they are represented is convenient. 2.) What about reading UTF-16LE text files line by line (e.g. from Windows, with a BOM)?
Re: Template function alias that leaves out the last type parameter
On 9/28/22 18:47, torhu via Digitalmars-d-learn wrote: It works like writefln, so that example would not compile. I forgot to make the format string explicit, I probably should have done that. ``` private template _messageBox(string title, int style) { void _messageBox(T...)(T args) { messageBox(format(args), title, style); } } ``` If you want to pass a message, why bother with varargs? Wouldn't it be simpler to just accept a formatted string? ```d import std; void messageBox(string title, int style)(string msg) { guiMessageBox(msg, title, style); } alias info = messageBox!("Info", 4); alias warn = messageBox!("Warning", 4); alias err = messageBox!("Error", 4); void main() { info("ok ok"); warn(5.iota.format!"%(%s, %)"); } ```
Re: Template function alias that leaves out the last type parameter
On Wednesday, 28 September 2022 at 12:43:52 UTC, rassoc wrote: On 9/28/22 02:04, torhu via Digitalmars-d-learn wrote: Thank you, works like a charm! It does? How are you formatting `info("foo", 'a', 42)` inside the template if I may ask? It works like writefln, so that example would not compile. I forgot to make the format string explicit, I probably should have done that. ``` private template _messageBox(string title, int style) { void _messageBox(T...)(T args) { messageBox(format(args), title, style); } } ```
Re: Template function alias that leaves out the last type parameter
On 9/28/22 02:04, torhu via Digitalmars-d-learn wrote: Thank you, works like a charm! It does? How are you formatting `info("foo", 'a', 42)` inside the template if I may ask?
Re: importC and cmake
On Wednesday, 28 September 2022 at 05:29:41 UTC, Chris Piker wrote: `Xmake` is indeed simpler. `Xmake` is really nice!