to delete the '\0' characters
ag0aep6g
anonymous at example.com
Thu Sep 22 12:20:31 UTC 2022
On 22.09.22 13:14, ag0aep6g wrote:
> On 22.09.22 12:53, Salih Dincer wrote:
[...]
>> ```d
>> auto foo(string s)
>> {
>> string r;
>> foreach(c; s)
>> {
>> if(c > 0)
>> {
>> r ~= c;
>> }
>> }
>> return r;
>> }
>> ```
[...]
> Here's a snippet that's a bit shorter than yours and doesn't copy the data:
>
> while (s.length > 0 && s[$ - 1] == '\0')
> {
> s = s[0 .. $ - 1];
> }
> return s;
>
> But do you really want to allow embedded '\0's? I.e., should
> foo("foo\0bar\0") really resolve to "foo\0bar" and not "foo"?
Whoops. Your code actually turns "foo\0bar" into "foobar", removing the
embedded '\0'. So my supposed alternative is wrong.
Still, you usually want to stop at the first '\0'.
More information about the Digitalmars-d-learn
mailing list