Thread overview
String view
Jan 21, 2007
NN
Jan 21, 2007
Lionello Lunesu
Jan 22, 2007
Daniel Keep
Jan 23, 2007
Luís Marques
Jan 24, 2007
Daniel Keep
January 21, 2007
Why there is no string view in D ?

Assume I have pointer in memory pointing to zero ending string: char* p;

I want to create a string from it but i do not want to copy it.
Assume I have a special class for this:
StringView s(p);

But what would I do if function receives char[] ? There will be a copy. What if I do not want a copy ?

Thanx.
January 21, 2007
char* p = ....;
char[] stringview = p[0..strlen(p)];
assert( stringview.ptr is p );

"NN" <nn-mail@bk.ru> wrote in message news:ep0ic7$1a9c$1@digitaldaemon.com...
> Why there is no string view in D ?
>
> Assume I have pointer in memory pointing to zero ending string: char* p;
>
> I want to create a string from it but i do not want to copy it.
> Assume I have a special class for this:
> StringView s(p);
>
> But what would I do if function receives char[] ? There will be a copy. What if I do not want a copy ?
>
> Thanx.


January 22, 2007
Lionello Lunesu wrote:
> char* p = ....;
> char[] stringview = p[0..strlen(p)];
> assert( stringview.ptr is p );
> 
> "NN" <nn-mail@bk.ru> wrote in message news:ep0ic7$1a9c$1@digitaldaemon.com...
> 
>>Why there is no string view in D ?
>>
>>Assume I have pointer in memory pointing to zero ending string:
>>char* p;
>>
>>I want to create a string from it but i do not want to copy it.
>>Assume I have a special class for this:
>>StringView s(p);
>>
>>But what would I do if function receives char[] ? There will be a copy.
>>What if I do not want a copy ?
>>
>>Thanx. 

A more in-depth explanation:

D has "slices" which are a combination of a pointer and a length (number of elements).  Slices are effectively the same thing as arrays (they work in precisely the same way).

You can take a slice of an array, *or* a pointer using the

  ptr[first..(last+1)]

syntax.  The reason you use the last index you want to slice + 1 is that this allows things like

  ptr[0..0]

for an empty slice and

  arr[0..$]

which is a slice over an entire array (where "$" is the array's length).

So, in your example, to convert a null-terminated C string into a D array (WITHOUT copying), you would use the code Lionello posted.  Just note that by doing so, you strip off the trailing NULL (D doesn't use trailing NULLs since all arrays know their own bounds).

	-- Daniel
January 23, 2007
Daniel Keep wrote:
> Slices are effectively the same thing as arrays (they work in precisely the same way).

Maybe I'm nitpicking but it might be better to refer to slices as an operation, returning a new array reference, instead of comparing them to arrays directly.

--
Luís
January 24, 2007
Luís Marques wrote:
> Daniel Keep wrote:
> 
>> Slices are effectively the same thing as arrays (they work in precisely the same way).
> 
> 
> Maybe I'm nitpicking but it might be better to refer to slices as an operation, returning a new array reference, instead of comparing them to arrays directly.
> 
> -- 
> Luís

I could be completely wrong, but this is how I've always looked at it:

slice, n: a section of memory denoted by a starting address and a number of elements.

slice, v: the operation of creating a new slice given a starting address and a number of elements from a section of memory.

So "slices" is the plural of the noun "slice", not the verb "slice" :P

I think.  Maybe.

  -- Daniel