commit cfe4bd1121b3007229be2f59fc4a46be715a662c Author: Daniel Swanson Date: Tue Jul 6 14:06:55 2021 -0500 InputFile helper for reading blanks diff --git a/lttoolbox/input_file.cc b/lttoolbox/input_file.cc index 0d910f6..1994fd8 100644 --- a/lttoolbox/input_file.cc +++ b/lttoolbox/input_file.cc @@ -197,3 +197,41 @@ InputFile::finishWBlank() } return ret; } + +UString +InputFile::readBlank(bool readwblank) +{ + UString ret; + while (!eof()) { + UChar32 c = get(); + if (c == '^' || c == '\0' || c == U_EOF) { + unget(c); + break; + } else if (c == '[') { + UChar32 c2 = get(); + if (c2 == '[') { + if (readwblank) { + ret += finishWBlank(); + } else { + // buffer size is 3, so we should be ok here + unget(c2); + unget(c); + break; + } + } else { + unget(c2); + ret += readBlock(c2, ']'); + } + } else { + ret += c; + if (c == '\\') { + if (eof() || peek() == '\0') { + std::cerr << "Unexpected trailing backslash" << std::endl; + exit(EXIT_FAILURE); + } + ret += get(); + } + } + } + return ret; +} diff --git a/lttoolbox/input_file.h b/lttoolbox/input_file.h index de031c8..dffcf7f 100644 --- a/lttoolbox/input_file.h +++ b/lttoolbox/input_file.h @@ -49,6 +49,10 @@ public: // assumes [[ has already been read, reads to ]] // returns entire string, including brackets UString finishWBlank(); + // read until ^ or \0 + // if readwblank == false, also stop at [[ + // Note: relies on the fact that ubuffer has length >= 2 + UString readBlank(bool readwblank = false); }; #endif