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] = s[i];
return r;
}
```
That is about 500% not what I meant. At all. Original code in
question:
- duplicates string unconditionally as mutable storage
- uses said mutable storage to gather all non-semicolons
- duplicates said mutable storage (again) as immutable
I suggested to make the second duplicate conditional, based on
amount of space freed by skipping semicolons.
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.
Scanning short strings twice is not all that expensive as they
will stay in the CPU cache > when you run over them a second
time.
```d
import std.stdio;
@safe:
string stripsemicolons(string s) @trusted {
int i,n;
foreach(c; s) n += c != ';'; // premature optimization
auto r = new char[](n);
foreach(c; s) if (c != ';') r[i++] = c;
return cast(string)r;
}
```
Again, that is a different algorithm than what I was responding
to. But sure, short strings - might as well. So long as you do
track the distinction somewhere up in the code and don't simply
call this on all strings.