Strings and Slices

Mike Brown mikey.be at gmail.com
Thu Feb 18 20:47:33 UTC 2021


Hi All,

I'm rebuilding a C++, and the beginning section is a lexer that 
uses strings, and string_view.

Is slices comparable to a string_view?

The architecture of the lexer is a single layer (Non-)FSM Lexer. 
Basically a main loop, checking the first letter of the current 
input position, which then calls a function/lambda to continue 
from there.

string lex_identifier(ref string input) {
   ...
}

while(!input.empty()) {
   if (isAlpha(input.front)) {
     auto tmp = lex_identifier(input);
   }
}

I'm passing in a ref because I ideally want to iterate over a 
string, and to produce a slice to the lexeme. This needs a way to 
create a mark at a given point, and have an iteration point. 
These marks seem logically to be the slice start and end.

Is there a better way to make these slices?
The ref doesn't work well with unittests that pass in a literal, 
is there an easier way than creating a temp var for the input?

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.

what is the best way to achieve this?

Kind regards,
Mikey





More information about the Digitalmars-d-learn mailing list