From 5108c5643cbcab4463c831c44c83a18298edf4cc Mon Sep 17 00:00:00 2001 From: Rangi Date: Mon, 8 Mar 2021 16:40:36 -0500 Subject: [PATCH] Let charmap_ConvertNext advance its output pointer --- include/asm/charmap.h | 2 +- src/asm/charmap.c | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/asm/charmap.h b/include/asm/charmap.h index b6b14b912..015b34130 100644 --- a/include/asm/charmap.h +++ b/include/asm/charmap.h @@ -18,6 +18,6 @@ void charmap_Push(void); void charmap_Pop(void); void charmap_Add(char *mapping, uint8_t value); size_t charmap_Convert(char const *input, uint8_t *output); -size_t charmap_ConvertNext(char const **input, uint8_t *output); +size_t charmap_ConvertNext(char const **input, uint8_t **output); #endif /* RGBDS_ASM_CHARMAP_H */ diff --git a/src/asm/charmap.c b/src/asm/charmap.c index 12cc3c404..941c68824 100644 --- a/src/asm/charmap.c +++ b/src/asm/charmap.c @@ -193,18 +193,15 @@ void charmap_Add(char *mapping, uint8_t value) size_t charmap_Convert(char const *input, uint8_t *output) { - size_t outputLen = 0; + uint8_t *start = output; - for (size_t charLen = charmap_ConvertNext(&input, output); charLen; - charLen = charmap_ConvertNext(&input, output)) { - output += charLen; - outputLen += charLen; - } + while (charmap_ConvertNext(&input, &output)) + ; - return outputLen; + return output - start; } -size_t charmap_ConvertNext(char const **input, uint8_t *output) +size_t charmap_ConvertNext(char const **input, uint8_t **output) { /* * The goal is to match the longest mapping possible. @@ -238,18 +235,21 @@ size_t charmap_ConvertNext(char const **input, uint8_t *output) if (match) { /* Arrived at a dead end with a match found */ if (output) - *output = match->value; + *(*output)++ = match->value; return 1; } else if (**input) { /* No match found */ - size_t codepointLen = readUTF8Char(output, *input); + size_t codepointLen = readUTF8Char(output ? *output : NULL, + *input); if (codepointLen == 0) error("Input string is not valid UTF-8!\n"); /* OK because UTF-8 has no NUL in multi-byte chars */ *input += codepointLen; + if (output) + *output += codepointLen; return codepointLen;