digitalmars.D - String view
- NN (9/9) Jan 21 2007 Why there is no string view in D ?
- Lionello Lunesu (5/14) Jan 21 2007 char* p = ....;
- Daniel Keep (18/38) Jan 21 2007 A more in-depth explanation:
- =?ISO-8859-1?Q?Lu=EDs_Marques?= (6/8) Jan 23 2007 Maybe I'm nitpicking but it might be better to refer to slices as an
- Daniel Keep (9/21) Jan 24 2007 I could be completely wrong, but this is how I've always looked at it:
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.
Jan 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.
Jan 21 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...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). -- DanielWhy 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.
Jan 21 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
Jan 23 2007
Luís Marques wrote:Daniel Keep wrote: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. -- DanielSlices 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
Jan 24 2007