mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Use std::vector for capture buffer
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include <deque>
|
#include <deque>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "platform.hpp" // SSIZE_MAX
|
#include "platform.hpp" // SSIZE_MAX
|
||||||
|
|
||||||
@@ -82,8 +83,7 @@ struct LexerState {
|
|||||||
|
|
||||||
bool capturing; // Whether the text being lexed should be captured
|
bool capturing; // Whether the text being lexed should be captured
|
||||||
size_t captureSize; // Amount of text captured
|
size_t captureSize; // Amount of text captured
|
||||||
char *captureBuf; // Buffer to send the captured text to if non-null
|
std::vector<char> *captureBuf; // Buffer to send the captured text to if non-null
|
||||||
size_t captureCapacity; // Size of the buffer above
|
|
||||||
|
|
||||||
bool disableMacroArgs;
|
bool disableMacroArgs;
|
||||||
bool disableInterpolation;
|
bool disableInterpolation;
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
struct CharmapNode {
|
struct CharmapNode {
|
||||||
bool isTerminal; // Whether there exists a mapping that ends here
|
bool isTerminal; // Whether there exists a mapping that ends here
|
||||||
uint8_t value; // If the above is true, its corresponding value
|
uint8_t value; // If the above is true, its corresponding value
|
||||||
// This MUST be indexes and not pointers, because pointers get invalidated by `realloc`!
|
// This MUST be indexes and not pointers, because pointers get invalidated by reallocation!
|
||||||
size_t next[255]; // Indexes of where to go next, 0 = nowhere
|
size_t next[255]; // Indexes of where to go next, 0 = nowhere
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -487,19 +488,6 @@ void lexer_ToggleStringExpansion(bool enable)
|
|||||||
|
|
||||||
// Functions for the actual lexer to obtain characters
|
// Functions for the actual lexer to obtain characters
|
||||||
|
|
||||||
static void reallocCaptureBuf()
|
|
||||||
{
|
|
||||||
if (lexerState->captureCapacity == SIZE_MAX)
|
|
||||||
fatalerror("Cannot grow capture buffer past %zu bytes\n", SIZE_MAX);
|
|
||||||
else if (lexerState->captureCapacity > SIZE_MAX / 2)
|
|
||||||
lexerState->captureCapacity = SIZE_MAX;
|
|
||||||
else
|
|
||||||
lexerState->captureCapacity *= 2;
|
|
||||||
lexerState->captureBuf = (char *)realloc(lexerState->captureBuf, lexerState->captureCapacity);
|
|
||||||
if (!lexerState->captureBuf)
|
|
||||||
fatalerror("realloc error while resizing capture buffer: %s\n", strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void beginExpansion(char const *str, bool owned, char const *name)
|
static void beginExpansion(char const *str, bool owned, char const *name)
|
||||||
{
|
{
|
||||||
size_t size = strlen(str);
|
size_t size = strlen(str);
|
||||||
@@ -763,12 +751,8 @@ static int peek()
|
|||||||
static void shiftChar()
|
static void shiftChar()
|
||||||
{
|
{
|
||||||
if (lexerState->capturing) {
|
if (lexerState->capturing) {
|
||||||
if (lexerState->captureBuf) {
|
if (lexerState->captureBuf)
|
||||||
if (lexerState->captureSize + 1 >= lexerState->captureCapacity)
|
lexerState->captureBuf->push_back(peek());
|
||||||
reallocCaptureBuf();
|
|
||||||
// TODO: improve this?
|
|
||||||
lexerState->captureBuf[lexerState->captureSize] = peek();
|
|
||||||
}
|
|
||||||
lexerState->captureSize++;
|
lexerState->captureSize++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2293,19 +2277,20 @@ static void startCapture(CaptureBody *capture)
|
|||||||
if (lexerState->isMmapped && lexerState->expansions.empty()) {
|
if (lexerState->isMmapped && lexerState->expansions.empty()) {
|
||||||
capture->body = &lexerState->mmap.ptr.unreferenced[lexerState->mmap.offset];
|
capture->body = &lexerState->mmap.ptr.unreferenced[lexerState->mmap.offset];
|
||||||
} else {
|
} else {
|
||||||
lexerState->captureCapacity = 128; // The initial size will be twice that
|
|
||||||
assert(lexerState->captureBuf == nullptr);
|
assert(lexerState->captureBuf == nullptr);
|
||||||
reallocCaptureBuf();
|
lexerState->captureBuf = new(std::nothrow) std::vector<char>();
|
||||||
|
if (!lexerState->captureBuf)
|
||||||
|
fatalerror("Failed to allocate capture buffer: %s\n", strerror(errno));
|
||||||
capture->body = nullptr; // Indicate to retrieve the capture buffer when done capturing
|
capture->body = nullptr; // Indicate to retrieve the capture buffer when done capturing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void endCapture(CaptureBody *capture)
|
static void endCapture(CaptureBody *capture)
|
||||||
{
|
{
|
||||||
// This being `nullptr` means we're capturing from the capture buf, which is `realloc`ed
|
// This being `nullptr` means we're capturing from the capture buf, which is reallocated
|
||||||
// during the whole capture process, and so MUST be retrieved at the end
|
// during the whole capture process, and so MUST be retrieved at the end
|
||||||
if (!capture->body)
|
if (!capture->body)
|
||||||
capture->body = lexerState->captureBuf;
|
capture->body = lexerState->captureBuf->data();
|
||||||
capture->size = lexerState->captureSize;
|
capture->size = lexerState->captureSize;
|
||||||
|
|
||||||
lexerState->capturing = false;
|
lexerState->capturing = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user