String convention
Derek Parnell
derek at psych.ward
Sat Jul 1 14:13:30 PDT 2006
On Sun, 02 Jul 2006 06:07:30 +1000, Niklas Ulvinge
<Niklas_member at pathlink.com> 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.
No, that is a misunderstanding. Strings in D are a variable-length array
of characters, and variable-length arrays consist of two data items. The
first is the array reference and this is a pseudo-struct with two members
: the length (uint of 32-bits) and a pointer to the first array element
(void *), the second data item is the array data itself which is a
contiguous block of RAM that will hold at least the number of elements
specified in the 'length' member.
But you as a coder don't need to worry about this because the compiler
handles all the manipulation for you.
> Why did they decide to use this aproach?
Because it makes for very fast an flexible dynamic arrays. Slices become
easy to implement and fast.
> What is this 'data' at the beginning of the string?
There is no data at the beginning of the string data. There is a separate
array reference though.
> This has a limitation, strings can't be longer than 'data' allows.
Currently, utf8 strings are limited to 4Gigabytes. This might change on
64-bit architectures. But if you are dealing with strings that big you
probably need to rethink you algorithms anyhow ;-)
> Is there a way around this?
Solve the problem when you get to it. Are you actually running into
limitations already?
> 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...).
That's right, it isn't.
> Couldn't this be used with strings?
> Then this would work:
> string s = "IDK\0";
> printf("%s",s);
Do not use the C function 'printf'. Use the D function 'writef' and your
formatting issues will disappear.
alias string char[];
string s = "IDK";
writef("%s", s);
--
Derek Parnell
Melbourne, Australia
More information about the Digitalmars-d
mailing list