Don't count single quote ' as garbage (#1801)

Also copy the "blank space" (space or tab) vs "whitespace" (space,
tab, or newline) convention from `<ctype.h>`
This commit is contained in:
Rangi
2025-08-14 10:10:59 -04:00
committed by GitHub
parent ea1358bbe6
commit db6793f444
15 changed files with 98 additions and 72 deletions

View File

@@ -186,7 +186,7 @@ static uint16_t parseNumber(char *&string, char const *errPrefix, uint16_t errVa
return number;
}
static void skipWhitespace(char *&arg) {
static void skipBlankSpace(char *&arg) {
arg += strspn(arg, " \t");
}
@@ -214,8 +214,8 @@ static std::vector<size_t> readAtFile(std::string const &path, std::vector<char>
for (std::vector<size_t> argvOfs;;) {
int c = file->sbumpc();
// First, discard any leading whitespace
while (isWhitespace(c)) {
// First, discard any leading blank space
while (isBlankSpace(c)) {
c = file->sbumpc();
}
@@ -239,13 +239,13 @@ static std::vector<size_t> readAtFile(std::string const &path, std::vector<char>
// Read one argument (until the next whitespace char).
// We know there is one because we already have its first character in `c`.
for (; c != EOF && !isNewline(c) && !isWhitespace(c); c = file->sbumpc()) {
for (; c != EOF && !isWhitespace(c); c = file->sbumpc()) {
argPool.push_back(c);
}
argPool.push_back('\0');
// Discard whitespace until the next argument (candidate)
while (isWhitespace(c)) {
// Discard blank space until the next argument (candidate)
while (isBlankSpace(c)) {
c = file->sbumpc();
}
} while (c != EOF && !isNewline(c)); // End if we reached EOL
@@ -285,7 +285,7 @@ static char *parseArgv(int argc, char *argv[]) {
options.baseTileIDs[1] = 0;
break;
}
skipWhitespace(arg);
skipBlankSpace(arg);
if (*arg != ',') {
error(
"Base tile IDs must be one or two comma-separated numbers, not \"%s\"",
@@ -294,7 +294,7 @@ static char *parseArgv(int argc, char *argv[]) {
break;
}
++arg; // Skip comma
skipWhitespace(arg);
skipBlankSpace(arg);
number = parseNumber(arg, "Bank 1 base tile ID", 0);
if (number >= 256) {
error("Bank 1 base tile ID must be below 256");
@@ -356,23 +356,23 @@ static char *parseArgv(int argc, char *argv[]) {
error("Input slice left coordinate is out of range!");
break;
}
skipWhitespace(arg);
skipBlankSpace(arg);
if (*arg != ',') {
error("Missing comma after left coordinate in \"%s\"", musl_optarg);
break;
}
++arg;
skipWhitespace(arg);
skipBlankSpace(arg);
options.inputSlice.top = parseNumber(arg, "Input slice upper coordinate");
skipWhitespace(arg);
skipBlankSpace(arg);
if (*arg != ':') {
error("Missing colon after upper coordinate in \"%s\"", musl_optarg);
break;
}
++arg;
skipWhitespace(arg);
skipBlankSpace(arg);
options.inputSlice.width = parseNumber(arg, "Input slice width");
skipWhitespace(arg);
skipBlankSpace(arg);
if (options.inputSlice.width == 0) {
error("Input slice width may not be 0!");
}
@@ -381,7 +381,7 @@ static char *parseArgv(int argc, char *argv[]) {
break;
}
++arg;
skipWhitespace(arg);
skipBlankSpace(arg);
options.inputSlice.height = parseNumber(arg, "Input slice height");
if (options.inputSlice.height == 0) {
error("Input slice height may not be 0!");
@@ -416,7 +416,7 @@ static char *parseArgv(int argc, char *argv[]) {
options.maxNbTiles[1] = 0;
break;
}
skipWhitespace(arg);
skipBlankSpace(arg);
if (*arg != ',') {
error(
"Bank capacity must be one or two comma-separated numbers, not \"%s\"",
@@ -425,7 +425,7 @@ static char *parseArgv(int argc, char *argv[]) {
break;
}
++arg; // Skip comma
skipWhitespace(arg);
skipBlankSpace(arg);
options.maxNbTiles[1] = parseNumber(arg, "Number of tiles in bank 1", 256);
if (options.maxNbTiles[1] > 256) {
error("Bank 1 cannot contain more than 256 tiles");

View File

@@ -31,7 +31,7 @@ using namespace std::string_view_literals;
static char const *hexDigits = "0123456789ABCDEFabcdef";
template<typename Str> // Should be std::string or std::string_view
static void skipWhitespace(Str const &str, size_t &pos) {
static void skipBlankSpace(Str const &str, size_t &pos) {
pos = std::min(str.find_first_not_of(" \t"sv, pos), str.length());
}
@@ -120,8 +120,8 @@ void parseInlinePalSpec(char const * const rawArg) {
n = pos;
}
// Skip whitespace, if any
skipWhitespace(arg, n);
// Skip trailing space, if any
skipBlankSpace(arg, n);
// Skip comma/semicolon, or end
if (n == arg.length()) {
@@ -134,7 +134,7 @@ void parseInlinePalSpec(char const * const rawArg) {
++nbColors;
// A trailing comma may be followed by a semicolon
skipWhitespace(arg, n);
skipBlankSpace(arg, n);
if (n == arg.length()) {
break;
} else if (arg[n] != ';' && arg[n] != ':') {
@@ -149,7 +149,7 @@ void parseInlinePalSpec(char const * const rawArg) {
case ':':
case ';':
++n;
skipWhitespace(arg, n);
skipBlankSpace(arg, n);
nbColors = 0; // Start a new palette
// Avoid creating a spurious empty palette
@@ -253,7 +253,7 @@ static std::optional<Rgba> parseColor(std::string const &str, size_t &n, uint16_
error("Failed to parse color #%d (\"%s\"): invalid red component", i + 1, str.c_str());
return std::nullopt;
}
skipWhitespace(str, n);
skipBlankSpace(str, n);
if (n == str.length()) {
error("Failed to parse color #%d (\"%s\"): missing green component", i + 1, str.c_str());
return std::nullopt;
@@ -263,7 +263,7 @@ static std::optional<Rgba> parseColor(std::string const &str, size_t &n, uint16_
error("Failed to parse color #%d (\"%s\"): invalid green component", i + 1, str.c_str());
return std::nullopt;
}
skipWhitespace(str, n);
skipBlankSpace(str, n);
if (n == str.length()) {
error("Failed to parse color #%d (\"%s\"): missing blue component", i + 1, str.c_str());
return std::nullopt;
@@ -356,7 +356,7 @@ static void parseGPLFile(char const *filename, std::filebuf &file) {
}
size_t n = 0;
skipWhitespace(line, n);
skipBlankSpace(line, n);
// Skip empty lines, or lines that contain just a comment.
if (line.length() == n || line[n] == '#') {
continue;