A string function I've found invaluable for parsing DSLs
downs
default_357-line at yahoo.de
Sat Nov 14 10:16:48 PST 2009
Slightly modified from what you so amusingly called "my CoolTools.ctfe":
ctSlice (not to be confused with the [] array slicing functionality) "slices" a string in two at a marker, returning the text before the marker and removing it (and the marker) from the input text.
The ct prefix is handy for differentiating between std.metastrings and std.string functions - one may often want to import both ..
string ctSlice(ref string what, string where) {
auto loc = what.ctFind(where);
if (loc == -1) {
auto res = what;
what = null;
return res;
}
auto res = what[0 .. loc];
what = what[loc+where.length .. $];
return res;
}
When is this useful?
For instance, when writing a compile-time enum, you may want to use a syntax of the form "EnumName: Entries"
In that case, you could write `string name = input.ctSlice(":"); '
More information about the Digitalmars-d
mailing list