Re: How to loop through characters of a string in D language?

2021-12-23 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 23 December 2021 at 16:13:49 UTC, Stanislav Blinov wrote: You're comparing apples and oranges. When benchmarking, at least look at the generated assembly first. I looked now and you're right. Insomuch that it should be eggplant not apple, banana not orange...:) Because it's an

Re: How to loop through characters of a string in D language?

2021-12-23 Thread rumbu via Digitalmars-d-learn
On Thursday, 23 December 2021 at 07:14:35 UTC, Salih Dincer wrote: It seems faster than algorithms in Phobos. We would love to see this in our new Phobos. Replace: 436 msecs Malloc : 259 msecs */ It seems because MallocReplace is cheating a lot: - it is not called through another function

Re: How to loop through characters of a string in D language?

2021-12-23 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 23 December 2021 at 07:14:35 UTC, Salih Dincer wrote: It seems faster than algorithms in Phobos. We would love to see this in our new Phobos. ```d void mallocReplace() void normalReplace() string result = str.replace(';',""); }/* Console Out: Replace: 436 msecs Malloc

Re: How to loop through characters of a string in D language?

2021-12-22 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:36:57 UTC, Ola Fosheim Grøstad wrote: ```d @safe: string prematureoptimizations(string s, char stripchar) @trusted { import core.memory; immutable uint flags = GC.BlkAttr.NO_SCAN|GC.BlkAttr.APPENDABLE; char* begin =

Re: How to loop through characters of a string in D language?

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Threshold could be relative for short strings and absolute for long ones. Makes little sense reallocating if you only waste a couple bytes, but makes perfect sense if you've just removed pages and pages of semicolons ;)

Re: How to loop through characters of a string in D language?

2021-12-12 Thread bauss via Digitalmars-d-learn
On Monday, 13 December 2021 at 05:46:06 UTC, forkit wrote: On Saturday, 11 December 2021 at 09:25:37 UTC, Ola Fosheim Grøstad wrote: ```putchar(…)``` is too slow! On planet Mars maybe, but here on earth, my computer can do about 4 billion ticks per second, and my entire program (using

Re: How to loop through characters of a string in D language?

2021-12-12 Thread forkit via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:25:37 UTC, Ola Fosheim Grøstad wrote: ```putchar(…)``` is too slow! On planet Mars maybe, but here on earth, my computer can do about 4 billion ticks per second, and my entire program (using putchar) takes only 3084 ticks.

Re: How to loop through characters of a string in D language?

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
Of course, since it is easy to mess up and use ranges in the wrong way, you might want to add ```assert```s. That is most likely *helpful* to newbies that might want to use your kickass library function: ``` auto helpfuldeatheater(char stripchar)(string str) { struct voldemort {

Re: How to loop through characters of a string in D language?

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Sunday, 12 December 2021 at 08:58:29 UTC, Ola Fosheim Grøstad wrote: this(string s)@trusted{ begin = s.ptr; end = s.ptr + s.length; } } Bug, it fails if the string ends or starts with ';'. Fix: ``` this(string s)@trusted{

Re: How to loop through characters of a string in D language?

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 19:50:55 UTC, russhy wrote: you need to import a 8k lines of code module that itself imports other modules, and then the code is hard to read I agree. ``` @safe: auto deatheater(char stripchar)(string str) { struct voldemort { immutable(char)*

Re: How to loop through characters of a string in D language?

2021-12-11 Thread russhy via Digitalmars-d-learn
On Saturday, 11 December 2021 at 18:51:12 UTC, Rumbu wrote: On Saturday, 11 December 2021 at 14:42:53 UTC, russhy wrote: Here is mine - 0 allocations - configurable - let's you use it how you wish - fast You know that this is already in phobos? ``` "abc;def;ghi".splitter(';').joiner

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Rumbu via Digitalmars-d-learn
On Saturday, 11 December 2021 at 14:42:53 UTC, russhy wrote: Here is mine - 0 allocations - configurable - let's you use it how you wish - fast You know that this is already in phobos? ``` "abc;def;ghi".splitter(';').joiner ```

Re: How to loop through characters of a string in D language?

2021-12-11 Thread russhy via Digitalmars-d-learn
Here is mine - 0 allocations - configurable - let's you use it how you wish - fast ```D import std; void main() { string a = "abc;def;ab"; writeln("a => ", a); foreach(item; split(a, ';')) writeln("\t", item); string b = "abc;def ;ab"; writeln("a => ",

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:40:47 UTC, Stanislav Blinov wrote: On Saturday, 11 December 2021 at 09:34:17 UTC, Ola Fosheim Grøstad wrote: void donttrythisathome(string s, char stripchar) @trusted { import core.stdc.stdlib; char* begin = cast(char*)alloca(s.length); A

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:34:17 UTC, Ola Fosheim Grøstad wrote: void donttrythisathome(string s, char stripchar) @trusted { import core.stdc.stdlib; char* begin = cast(char*)alloca(s.length); A function with that name, and calling alloca to boot, cannot be @trusted ;)

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:34:17 UTC, Ola Fosheim Grøstad wrote: @system Shouldn't be there. Residual leftovers… (I don't want to confuse newbies!)

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 08:46:32 UTC, forkit wrote: On Saturday, 11 December 2021 at 08:05:01 UTC, Ola Fosheim Grøstad wrote: Using libraries can trigger hidden allocations. ok. fine. no unnecessary, hidden allocations then. // -- module test; import

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:26:06 UTC, Stanislav Blinov wrote: What you're showing is... indeed, don't do this, but I fail to see what that has to do with my suggestion, or the original code. You worry too much, just have fun with differing ways of expressing the same thing.

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 10 December 2021 at 23:53:47 UTC, Ola Fosheim Grøstad wrote: ```d char[] dontdothis(string s, int i=0, int skip=0){ if (s.length == i) return new char[](i - skip); if (s[i] == ';') return dontdothis(s, i+1, skip+1); auto r = dontdothis(s, i+1, skip); r[i-skip] =

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 08:46:32 UTC, forkit wrote: On Saturday, 11 December 2021 at 08:05:01 UTC, Ola Fosheim Grøstad wrote: Using libraries can trigger hidden allocations. ok. fine. no unnecessary, hidden allocations then. // -- module test; import

Re: How to loop through characters of a string in D language?

2021-12-11 Thread forkit via Digitalmars-d-learn
On Saturday, 11 December 2021 at 08:05:01 UTC, Ola Fosheim Grøstad wrote: Using libraries can trigger hidden allocations. ok. fine. no unnecessary, hidden allocations then. // -- module test; import core.stdc.stdio : putchar; nothrow @nogc void main() { string str =

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Threshold could be relative for short strings and absolute for long ones. Makes little sense reallocating if you only waste a couple bytes, but makes perfect sense if you've just removed pages and pages of semicolons ;)

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 00:39:15 UTC, forkit wrote: On Friday, 10 December 2021 at 22:35:58 UTC, Arjan wrote: "abc;def;ghi".tr(";", "", "d" ); I don't think we have enough ways of doing the same thing yet... so here's one more.. "abc;def;ghi".substitute(";", ""); Using

Re: How to loop through characters of a string in D language?

2021-12-10 Thread forkit via Digitalmars-d-learn
On Friday, 10 December 2021 at 22:35:58 UTC, Arjan wrote: "abc;def;ghi".tr(";", "", "d" ); I don't think we have enough ways of doing the same thing yet... so here's one more.. "abc;def;ghi".substitute(";", "");

Re: How to loop through characters of a string in D language?

2021-12-10 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Oooh, finally someone suggested to preallocate storage for all these reinventions of the wheel :D ``` import std.stdio; char[] dontdothis(string s, int i=0, int skip=0){ if (s.length == i) return new char[](i - skip);

Re: How to loop through characters of a string in D language?

2021-12-10 Thread Arjan via Digitalmars-d-learn
On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The character I want to skip: `;` Expected result: ``` abcdefab ``` Since it seems there is a contest here: ```d

Re: How to loop through characters of a string in D language?

2021-12-10 Thread forkit via Digitalmars-d-learn
On Friday, 10 December 2021 at 12:15:18 UTC, Rumbu wrote: I thought it's a beauty contest. Well, if it's a beauty contest, then i got a beauty.. char[("abc;def;ab".length - count("abc;def;ab", ";"))] b = "abc;def;ab".replace(";", "");

Re: How to loop through characters of a string in D language?

2021-12-10 Thread Luís Ferreira via Digitalmars-d-learn
Yes it will. You can use lazy templates instead, like splitter and joiner, which splits and joins lazily, respectively. LDC can optimize those templates fairly well and avoid too much lazy calls and pretty much constructs the logic equivalent to for loop. On 10 December 2021 11:06:21 WET,

Re: How to loop through characters of a string in D language?

2021-12-10 Thread Rumbu via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Be interesting to see if this thread does evolve into a SIMD http://lemire.me/blog/2017/01/20/how-quickly-can-you-remove-spaces-from-a-string/

Re: How to loop through characters of a string in D language?

2021-12-10 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 10 December 2021 at 13:22:58 UTC, Matheus wrote: My C way of thinking while using D: import std; string stripsemicolons(string input){ char[] s = input.dup; int j=0; for(int i=0;i Oooh, finally someone suggested to preallocate storage for all these reinventions of

Re: How to loop through characters of a string in D language?

2021-12-10 Thread Matheus via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: ... The character I want to skip: `;` My C way of thinking while using D: import std; string stripsemicolons(string input){ char[] s = input.dup; int j=0; for(int i=0;i

Re: How to loop through characters of a string in D language?

2021-12-10 Thread Rumbu via Digitalmars-d-learn
On Friday, 10 December 2021 at 11:06:21 UTC, IGotD- wrote: On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote: Since it seems there is a contest here: ```d "abc;def;ghi".split(';').join(); ``` :) Would that become two for loops or not? I thought it's a beauty contest. ```d string

Re: How to loop through characters of a string in D language?

2021-12-10 Thread IGotD- via Digitalmars-d-learn
On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote: Since it seems there is a contest here: ```d "abc;def;ghi".split(';').join(); ``` :) Would that become two for loops or not?

Re: How to loop through characters of a string in D language?

2021-12-09 Thread Rumbu via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The character I want to skip: `;` Expected result: ``` abcdefab ``` Since it seems there is a contest here: ```d "abc;def;ghi".split(';').join(); ``` :)

Re: How to loop through characters of a string in D language?

2021-12-09 Thread forkit via Digitalmars-d-learn
On Thursday, 9 December 2021 at 18:00:42 UTC, kdevel wrote: PRO: - saves two lines of boilerplate code CONS: - raw loop - postinc ++ is only permitted in ++C - inconsistent spacing around "=" - mixing tabs and spaces for indentation - arrow code more PROs: - You become less dependent on

Re: How to loop through characters of a string in D language?

2021-12-09 Thread kdevel via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote: [...] string b = a.replace(";", ""); 

Re: How to loop through characters of a string in D language?

2021-12-09 Thread kdevel via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 13:01:32 UTC, BoQsc wrote: [...] I'm not getting used to the syntax and that leads to poor readability. It depends on what you expect when you read source code. I don't want to read how seats in the memory are assigned to bits and bytes. Instead I want to

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The character I want to skip: `;` Expected result: ``` abcdefab

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:55:02 UTC, forkit wrote: On Wednesday, 8 December 2021 at 22:35:35 UTC, Stanislav Blinov wrote: You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:35:35 UTC, Stanislav Blinov wrote: You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug code. but this will change nothing. the compilation cost of

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:18:23 UTC, forkit wrote: It's also worth noting the differences in compiler output, as well as the time taken to compile, these two approaches: (1) string str = "abc;def;ab".filter!(c => c != ';').to!string; (2) string str = "abc;def;ab".replace(";", "");

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 14:27:22 UTC, BoQsc wrote: On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import

Re: How to loop through characters of a string in D language?

2021-12-08 Thread BoQsc via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The

Re: How to loop through characters of a string in D language?

2021-12-08 Thread bauss via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The character I want to skip: `;` Expected result: ``` abcdefab

Re: How to loop through characters of a string in D language?

2021-12-08 Thread BoQsc via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 12:49:39 UTC, Adam D Ruppe wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: The string example to loop/iterate: foreach(ch; a) { } does the individual chars of the string you can also foreach(dchar ch; a) { } to decode the utf 8 Thanks

Re: How to loop through characters of a string in D language?

2021-12-08 Thread BoQsc via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:35:39 UTC, Biotronic wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ```

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: The string example to loop/iterate: foreach(ch; a) { } does the individual chars of the string you can also foreach(dchar ch; a) { } to decode the utf 8

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Biotronic via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The character I want to skip: `;` Expected result: ``` abcdefab

How to loop through characters of a string in D language?

2021-12-08 Thread BoQsc via Digitalmars-d-learn
Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The character I want to skip: `;` Expected result: ``` abcdefab ```