Skip to content

Commit

Permalink
Use QUOTEDSTRLEN macro instead of sizeof or strlen
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Mar 27, 2024
1 parent 912a150 commit dcb4e40
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
3 changes: 3 additions & 0 deletions include/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ static inline int clz(unsigned int x) {
// For lack of <ranges>, 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<typename T>
struct Defer {
Expand Down
5 changes: 3 additions & 2 deletions src/asm/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <unistd.h>
#endif

#include "helpers.hpp" // QUOTEDSTRLEN
#include "util.hpp"

#include "asm/fixpoint.hpp"
Expand Down Expand Up @@ -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--;
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/asm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "error.hpp"
#include "extern/getopt.hpp"
#include "helpers.hpp" // Defer
#include "helpers.hpp"
#include "parser.hpp"
#include "version.hpp"

Expand Down Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/asm/warning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string.h>

#include "error.hpp"
#include "helpers.hpp" // QUOTEDSTRLEN
#include "itertools.hpp"

#include "asm/fstack.hpp"
Expand Down Expand Up @@ -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':
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/gfx/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,9 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
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
Expand Down

0 comments on commit dcb4e40

Please sign in to comment.