diff --git a/include/helpers.hpp b/include/helpers.hpp index 010beb5a5..5186a47df 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -86,6 +86,9 @@ static inline int clz(unsigned int x) { // For lack of , this adds some more brevity #define RANGE(s) std::begin(s), std::end(s) +// MSVC does not inline `strlen()` or `.length()` of a constant string, so we use `sizeof` +#define QUOTEDSTRLEN(s) (sizeof(s) - 1) + // For ad-hoc RAII in place of a `defer` statement or cross-platform `__attribute__((cleanup))` template struct Defer { diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 7d9d15b52..dd72443f4 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -21,6 +21,7 @@ #include #endif +#include "helpers.hpp" // QUOTEDSTRLEN #include "util.hpp" #include "asm/fixpoint.hpp" @@ -2233,7 +2234,7 @@ Capture lexer_CaptureRept() { endCapture(capture); // The final ENDR has been captured, but we don't want it! // We know we have read exactly "ENDR", not e.g. an EQUS - capture.span.size -= strlen("ENDR"); + capture.span.size -= QUOTEDSTRLEN("ENDR"); return capture; } depth--; @@ -2279,7 +2280,7 @@ Capture lexer_CaptureMacro() { endCapture(capture); // The ENDM has been captured, but we don't want it! // We know we have read exactly "ENDM", not e.g. an EQUS - capture.span.size -= strlen("ENDM"); + capture.span.size -= QUOTEDSTRLEN("ENDM"); return capture; default: diff --git a/src/asm/main.cpp b/src/asm/main.cpp index b8d988b3e..136b90f1b 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -10,7 +10,7 @@ #include "error.hpp" #include "extern/getopt.hpp" -#include "helpers.hpp" // Defer +#include "helpers.hpp" #include "parser.hpp" #include "version.hpp" @@ -45,7 +45,7 @@ static std::string make_escape(std::string &str) { break; escaped.append(str, pos, nextPos - pos); escaped.append("$$"); - pos = nextPos + sizeof("$") - 1; + pos = nextPos + QUOTEDSTRLEN("$"); } escaped.append(str, pos, str.length() - pos); return escaped; diff --git a/src/asm/warning.cpp b/src/asm/warning.cpp index 63a46b5fa..1707d69be 100644 --- a/src/asm/warning.cpp +++ b/src/asm/warning.cpp @@ -10,6 +10,7 @@ #include #include "error.hpp" +#include "helpers.hpp" // QUOTEDSTRLEN #include "itertools.hpp" #include "asm/fstack.hpp" @@ -230,8 +231,8 @@ void processWarningFlag(char const *flag) { } // If it's not a meta warning, specially check against `-Werror` - if (!strncmp(flag, "error", strlen("error"))) { - char const *errorFlag = flag + strlen("error"); + if (!strncmp(flag, "error", QUOTEDSTRLEN("error"))) { + char const *errorFlag = flag + QUOTEDSTRLEN("error"); switch (*errorFlag) { case '\0': @@ -254,9 +255,9 @@ void processWarningFlag(char const *flag) { WarningState state = setError ? WARNING_ERROR // Not an error, then check if this is a negation - : strncmp(flag, "no-", strlen("no-")) ? WARNING_ENABLED - : WARNING_DISABLED; - char const *rootFlag = state == WARNING_DISABLED ? flag + strlen("no-") : flag; + : strncmp(flag, "no-", QUOTEDSTRLEN("no-")) ? WARNING_ENABLED + : WARNING_DISABLED; + char const *rootFlag = state == WARNING_DISABLED ? flag + QUOTEDSTRLEN("no-") : flag; // Is this a "parametric" warning? if (state != WARNING_DISABLED) { // The `no-` form cannot be parametrized diff --git a/src/gfx/process.cpp b/src/gfx/process.cpp index 20d92035c..58d2c7a2b 100644 --- a/src/gfx/process.cpp +++ b/src/gfx/process.cpp @@ -632,9 +632,9 @@ static std::tuple, std::vector> char *ptr = buf; for (uint16_t cgbColor : list) { sprintf(ptr, ", $%04x", cgbColor); - ptr += 7; + ptr += QUOTEDSTRLEN(", $XXXX"); } - return &buf[2]; + return &buf[QUOTEDSTRLEN(", ")]; }; // Iterate through proto-palettes, and try mapping them to the specified palettes