Use QUOTEDSTRLEN macro instead of sizeof or strlen

This commit is contained in:
Rangi42
2024-03-27 11:50:48 -04:00
parent 912a1504ec
commit dcb4e40388
5 changed files with 16 additions and 11 deletions

View File

@@ -86,6 +86,9 @@ static inline int clz(unsigned int x) {
// For lack of <ranges>, this adds some more brevity // For lack of <ranges>, this adds some more brevity
#define RANGE(s) std::begin(s), std::end(s) #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))` // For ad-hoc RAII in place of a `defer` statement or cross-platform `__attribute__((cleanup))`
template<typename T> template<typename T>
struct Defer { struct Defer {

View File

@@ -21,6 +21,7 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include "helpers.hpp" // QUOTEDSTRLEN
#include "util.hpp" #include "util.hpp"
#include "asm/fixpoint.hpp" #include "asm/fixpoint.hpp"
@@ -2233,7 +2234,7 @@ Capture lexer_CaptureRept() {
endCapture(capture); endCapture(capture);
// The final ENDR has been captured, but we don't want it! // The final ENDR has been captured, but we don't want it!
// We know we have read exactly "ENDR", not e.g. an EQUS // We know we have read exactly "ENDR", not e.g. an EQUS
capture.span.size -= strlen("ENDR"); capture.span.size -= QUOTEDSTRLEN("ENDR");
return capture; return capture;
} }
depth--; depth--;
@@ -2279,7 +2280,7 @@ Capture lexer_CaptureMacro() {
endCapture(capture); endCapture(capture);
// The ENDM has been captured, but we don't want it! // The ENDM has been captured, but we don't want it!
// We know we have read exactly "ENDM", not e.g. an EQUS // We know we have read exactly "ENDM", not e.g. an EQUS
capture.span.size -= strlen("ENDM"); capture.span.size -= QUOTEDSTRLEN("ENDM");
return capture; return capture;
default: default:

View File

@@ -10,7 +10,7 @@
#include "error.hpp" #include "error.hpp"
#include "extern/getopt.hpp" #include "extern/getopt.hpp"
#include "helpers.hpp" // Defer #include "helpers.hpp"
#include "parser.hpp" #include "parser.hpp"
#include "version.hpp" #include "version.hpp"
@@ -45,7 +45,7 @@ static std::string make_escape(std::string &str) {
break; break;
escaped.append(str, pos, nextPos - pos); escaped.append(str, pos, nextPos - pos);
escaped.append("$$"); escaped.append("$$");
pos = nextPos + sizeof("$") - 1; pos = nextPos + QUOTEDSTRLEN("$");
} }
escaped.append(str, pos, str.length() - pos); escaped.append(str, pos, str.length() - pos);
return escaped; return escaped;

View File

@@ -10,6 +10,7 @@
#include <string.h> #include <string.h>
#include "error.hpp" #include "error.hpp"
#include "helpers.hpp" // QUOTEDSTRLEN
#include "itertools.hpp" #include "itertools.hpp"
#include "asm/fstack.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 it's not a meta warning, specially check against `-Werror`
if (!strncmp(flag, "error", strlen("error"))) { if (!strncmp(flag, "error", QUOTEDSTRLEN("error"))) {
char const *errorFlag = flag + strlen("error"); char const *errorFlag = flag + QUOTEDSTRLEN("error");
switch (*errorFlag) { switch (*errorFlag) {
case '\0': case '\0':
@@ -254,9 +255,9 @@ void processWarningFlag(char const *flag) {
WarningState state = setError ? WARNING_ERROR WarningState state = setError ? WARNING_ERROR
// Not an error, then check if this is a negation // Not an error, then check if this is a negation
: strncmp(flag, "no-", strlen("no-")) ? WARNING_ENABLED : strncmp(flag, "no-", QUOTEDSTRLEN("no-")) ? WARNING_ENABLED
: WARNING_DISABLED; : WARNING_DISABLED;
char const *rootFlag = state == WARNING_DISABLED ? flag + strlen("no-") : flag; char const *rootFlag = state == WARNING_DISABLED ? flag + QUOTEDSTRLEN("no-") : flag;
// Is this a "parametric" warning? // Is this a "parametric" warning?
if (state != WARNING_DISABLED) { // The `no-` form cannot be parametrized if (state != WARNING_DISABLED) { // The `no-` form cannot be parametrized

View File

@@ -632,9 +632,9 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
char *ptr = buf; char *ptr = buf;
for (uint16_t cgbColor : list) { for (uint16_t cgbColor : list) {
sprintf(ptr, ", $%04x", cgbColor); 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 // Iterate through proto-palettes, and try mapping them to the specified palettes