Skip to content

Commit

Permalink
Let charmap_ConvertNext advance its output pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Apr 17, 2021
1 parent 2005ed1 commit 5108c56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/asm/charmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
20 changes: 10 additions & 10 deletions src/asm/charmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 5108c56

Please sign in to comment.