Jump to page: 1 2 3
Thread overview
what exactly is string length?
Apr 02, 2021
mw
Apr 02, 2021
rikki cattermole
Apr 02, 2021
mw
Apr 02, 2021
rikki cattermole
Apr 02, 2021
mw
Apr 02, 2021
mw
Apr 02, 2021
rikki cattermole
Apr 02, 2021
mw
Apr 02, 2021
H. S. Teoh
Apr 02, 2021
mw
Apr 02, 2021
Paul Backus
Apr 02, 2021
H. S. Teoh
Apr 02, 2021
Computermatronic
Apr 02, 2021
mw
Apr 02, 2021
Computermatronic
Apr 02, 2021
rikki cattermole
Apr 02, 2021
mw
Apr 02, 2021
Berni44
Apr 02, 2021
mw
Apr 02, 2021
H. S. Teoh
April 02, 2021

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.

April 02, 2021
On 02/04/2021 5:32 PM, mw wrote:
> ---
> 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?
assert(t[9] == '\0');
> }
> ---

April 02, 2021
On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote:
> On 02/04/2021 5:32 PM, mw wrote:
>> ---
>> 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?
> assert(t[9] == '\0');
>> }
>> ---

I don't get it, what do you mean by the assertion:


assert(t[9] == '\0');


t == "head-abc-tail"



April 02, 2021
On 02/04/2021 5:38 PM, mw wrote:
> On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote:
>> On 02/04/2021 5:32 PM, mw wrote:
>>> ---
>>> 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?
>> assert(t[9] == '\0');
>>> }
>>> ---
> 
> I don't get it, what do you mean by the assertion:
> 
> 
> assert(t[9] == '\0');
> 
> 
> t == "head-abc-tail"

Not all characters can be printed such as NULL.

[104, 101, 97, 100, 45, 97, 98, 99, 0, 0, 0, 45, 116, 97, 105, 108]

April 02, 2021

On Friday, 2 April 2021 at 04:38:37 UTC, mw wrote:

>

On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote:
I don't get it, what do you mean by the assertion:

assert(t[9] == '\0');

t == "head-abc-tail"

Just tried this:

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

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 16 instead of 13, t's type is string here
    assert(t[9] == '\0');   // ok
    assert(t == "head-abc-tail");  // failed!
}

I'm even more puzzled by the last 2 assertions behavior: t is print out as "head-abc-tail".

April 02, 2021

On Friday, 2 April 2021 at 04:43:48 UTC, rikki cattermole wrote:

>

On 02/04/2021 5:38 PM, mw wrote:

>

On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote:

>

On 02/04/2021 5:32 PM, mw wrote:

>

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?
assert(t[9] == '\0');
}

I don't get it, what do you mean by the assertion:

assert(t[9] == '\0');

t == "head-abc-tail"

Not all characters can be printed such as NULL.

[104, 101, 97, 100, 45, 97, 98, 99, 0, 0, 0, 45, 116, 97, 105, 108]

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?

April 02, 2021

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

>

On Friday, 2 April 2021 at 04:43:48 UTC, rikki cattermole wrote:

>

On 02/04/2021 5:38 PM, mw wrote:

>

On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote:

>

On 02/04/2021 5:32 PM, mw wrote:

>

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?
assert(t[9] == '\0');
}

I don't get it, what do you mean by the assertion:

assert(t[9] == '\0');

t == "head-abc-tail"

Not all characters can be printed such as NULL.

[104, 101, 97, 100, 45, 97, 98, 99, 0, 0, 0, 45, 116, 97, 105, 108]

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?

Then how can I construct t? to make this assertion true:

assert(t == "head-abc-tail");  // failed!
April 02, 2021

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

April 01, 2021
On Fri, Apr 02, 2021 at 04:32:53AM +0000, mw via Digitalmars-d-learn wrote: [...]
> ---
> 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.

Because `s` contains 6 chars, and you only assigned 3 of them, so there are 3 trailing null bytes that are inserted before "-tail". Null bytes don't print anything, so you don't see them when printed as a string, but if you cast it to ubyte[], you will see them:

	writefln("%(%02X %)", t);
	// Prints: 68 65 61 64 2D 61 62 63 00 00 00 2D 74 61 69 6C

Remember, this is D, not C. Strings are not terminated by nulls, so appending the static array will append all 6 chars, including the nulls.


T

-- 
Holding a grudge is like drinking poison and hoping the other person dies. -- seen on the 'Net
April 02, 2021
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
« First   ‹ Prev
1 2 3