lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I find that %b() is a great feature of Lua, but it lacks one crucial
feature: handling the case where one or both of the args may appear in
the string prefixed by an escape character, and thus shouldn't be
counted. One solution to this is gsub'ing the escape sequences and
then matching, but this can be inefficient when dealing with very
large strings.

My suggestion is to add the token %B, which would perform the same
thing as %b, except it would recognize an escape character specified
in the arguments to the token, and upon encountering the escape
character, the following character would be ignored. I doubt it would
handle all use cases, but it almost certainly would handle a large
percentage.

This would be very easy to implement; in fact, I just did so this
evening with zero experience coding in C. [1] (I've confirmed that
this compiles in Visual Studio 2015 and runs on Windows 10, and that
the new feature works, but it has not otherwise been tested, including
for regressions).

The format in that commit is "%B123", where 1 is the beginning
character, 3 is the ending character, and 2 is the escape character. A
simple example in an interactive session:

> str1 = "test(testing^(test...)hello)end"
> str1:match"%b()"
(testing^(test...)hello)
> str1:match"%B(^)"
(testing^(test...)
> str2 = [[test(testing\(test...)hello)end]]
> str2:match[[%B(\)]]
(testing\(test...)

What do people think about this? Is this a good idea, bad idea,
somewhere in between? I'm especially interested in hearing Roberto's
opinion.

[1] https://github.com/jcgoble3/lua-testing/commit/ee90b07d7a8c8900e0a1c29cced1b3bf4576e8fc