diff --git a/ZString.cpp b/ZString.cpp index 0d3f460..13a2628 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -328,16 +328,24 @@ namespace coreutils { } ZString ZString::getTokenExclude(const char *exclude) { - char previous = 0; + // Scans up to the first character in exclude that is not escaped. + // A terminator is escaped only by an odd run of backslashes before + // it, so "a\\" ends at its closing quote while "a\"b" does not. + // Escapes are left in the returned token as written. Stops at the + // end of the data - previously a token ending in a backslash looped + // forever there. char *start = cursor; - do { - while ((cursor < (data + length)) && !isCharacter(*cursor, exclude)) { - previous = *cursor; - ++cursor; + char *end = data + length; + while(cursor < end) { + if(isCharacter(*cursor, exclude)) { + size_t backslashes = 0; + for(char *p = cursor; (p > start) && (p[-1] == '\\'); --p) + ++backslashes; + if((backslashes % 2) == 0) + break; } ++cursor; - } while(previous == '\\'); - --cursor; + } return ZString(start, cursor - start); }