aboutsummaryrefslogtreecommitdiffstats
path: root/libcharconv_mirrored.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-01-25 15:47:41 +0100
committerMattias Andrée <m@maandree.se>2026-01-25 15:47:41 +0100
commitb8f963edcaa3dbe4fe8160bb131a7aeb35a95f89 (patch)
tree53302506a90fac28a0c336adc2c445d96388e3ab /libcharconv_mirrored.c
parentAdd joined (diff)
downloadcharconv-b8f963edcaa3dbe4fe8160bb131a7aeb35a95f89.tar.gz
charconv-b8f963edcaa3dbe4fe8160bb131a7aeb35a95f89.tar.bz2
charconv-b8f963edcaa3dbe4fe8160bb131a7aeb35a95f89.tar.xz
Add mirrored
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'libcharconv_mirrored.c')
-rw-r--r--libcharconv_mirrored.c64
1 files changed, 64 insertions, 0 deletions
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;
+}