www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C++ std::string_view equivalent in D?

reply 0xFFFFFFFF <0xFFFFFFFF mail.com> writes:
What is the equivalent of C++17 std::string_view (an object that 
can refer to a constant contiguous sequence of char-like objects 
with the first element of the sequence at position zero) in D?

PS: I'm getting back to D after years (since DMD 1 days). A lot 
changes since v1.0.
Feb 21 2018
next sibling parent Smaehtin <smaehtin invalid.com> writes:
On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object 
 that can refer to a constant contiguous sequence of char-like 
 objects with the first element of the sequence at position 
 zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot 
 changes since v1.0.
Perhaps you are looking for slices: https://dlang.org/spec/arrays.html#slicing?
Feb 21 2018
prev sibling next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 21/02/2018 9:21 AM, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object that can 
 refer to a constant contiguous sequence of char-like objects with the 
 first element of the sequence at position zero) in D?
 
 PS: I'm getting back to D after years (since DMD 1 days). A lot changes 
 since v1.0.
Think of string_view as a poor man's slice support. Slices are essentially just dynamic arrays, a length and a pointer. char* ptr = cast(char*)malloc(32); char[] array = ptr[0 .. 32]; char[] anotherArray = array[16 .. $]; assert(anotherArray.length == 16); It isn't limited to just char's either :)
Feb 21 2018
prev sibling next sibling parent Kagamin <spam here.lot> writes:
On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object 
 that can refer to a constant contiguous sequence of char-like 
 objects with the first element of the sequence at position 
 zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot 
 changes since v1.0.
Strings were always slices in D even in version 1.0, what's changed is that string data is immutable now - guaranteed to not change.
Feb 21 2018
prev sibling next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, February 21, 2018 09:21:58 0xFFFFFFFF via Digitalmars-d-learn 
wrote:
 What is the equivalent of C++17 std::string_view (an object that
 can refer to a constant contiguous sequence of char-like objects
 with the first element of the sequence at position zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot
 changes since v1.0.
strings in D are that way by their very nature, because they're dynamic arrays, and in D, dynamic arrays are just poiner and a length. Effectively, a dynamic array is struct DynamicArray(T) { size_t length; T* ptr; } They're slices of some piece of memory - usually GC-allocated memory, though you can slice any memory and get a dynamic array out of it. I'd suggest reading this article: https://dlang.org/articles/d-array-article.html It does not use the official terminology, because it refers to the GC-managed buffer as the dynamic array rather than the T[] as being the dynamic array (whereas the official terminology is that T[] is a dynamic array, regardless of what memory it's a slice of), but otherwise, what it says is quite good and should give you a solid idea of how arrays work in D. - Jonathan M Davis
Feb 21 2018
prev sibling next sibling parent reply 0xFFFFFFFF <0xFFFFFFFF mail.com> writes:
On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object 
 that can refer to a constant contiguous sequence of char-like 
 objects with the first element of the sequence at position 
 zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot 
 changes since v1.0.
Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ``` So I can query it's size, make it point to sth else, use it in callbacks etc. Of course I'm aware of scoping etc... I'm NOT doing any dynamic allocations, I just want a situation where a string allocation string would be a little bit expensive. [I believe I still have to study D more in that case to come up with sth better, enjoying my ride so far]
Feb 21 2018
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 21/02/2018 10:17 AM, 0xFFFFFFFF wrote:
 On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object that can 
 refer to a constant contiguous sequence of char-like objects with the 
 first element of the sequence at position zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot 
 changes since v1.0.
Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ```
string sv = "some string"; assert(sv.length == 11); Or just "some string".length ;)
Feb 21 2018
parent 0xFFFFFFFF <0xFFFFFFFF mail.com> writes:
On Wednesday, 21 February 2018 at 10:22:56 UTC, rikki cattermole 
wrote:
 On 21/02/2018 10:17 AM, 0xFFFFFFFF wrote:
 On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF 
 wrote:
 What is the equivalent of C++17 std::string_view (an object 
 that can refer to a constant contiguous sequence of char-like 
 objects with the first element of the sequence at position 
 zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A 
 lot changes since v1.0.
Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ```
string sv = "some string"; assert(sv.length == 11); Or just "some string".length ;)
Thanks, it took time before I updated my reply to prevous answers. I didn't see these before I did. [I'm not that dumb] :winks:
Feb 21 2018
prev sibling next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, February 21, 2018 10:17:55 0xFFFFFFFF via Digitalmars-d-learn 
wrote:
 On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object
 that can refer to a constant contiguous sequence of char-like
 objects with the first element of the sequence at position
 zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot
 changes since v1.0.
Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ``` So I can query it's size, make it point to sth else, use it in callbacks etc. Of course I'm aware of scoping etc... I'm NOT doing any dynamic allocations, I just want a situation where a string allocation string would be a little bit expensive. [I believe I still have to study D more in that case to come up with sth better, enjoying my ride so far]
As I said in my previous response, read this article: https://dlang.org/articles/d-array-article.html You clearly do not understand at all how strings work in D. Strings in D basically _are_ string_views where most strings have the actual memory buffer managed by the GC. If you do string sv = "some string"; then _zero_ heap allocations take place. - Jonathan M Davis
Feb 21 2018
parent 0xFFFFFFFF <0xFFFFFFFF mail.com> writes:
On Wednesday, 21 February 2018 at 10:23:23 UTC, Jonathan M Davis 
wrote:
 As I said in my previous response, read this article:

 https://dlang.org/articles/d-array-article.html
Sorry, I didn't see this before I replied. Had I, I wouldn't have done that.
string sv = "some string";
then _zero_ heap allocations take place.
I think u've answered my question.
Feb 21 2018
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
0xFFFFFFFF wrote:

 On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object that can 
 refer to a constant contiguous sequence of char-like objects with the 
 first element of the sequence at position zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot changes 
 since v1.0.
Wow! Thanks guys. Those are handy, but I specifically want to do something like: ``` string_view sv = "some string"; ``` So I can query it's size, make it point to sth else, use it in callbacks etc. Of course I'm aware of scoping etc... I'm NOT doing any dynamic allocations, I just want a situation where a string allocation string would be a little bit expensive.
and that is exactly what slices are for! ;-) you are probably better to use `const(char)[]`, tho. like this: // don't store `s`, as it's contents may change after exiting of `myfunc` // (this is what `const` implies here) void myfunc (const(char)[] s) { ... } ... string s = "test string"; myfunc(s); // yep, this works s = s[2..4]; // this doesn't allocate myfunc(s); myfunc(s[3..6]); // or this, it doesn't allocate myfunc("abc"); // or this, doesn't allocate you got the idea.
Feb 21 2018
parent reply 0xFFFFFFFF <0xFFFFFFFF mail.com> writes:
On Wednesday, 21 February 2018 at 10:24:39 UTC, ketmar wrote:
 0xFFFFFFFF wrote:

 [...]
and that is exactly what slices are for! ;-) you are probably better to use `const(char)[]`, tho. like this: // don't store `s`, as it's contents may change after exiting of `myfunc` // (this is what `const` implies here) void myfunc (const(char)[] s) { ... } ... string s = "test string"; myfunc(s); // yep, this works s = s[2..4]; // this doesn't allocate myfunc(s); myfunc(s[3..6]); // or this, it doesn't allocate myfunc("abc"); // or this, doesn't allocate you got the idea.
Gotta save this too. [BTW how do I post a thumbs up emoji]
Feb 21 2018
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 21/02/2018 10:43 AM, 0xFFFFFFFF wrote:
 On Wednesday, 21 February 2018 at 10:24:39 UTC, ketmar wrote:
 0xFFFFFFFF wrote:

 [...]
and that is exactly what slices are for! ;-) you are probably better to use `const(char)[]`, tho. like this:     // don't store `s`, as it's contents may change after exiting of `myfunc`     // (this is what `const` implies here)     void myfunc (const(char)[] s) { ... }     ...     string s = "test string";     myfunc(s); // yep, this works     s = s[2..4]; // this doesn't allocate     myfunc(s);     myfunc(s[3..6]); // or this, it doesn't allocate     myfunc("abc"); // or this, doesn't allocate you got the idea.
Gotta save this too. [BTW how do I post a thumbs up emoji]
+X (typically just 1) means that you agree. But here we just say "thanks". This "forum" is backed by an NNTP server, so lots of the more newer concepts don't exist here.
Feb 21 2018
prev sibling parent Kagamin <spam here.lot> writes:
On Wednesday, 21 February 2018 at 10:43:14 UTC, 0xFFFFFFFF wrote:
 Gotta save this too.

 [BTW how do I post a thumbs up emoji]
👍
Feb 21 2018
prev sibling parent reply 0xFFFFFFFF <0xFFFFFFFF mail.com> writes:
On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object 
 that can refer to a constant contiguous sequence of char-like 
 objects with the first element of the sequence at position 
 zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot 
 changes since v1.0.
Thanks guys all. I completed the task. Glad to know there are lots people helping out here.
Feb 21 2018
parent rikki cattermole <rikki cattermole.co.nz> writes:
On 21/02/2018 10:41 AM, 0xFFFFFFFF wrote:
 On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
 What is the equivalent of C++17 std::string_view (an object that can 
 refer to a constant contiguous sequence of char-like objects with the 
 first element of the sequence at position zero) in D?

 PS: I'm getting back to D after years (since DMD 1 days). A lot 
 changes since v1.0.
Thanks guys all. I completed the task. Glad to know there are lots people helping out here.
Hop on to IRC (FreeNode #d) or Discord https://discord.gg/sVMHUD (invite expires in a day) if you'd like a more interactive conversation :)
Feb 21 2018