April 02

On Friday, 2 April 2021 at 04:54:07 UTC, Computermatronic wrote:

>

On Friday, 2 April 2021 at 04:49:22 UTC, mw wrote:

>

So you mean inside the writeln() call, the 0s are skipped?

Well, if I use string t as filename, it will try to looking for a file called:

"head-abc\0\0\0-tail" instead of just "head-abc-tail" ?

or it's platform dependent?

I would imagine that it's platform dependant, but given most platforms adhere to the C ABI, and C string are null terminated, you'd end up looking for a file called "head-abc".

Ahh, I got what I see (from writeln) is not what get string here ;-)

And I just tried:

string t = text("head-", strip(s), "-tail");

It's the same behavior.

So how can I trim the leading & trailing \0 from the static char array?

April 02
On Friday, 2 April 2021 at 05:01:27 UTC, rikki cattermole wrote:
>
> On 02/04/2021 5:51 PM, mw wrote:
>> Then how can I construct `t`? to make this assertion true:
>> 
>>     assert(t == "head-abc-tail");  // failed!
>
> Slice it.
>
> string t = text("head-", s[0 .. 3], "-tail");
>
> http://ddili.org/ders/d.en/slices.html

This is just an example, what if the exact length is not known statically, is there a functions to trim the `\0`s?


April 02

On Friday, 2 April 2021 at 05:02:52 UTC, mw wrote:

>

Ahh, I got what I see (from writeln) is not what get string here ;-)

And I just tried:

string t = text("head-", strip(s), "-tail");

It's the same behavior.

So how can I trim the leading & trailing \0 from the static char array?

strip only removes whitespace, not null characters. You'd have to do something like
d string t = cast(string)text("head-", s, "-tail").filter!`a != '\0'`().array;
I would assume there would be a better way, but I haven't been able to find a dedicated function for stripping null chars in std.

April 01
On Fri, Apr 02, 2021 at 05:05:21AM +0000, mw via Digitalmars-d-learn wrote: [...]
> This is just an example, what if the exact length is not known statically, is there a functions to trim the `\0`s?

What about `s.until('\0')`?

Example:

	auto s = "abc\0\0\0def";
	auto t = "blah" ~ s.until('\0').array ~ "boo";


T

-- 
What do you call optometrist jokes? Vitreous humor.
April 01
On Fri, Apr 02, 2021 at 05:05:21AM +0000, mw via Digitalmars-d-learn wrote: [...]
> This is just an example, what if the exact length is not known statically, is there a functions to trim the `\0`s?

Another way, if you want to avoid the extra allocation, slice the static array with .indexOf:

	s[0 .. s.indexOf('\0')]

should give you the initial segment up to the first null.


T

-- 
Questions are the beginning of intelligence, but the fear of God is the beginning of wisdom.
April 02
On 02/04/2021 6:10 PM, Computermatronic wrote:
> On Friday, 2 April 2021 at 05:02:52 UTC, mw wrote:
>> Ahh, I got what I see (from writeln) is not what get string here ;-)
>>
>> And I just tried:
>>
>> string t = text("head-", strip(s), "-tail");
>>
>> It's the same behavior.
>>
>> So how can I trim the leading & trailing `\0` from the static char array?
> 
> strip only removes whitespace, not null characters. You'd have to do something like
> ```d string t = cast(string)text("head-", s, "-tail").filter!`a != '\0'`().array;```
> I would assume there would be a better way, but I haven't been able to find a dedicated function for stripping null chars in std.

If you know it has null terminators you can use fromStringz.
But this is a case of you should store the length.
April 02

On Friday, 2 April 2021 at 04:32:53 UTC, mw wrote:

>

https://run.dlang.io/is/B4jcno


import std;
import std.conv : text;

void main()
{
char[6] s;
s = "abc";
writeln(s, s.length); // abc6, ok it's the static array's length

string t = text("head-", s, "-tail");
writeln(t, t.length);  // head-abc-tail16, why?

}

Why the last output is 16 instead of 13, t's type is string here.

Test this:
https://run.dlang.io/is/Cq4vjP

April 02
On Friday, 2 April 2021 at 05:18:49 UTC, H. S. Teoh wrote:
> On Fri, Apr 02, 2021 at 05:05:21AM +0000, mw via Digitalmars-d-learn wrote: [...]
>> This is just an example, what if the exact length is not known statically, is there a functions to trim the `\0`s?
>
> What about `s.until('\0')`?
>
> Example:
>
> 	auto s = "abc\0\0\0def";
> 	auto t = "blah" ~ s.until('\0').array ~ "boo";

Finally, I'm using:

https://run.dlang.io/is/651lT6

    string t = text("head-", s[].until('\0').array, "-tail");

It works for both s = "abc" (with \0), and "abcdef" (full 6 chars, indexOf will return -1 for bad range index).


Thank everyone who helped.
April 02

On Friday, 2 April 2021 at 05:39:26 UTC, mw wrote:

>

Finally, I'm using:

https://run.dlang.io/is/651lT6

string t = text("head-", s[].until('\0').array, "-tail");

FYI, you don't need the call to .array there--text accepts input ranges.

April 02

On Friday, 2 April 2021 at 05:02:52 UTC, mw wrote:

>

On Friday, 2 April 2021 at 04:54:07 UTC, Computermatronic wrote:

>

On Friday, 2 April 2021 at 04:49:22 UTC, mw wrote:

>

So you mean inside the writeln() call, the 0s are skipped?

Well, if I use string t as filename, it will try to looking for a file called:

"head-abc\0\0\0-tail" instead of just "head-abc-tail" ?

or it's platform dependent?

I would imagine that it's platform dependant, but given most platforms adhere to the C ABI, and C string are null terminated, you'd end up looking for a file called "head-abc".

Ahh, I got what I see (from writeln) is not what get string here ;-)

BTW, shall I log a writeln() improvement bug ?

It's really confusing, e.g as debug print or logs.

Output something like:

"head-abc\0\0\0-tail"

"head-abc...-tail"
"head-abc???-tail"

is more clear.

1 2 3