Pass std::string references to charmap functions

This commit is contained in:
Rangi42
2024-03-18 11:24:25 -04:00
committed by Sylvie
parent 04899a21cd
commit 91164ac1b0
3 changed files with 30 additions and 29 deletions

View File

@@ -4,17 +4,18 @@
#define RGBDS_ASM_CHARMAP_H #define RGBDS_ASM_CHARMAP_H
#include <stdint.h> #include <stdint.h>
#include <string>
#include <vector> #include <vector>
#define DEFAULT_CHARMAP_NAME "main" #define DEFAULT_CHARMAP_NAME "main"
void charmap_New(char const *name, char const *baseName); void charmap_New(std::string const &name, std::string const *baseName);
void charmap_Set(char const *name); void charmap_Set(std::string const &name);
void charmap_Push(); void charmap_Push();
void charmap_Pop(); void charmap_Pop();
void charmap_Add(char const *mapping, uint8_t value); void charmap_Add(std::string const &mapping, uint8_t value);
bool charmap_HasChar(char const *input); bool charmap_HasChar(std::string const &input);
void charmap_Convert(char const *input, std::vector<uint8_t> &output); void charmap_Convert(std::string const &input, std::vector<uint8_t> &output);
size_t charmap_ConvertNext(char const *&input, std::vector<uint8_t> *output); size_t charmap_ConvertNext(char const *&input, std::vector<uint8_t> *output);
#endif // RGBDS_ASM_CHARMAP_H #endif // RGBDS_ASM_CHARMAP_H

View File

@@ -7,7 +7,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string>
#include <unordered_map> #include <unordered_map>
#include "util.hpp" #include "util.hpp"
@@ -36,20 +35,20 @@ static std::unordered_map<std::string, Charmap> charmaps;
static Charmap *currentCharmap; static Charmap *currentCharmap;
std::stack<Charmap *> charmapStack; std::stack<Charmap *> charmapStack;
void charmap_New(char const *name, char const *baseName) { void charmap_New(std::string const &name, std::string const *baseName) {
Charmap *base = nullptr; Charmap *base = nullptr;
if (baseName != nullptr) { if (baseName != nullptr) {
auto search = charmaps.find(baseName); auto search = charmaps.find(*baseName);
if (search == charmaps.end()) if (search == charmaps.end())
error("Base charmap '%s' doesn't exist\n", baseName); error("Base charmap '%s' doesn't exist\n", baseName->c_str());
else else
base = &search->second; base = &search->second;
} }
if (charmaps.find(name) != charmaps.end()) { if (charmaps.find(name) != charmaps.end()) {
error("Charmap '%s' already exists\n", name); error("Charmap '%s' already exists\n", name.c_str());
return; return;
} }
@@ -65,11 +64,11 @@ void charmap_New(char const *name, char const *baseName) {
currentCharmap = &charmap; currentCharmap = &charmap;
} }
void charmap_Set(char const *name) { void charmap_Set(std::string const &name) {
auto search = charmaps.find(name); auto search = charmaps.find(name);
if (search == charmaps.end()) if (search == charmaps.end())
error("Charmap '%s' doesn't exist\n", name); error("Charmap '%s' doesn't exist\n", name.c_str());
else else
currentCharmap = &search->second; currentCharmap = &search->second;
} }
@@ -88,12 +87,12 @@ void charmap_Pop() {
charmapStack.pop(); charmapStack.pop();
} }
void charmap_Add(char const *mapping, uint8_t value) { void charmap_Add(std::string const &mapping, uint8_t value) {
Charmap &charmap = *currentCharmap; Charmap &charmap = *currentCharmap;
size_t nodeIdx = 0; size_t nodeIdx = 0;
for (; *mapping; mapping++) { for (char c : mapping) {
size_t &nextIdxRef = charmap.nodes[nodeIdx].next[(uint8_t)*mapping - 1]; size_t &nextIdxRef = charmap.nodes[nodeIdx].next[(uint8_t)c - 1];
size_t nextIdx = nextIdxRef; size_t nextIdx = nextIdxRef;
if (!nextIdx) { if (!nextIdx) {
@@ -117,12 +116,12 @@ void charmap_Add(char const *mapping, uint8_t value) {
node.value = value; node.value = value;
} }
bool charmap_HasChar(char const *input) { bool charmap_HasChar(std::string const &input) {
Charmap const &charmap = *currentCharmap; Charmap const &charmap = *currentCharmap;
size_t nodeIdx = 0; size_t nodeIdx = 0;
for (; *input; input++) { for (char c : input) {
nodeIdx = charmap.nodes[nodeIdx].next[(uint8_t)*input - 1]; nodeIdx = charmap.nodes[nodeIdx].next[(uint8_t)c - 1];
if (!nodeIdx) if (!nodeIdx)
return false; return false;
@@ -131,8 +130,9 @@ bool charmap_HasChar(char const *input) {
return charmap.nodes[nodeIdx].isTerminal; return charmap.nodes[nodeIdx].isTerminal;
} }
void charmap_Convert(char const *input, std::vector<uint8_t> &output) { void charmap_Convert(std::string const &input, std::vector<uint8_t> &output) {
while (charmap_ConvertNext(input, &output)) char const *ptr = input.c_str();
while (charmap_ConvertNext(ptr, &output))
; ;
} }

View File

@@ -1141,22 +1141,22 @@ incbin:
charmap: charmap:
POP_CHARMAP string COMMA const_8bit { POP_CHARMAP string COMMA const_8bit {
charmap_Add($2.c_str(), (uint8_t)$4); charmap_Add($2, (uint8_t)$4);
} }
; ;
newcharmap: newcharmap:
POP_NEWCHARMAP ID { POP_NEWCHARMAP ID {
charmap_New($2.c_str(), nullptr); charmap_New($2, nullptr);
} }
| POP_NEWCHARMAP ID COMMA ID { | POP_NEWCHARMAP ID COMMA ID {
charmap_New($2.c_str(), $4.c_str()); charmap_New($2, &$4);
} }
; ;
setcharmap: setcharmap:
POP_SETCHARMAP ID { POP_SETCHARMAP ID {
charmap_Set($2.c_str()); charmap_Set($2);
} }
; ;
@@ -1224,7 +1224,7 @@ constlist_8bit_entry:
| string { | string {
std::vector<uint8_t> output; std::vector<uint8_t> output;
charmap_Convert($1.c_str(), output); charmap_Convert($1, output);
sect_AbsByteGroup(output.data(), output.size()); sect_AbsByteGroup(output.data(), output.size());
} }
; ;
@@ -1241,7 +1241,7 @@ constlist_16bit_entry:
| string { | string {
std::vector<uint8_t> output; std::vector<uint8_t> output;
charmap_Convert($1.c_str(), output); charmap_Convert($1, output);
sect_AbsWordGroup(output.data(), output.size()); sect_AbsWordGroup(output.data(), output.size());
} }
; ;
@@ -1258,7 +1258,7 @@ constlist_32bit_entry:
| string { | string {
std::vector<uint8_t> output; std::vector<uint8_t> output;
charmap_Convert($1.c_str(), output); charmap_Convert($1, output);
sect_AbsLongGroup(output.data(), output.size()); sect_AbsLongGroup(output.data(), output.size());
} }
; ;
@@ -1309,7 +1309,7 @@ relocexpr:
| string { | string {
std::vector<uint8_t> output; std::vector<uint8_t> output;
charmap_Convert($1.c_str(), output); charmap_Convert($1, output);
rpn_Number($$, str2int2(output)); rpn_Number($$, str2int2(output));
} }
; ;
@@ -1492,7 +1492,7 @@ relocexpr_no_str:
rpn_Number($$, charlenUTF8($3)); rpn_Number($$, charlenUTF8($3));
} }
| OP_INCHARMAP LPAREN string RPAREN { | OP_INCHARMAP LPAREN string RPAREN {
rpn_Number($$, charmap_HasChar($3.c_str())); rpn_Number($$, charmap_HasChar($3));
} }
| LPAREN relocexpr RPAREN { | LPAREN relocexpr RPAREN {
$$ = std::move($2); $$ = std::move($2);