to delete the '\0' characters

Ali Çehreli acehreli at
Thu Sep 22 15:19:35 UTC 2022

On 9/22/22 03:53, Salih Dincer wrote:
 > Is there a more accurate way to delete the '\0' characters at the end of
 > the string? I tried functions in this module:

Just to remind, the following are always related as well because strings 
are arrays, which are ranges:


 >        r ~= c;

Stefan Koch once said the ~ operator should be called "the slow 
operator". Meaning, if you want to make your code slow, then use that 
operator. :)

The reason is, that operation may need to allocate memory from the heap 
and copy existing elements there. And any memory allocation may trigger 
a garbage collection cycle.

Of course, none of that matters if we are talking about a short string. 
However, it may become a dominating reason why a program may be slow.

I was going to suggest Paul Backus' solution as well but I may leave the 
array part out in my own code until I really need it:

string noZeroes(string s)
     return s.byCodeUnit.filter!(c => c != '\0');

Now, a caller may be happy without an array:

     auto a = s.noZeroes.take(10);

And another can easily add a .array when really needed:

     auto b = s.noZeroes.array;

That may be seen as premature optimization but I see it as avoiding a 
premature pessimization because I did not put in any extra work there. 
But again, this all depends on each program.

If we were talking about mutable elements and the order of elements did 
not matter, then the fastest option would be to remove with 

import std;

void main() {
     auto arr = [ 1, 0, 2, 0, 0, 3, 4, 5 ];
     arr = remove!(i => i == 0, SwapStrategy.unstable)(arr);

unstable works by swapping the first 0 that it finds with the last 
non-zero that it finds and continues in that way. No memory is 
allocated. As a result, the order of elements will not preserved but 
unstable can be very fast compared to .stable (which is the default) 
because .stable must move elements to the left (multiple times in some 
cases) and can be expensive especially for some types.

The result of the program above is the following:

[1, 5, 2, 4, 3]

Zeros are removed but the order is not preserved.

And very important: Don't forget to assign remove's return value back to 
'arr'. ;)

I know this will not work for a string but something to keep in mind...


More information about the Digitalmars-d-learn mailing list