From b8f963edcaa3dbe4fe8160bb131a7aeb35a95f89 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 25 Jan 2026 15:47:41 +0100 Subject: Add mirrored MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libcharconv_mirrored.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 libcharconv_mirrored.c (limited to 'libcharconv_mirrored.c') diff --git a/libcharconv_mirrored.c b/libcharconv_mirrored.c new file mode 100644 index 0000000..a95f636 --- /dev/null +++ b/libcharconv_mirrored.c @@ -0,0 +1,64 @@ +/* See LICENSE file for copyright and license details. */ +#include "lib-common.h" + + +static struct { + uint_least32_t a; + uint_least32_t b; +} pairs[] = { + {0x2032, 0x2035}, + {0x2033, 0x2036}, + {0x2034, 0x2037}, + {0x204F, 0x003B}, + {0x2E2E, 0x003F}, + {0x2143, 0x004C} +}; + + +enum libcharconv_result +libcharconv_mirrored(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp) +{ + uint_least32_t c; + size_t i, clen; + *n = 0; + while (slen) { + clen = libcharconv_decode_utf8_(s, slen, &c); + if (clen > slen) { + if (*n) + goto no_conv; + return LIBCHARCONV_INDETERMINATE; + } + if (!clen) { + *n += 1u; + slen -= 1u; + s = &s[1]; + continue; + } + + for (i = 0u; i < sizeof(pairs) / sizeof(*pairs); i++) { + if (c == pairs[i].a) { + c = pairs[i].b; + goto conv; + } + if (c == pairs[i].b) { + c = pairs[i].a; + goto conv; + } + } + + *n += clen; + s = &s[clen]; + slen -= clen; + } +no_conv: + return LIBCHARCONV_NO_CONVERT; + +conv: + if (*n) + goto no_conv; + if (*ncp) + *cp = c; + *n += clen; + *ncp = 1u; + return LIBCHARCONV_CONVERTED; +} -- cgit v1.2.3-70-g09d2