Fast removal of character

Steven Schveighoffer schveiguy at yahoo.com
Thu Oct 12 00:33:35 UTC 2017


On 10/11/17 7:06 PM, Johan Engelen wrote:
> 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.

Performance-wise, I would use neither, as both autodecode (removechars 
by using the opaque/slow foreach decoding) and reencode.

Especially if you are removing a single ascii char.

> 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.

This should have been done. A deprecation in this manner where there is 
no exact path forward is confusing and unnecessary. If we are 
deprecating a function, the person deprecating the function should have 
had a replacement in mind. The only way the message could be worse is if 
it said "please use other functions in Phobos".

-Steve


More information about the Digitalmars-d-learn mailing list