mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +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:
|
||||
|
||||
Reference in New Issue
Block a user