mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-24 12:02:08 +00:00
Prefer C++ constructs to C-style sizeof-based macros
This commit is contained in:
@@ -500,27 +500,27 @@ BufferedContent::~BufferedContent() {
|
||||
}
|
||||
|
||||
void BufferedContent::advance() {
|
||||
assume(offset < ARRAY_SIZE(buf));
|
||||
assume(offset < std::size(buf));
|
||||
offset++;
|
||||
if (offset == ARRAY_SIZE(buf))
|
||||
if (offset == std::size(buf))
|
||||
offset = 0; // Wrap around if necessary
|
||||
assume(size > 0);
|
||||
size--;
|
||||
}
|
||||
|
||||
void BufferedContent::refill() {
|
||||
size_t target = ARRAY_SIZE(buf) - size; // Aim: making the buf full
|
||||
size_t target = std::size(buf) - size; // Aim: making the buf full
|
||||
|
||||
// Compute the index we'll start writing to
|
||||
size_t startIndex = (offset + size) % ARRAY_SIZE(buf);
|
||||
size_t startIndex = (offset + size) % std::size(buf);
|
||||
|
||||
// If the range to fill passes over the buffer wrapping point, we need two reads
|
||||
if (startIndex + target > ARRAY_SIZE(buf)) {
|
||||
size_t nbExpectedChars = ARRAY_SIZE(buf) - startIndex;
|
||||
if (startIndex + target > std::size(buf)) {
|
||||
size_t nbExpectedChars = std::size(buf) - startIndex;
|
||||
size_t nbReadChars = readMore(startIndex, nbExpectedChars);
|
||||
|
||||
startIndex += nbReadChars;
|
||||
if (startIndex == ARRAY_SIZE(buf))
|
||||
if (startIndex == std::size(buf))
|
||||
startIndex = 0;
|
||||
|
||||
// If the read was incomplete, don't perform a second read
|
||||
@@ -534,7 +534,7 @@ void BufferedContent::refill() {
|
||||
|
||||
size_t BufferedContent::readMore(size_t startIndex, size_t nbChars) {
|
||||
// This buffer overflow made me lose WEEKS of my life. Never again.
|
||||
assume(startIndex + nbChars <= ARRAY_SIZE(buf));
|
||||
assume(startIndex + nbChars <= std::size(buf));
|
||||
ssize_t nbReadChars = read(fd, &buf[startIndex], nbChars);
|
||||
|
||||
if (nbReadChars == -1)
|
||||
@@ -720,7 +720,7 @@ int LexerState::peekChar() {
|
||||
auto &cbuf = content.get<BufferedContent>();
|
||||
if (cbuf.size == 0)
|
||||
cbuf.refill();
|
||||
assume(cbuf.offset < ARRAY_SIZE(cbuf.buf));
|
||||
assume(cbuf.offset < std::size(cbuf.buf));
|
||||
if (cbuf.size > 0)
|
||||
return static_cast<uint8_t>(cbuf.buf[cbuf.offset]);
|
||||
}
|
||||
@@ -748,11 +748,11 @@ int LexerState::peekCharAhead() {
|
||||
return static_cast<uint8_t>(view.span.ptr[view.offset + distance]);
|
||||
} else {
|
||||
auto &cbuf = content.get<BufferedContent>();
|
||||
assume(distance < ARRAY_SIZE(cbuf.buf));
|
||||
assume(distance < std::size(cbuf.buf));
|
||||
if (cbuf.size <= distance)
|
||||
cbuf.refill();
|
||||
if (cbuf.size > distance)
|
||||
return static_cast<uint8_t>(cbuf.buf[(cbuf.offset + distance) % ARRAY_SIZE(cbuf.buf)]);
|
||||
return static_cast<uint8_t>(cbuf.buf[(cbuf.offset + distance) % std::size(cbuf.buf)]);
|
||||
}
|
||||
|
||||
// If there aren't enough chars, give up
|
||||
@@ -2339,7 +2339,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 -= QUOTEDSTRLEN("ENDR");
|
||||
capture.span.size -= literal_strlen("ENDR");
|
||||
return capture;
|
||||
}
|
||||
depth--;
|
||||
@@ -2385,7 +2385,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 -= QUOTEDSTRLEN("ENDM");
|
||||
capture.span.size -= literal_strlen("ENDM");
|
||||
return capture;
|
||||
|
||||
default:
|
||||
|
||||
@@ -41,7 +41,7 @@ static std::string make_escape(std::string &str) {
|
||||
break;
|
||||
escaped.append(str, pos, nextPos - pos);
|
||||
escaped.append("$$");
|
||||
pos = nextPos + QUOTEDSTRLEN("$");
|
||||
pos = nextPos + literal_strlen("$");
|
||||
}
|
||||
escaped.append(str, pos, str.length() - pos);
|
||||
return escaped;
|
||||
|
||||
@@ -149,16 +149,16 @@ void processWarningFlag(char const *flag) {
|
||||
if (rootFlag.starts_with("error=")) {
|
||||
// `-Werror=<flag>` enables the flag as an error
|
||||
state = {.state = WARNING_ENABLED, .error = WARNING_ENABLED};
|
||||
rootFlag.erase(0, QUOTEDSTRLEN("error="));
|
||||
rootFlag.erase(0, literal_strlen("error="));
|
||||
} else if (rootFlag.starts_with("no-error=")) {
|
||||
// `-Wno-error=<flag>` prevents the flag from being an error,
|
||||
// without affecting whether it is enabled
|
||||
state = {.state = WARNING_DEFAULT, .error = WARNING_DISABLED};
|
||||
rootFlag.erase(0, QUOTEDSTRLEN("no-error="));
|
||||
rootFlag.erase(0, literal_strlen("no-error="));
|
||||
} else if (rootFlag.starts_with("no-")) {
|
||||
// `-Wno-<flag>` disables the flag
|
||||
state = {.state = WARNING_DISABLED, .error = WARNING_DEFAULT};
|
||||
rootFlag.erase(0, QUOTEDSTRLEN("no-"));
|
||||
rootFlag.erase(0, literal_strlen("no-"));
|
||||
} else {
|
||||
// `-W<flag>` enables the flag
|
||||
state = {.state = WARNING_ENABLED, .error = WARNING_DEFAULT};
|
||||
|
||||
Reference in New Issue
Block a user