shithub: rgbds

Download patch

ref: 5108c5643cbcab4463c831c44c83a18298edf4cc
parent: 2005ed1df9a6abfbb693889129af15f78569282e
author: Rangi <[email protected]>
date: Mon Mar 8 11:40:36 EST 2021

Let charmap_ConvertNext advance its output pointer

--- a/include/asm/charmap.h
+++ b/include/asm/charmap.h
@@ -18,6 +18,6 @@
 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 */
--- a/src/asm/charmap.c
+++ b/src/asm/charmap.c
@@ -193,18 +193,15 @@
 
 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,12 +235,13 @@
 
 			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");
@@ -250,6 +248,8 @@
 
 				/* OK because UTF-8 has no NUL in multi-byte chars */
 				*input += codepointLen;
+				if (output)
+					*output += codepointLen;
 
 				return codepointLen;