Fast removal of character

Johan Engelen j at j.nl
Wed Oct 11 23:06:13 UTC 2017


On Wednesday, 11 October 2017 at 22:45:14 UTC, Jonathan M Davis 
wrote:
> On Wednesday, October 11, 2017 22:22:43 Johan Engelen via 
> Digitalmars-d- learn wrote:
>> std.string.removechars is now deprecated. 
>> https://dlang.org/changelog/2.075.0.html#pattern-deprecate
>>
>> What is now the most efficient way to remove characters from a 
>> string, if only one type of character needs to be removed?
>>
>> ```
>> // old
>> auto old(string s) {
>>      return s.removechars(",").to!int;
>> }
>>
>> // new?
>> auto newnew(string s) {
>>      return s.filter!(a => a != ',').to!int;
>> }
>> ```
>
> Well, in general, I'd guess that the fastest way to remove all 
> instances of a character from a string would be 
> std.array.replace with the replacement being the empty string,

Is that optimized for empty replacement?

> but if you're feeding it to std.conv.to rather than really 
> using the resultant string, then filter probably is faster, 
> because it won't allocate. Really though, you'd have to test 
> for your use case and see how fast a given solution is.

Yeah :(

I am disappointed to see functions being deprecated, without an 
extensive documentation of how to rewrite them for different 
usage of the deprecated function. It makes me feel that no deep 
thought went into removing them (perhaps there was, I can't tell).

One has to go and browse through the different version _release 
notes_ to find any documentation on how to rewrite them. It would 
have been much better to add it (aswell) to the deprecated 
function documentation.

I have the same problem for std.string.squeeze. The release notes 
only say how to rewrite the `squeeze()` case, but not the 
`squeeze("_")` use case. I guess `uniq!("a=='_' && a == b")` ? 
Great improvement?

- Johan



More information about the Digitalmars-d-learn mailing list