Strings and Slices

Adam D. Ruppe destructionator at gmail.com
Thu Feb 18 21:08:45 UTC 2021


On Thursday, 18 February 2021 at 20:47:33 UTC, Mike Brown wrote:
> Is slices comparable to a string_view?

My c++ is rusty af but yes I think so.

A d slice is `struct slice { size_t length; T* ptr; }` so when in 
doubt just think back to what that does.

> string lex_identifier(ref string input) {

And that makes this ref iffy. Since it is already passed as a 
ptr+length pair, you rarely need ref on it. Only when you'd use a 
char** in C; that is, when you can reassign the value of the 
pointer and have the caller see that change (e.g. you are 
appending to it). If you're just looking, no need for ref.

> In the body of the lex_identifier, i am using drop(). This 
> doesn't seem to do what I thought it did. I want to create a 
> slice from the beginning of a ref slice upto a given mark, and 
> move the beginning point of that ref slice to that mark also.

I do it in two steps:

piece = input[0 .. mark]; // get piece out
input = input[mark .. $]; // advance the original slice


Note that such operations are just `ptr += mark; length -= mark;` 
so they are very cheap.



More information about the Digitalmars-d-learn mailing list