A string function I've found invaluable for parsing DSLs

downs default_357-line at yahoo.de
Sat Nov 14 18:34:39 PST 2009


Bill Baxter wrote:
> On Sat, Nov 14, 2009 at 10:16 AM, downs <default_357-line at yahoo.de> wrote:
>> 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 ..
> 
> Yeh, I agree some diffentiator is good.  std.metastrings started off
> with a capital letter convention to differentiate.  I'm not sure which
> is better.
> 
>> 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(":"); '
> 
> That's nice.
> I would probably call that something like "ctZapToChar", in honor of
> the same function in Emacs. :-)
> Slice is a very confusing name for it.
> 
> So is your code free game for stealing from for std.metastring?
> (meaning whatever I borrowed would be re-licensed under Boost)
> 
> --bb

Feel free. :D

(yes this constitutes a full license to redistribute)



More information about the Digitalmars-d mailing list