Skip to content

Commit

Permalink
Refactor readUTF8Char into charmap_ConvertNext
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Jan 28, 2025
1 parent 34a9c8e commit e49291b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
7 changes: 0 additions & 7 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
#ifndef RGBDS_UTIL_HPP
#define RGBDS_UTIL_HPP

#include <stddef.h>
#include <stdint.h>
#include <vector>

char const *printChar(int c);

// @return The number of bytes read, or 0 if invalid data was found
size_t readUTF8Char(std::vector<int32_t> *dest, char const *src);

#endif // RGBDS_UTIL_HPP
21 changes: 18 additions & 3 deletions src/asm/charmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <unordered_map>
#include <utility>

#include "extern/utf8decoder.hpp"
#include "helpers.hpp"
#include "util.hpp"

Expand Down Expand Up @@ -224,16 +225,30 @@ size_t charmap_ConvertNext(std::string_view &input, std::vector<int32_t> *output

matchLen = value.size();
} else if (inputIdx < input.length()) { // No match found, but there is some input left
int firstChar = input[inputIdx];
size_t codepointLen = 0;
// This will write the codepoint's value to `output`, little-endian
size_t codepointLen = readUTF8Char(output, input.data() + inputIdx);
for (uint32_t state = 0, codepoint = 0;;) {
if (decode(&state, &codepoint, input[inputIdx + codepointLen]) == 1) {
codepointLen = 0;
break;
}

if (output) {
output->push_back(input[inputIdx + codepointLen]);
}
codepointLen++;

if (state == 0) {
break;
}
}

if (codepointLen == 0) {
error("Input string is not valid UTF-8\n");
}

// Warn if this character is not mapped but any others are
if (charmap.nodes.size() > 1) {
if (int firstChar = input[inputIdx]; charmap.nodes.size() > 1) {
warning(WARNING_UNMAPPED_CHAR_1, "Unmapped character %s\n", printChar(firstChar));
} else if (charmap.name != DEFAULT_CHARMAP_NAME) {
warning(
Expand Down
10 changes: 5 additions & 5 deletions src/asm/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -2527,10 +2527,10 @@ static size_t strlenUTF8(std::string const &str) {
size_t len = 0;
uint32_t state = 0;

for (uint32_t codep = 0; *ptr; ptr++) {
for (uint32_t codepoint = 0; *ptr; ptr++) {
uint8_t byte = *ptr;

switch (decode(&state, &codep, byte)) {
switch (decode(&state, &codepoint, byte)) {
case 1:
errorInvalidUTF8Byte(byte, "STRLEN");
state = 0;
Expand All @@ -2553,12 +2553,12 @@ static std::string strsubUTF8(std::string const &str, uint32_t pos, uint32_t len
char const *ptr = str.c_str();
size_t index = 0;
uint32_t state = 0;
uint32_t codep = 0;
uint32_t codepoint = 0;
uint32_t curPos = 1; // RGBASM strings are 1-indexed!

// Advance to starting position in source string.
while (ptr[index] && curPos < pos) {
switch (decode(&state, &codep, ptr[index])) {
switch (decode(&state, &codepoint, ptr[index])) {
case 1:
errorInvalidUTF8Byte(ptr[index], "STRSUB");
state = 0;
Expand All @@ -2583,7 +2583,7 @@ static std::string strsubUTF8(std::string const &str, uint32_t pos, uint32_t len

// Compute the result length in bytes.
while (ptr[index] && curLen < len) {
switch (decode(&state, &codep, ptr[index])) {
switch (decode(&state, &codepoint, ptr[index])) {
case 1:
errorInvalidUTF8Byte(ptr[index], "STRSUB");
state = 0;
Expand Down
23 changes: 0 additions & 23 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <vector>

#include "extern/utf8decoder.hpp"

char const *printChar(int c) {
// "'A'" + '\0': 4 bytes
Expand Down Expand Up @@ -53,23 +50,3 @@ char const *printChar(int c) {
buf[4] = '\0';
return buf;
}

size_t readUTF8Char(std::vector<int32_t> *dest, char const *src) {
uint32_t state = 0, codepoint;
size_t i = 0;

for (;;) {
if (decode(&state, &codepoint, src[i]) == 1) {
return 0;
}

if (dest) {
dest->push_back(src[i]);
}
i++;

if (state == 0) {
return i;
}
}
}

0 comments on commit e49291b

Please sign in to comment.