String convention

Niklas Ulvinge Niklas_member at pathlink.com
Sat Jul 1 14:07:24 PDT 2006


In article <e86l86$1364$1 at digitaldaemon.com>, Kirk McDonald says...
>
>Niklas Ulvinge wrote:
>> I could't find any info about it so I'm asking here...
>> 
>> I just looked at D and it sounds rather interesting.
>> 
>> Now to my Q:
>> Strings in D starts with some data that is defining the length of the string.
>> Why did they decide to use this aproach?
>> What is this 'data' at the beginning of the string?
>> 
>
>This is an implementation detail, and shouldn't matter to your code.
>
>> This has a limitation, strings can't be longer than 'data' allows.
>> Is there a way around this?
>> 
>
>I think you are somewhat confused. Strings in D are dynamic arrays of 
>type char. They may be of any length, so long as you have enough RAM.
>
>http://www.digitalmars.com/d/arrays.html
>
>> 
>> An idea, I got when I wrote a dynamic array (in C), was to use s[-1] as the size
>> for array s (and s[-2] for capacity, but that isn't necesary here...).
>> 
>> Couldn't this be used with strings?
>> Then this would work:
>> string s = "IDK\0";
>
>The D syntax is:
>
>char[] s = "IDK";
>
>The \0 is not needed as strings in D are not null-terminated. The length 
>of the string may be retrieved with "s.length".
>

Thanks for clearing things up a bit.

First, if the data don't have a terminator, then they can't have a big(infinity)
size.
This is becouse, s.length need to be a variable. And that variable has got to
have a size, wich makes it impossible to make big arrays.

I only need to rewrite my Q's...
What type is s.length(or for that matter any dynamic array's size)?
Or is dynamic arrays implemented in a different way?

Niklas Ulvinge
aka IDK wishes
everyone happy
programming!!!



More information about the Digitalmars-d mailing list