String convention

Kirk McDonald kirklin.mcdonald at gmail.com
Sat Jul 1 13:17:16 PDT 2006


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

> printf("%s",s);
> 
> Niklas Ulvinge
> aka IDK wishes
> everyone happy
> programming!!!


-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d mailing list