aboutsummaryrefslogtreecommitdiffstats
path: root/libcharconv_turned.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-01-25 16:05:05 +0100
committerMattias Andrée <m@maandree.se>2026-01-25 16:05:05 +0100
commita0cd51f0e62cef118c17cfc308ff2a67bef49338 (patch)
tree3e9f0debf88f4398417e79abc7fc31a45ef5922f /libcharconv_turned.c
parentAdd mirrored (diff)
downloadcharconv-a0cd51f0e62cef118c17cfc308ff2a67bef49338.tar.gz
charconv-a0cd51f0e62cef118c17cfc308ff2a67bef49338.tar.bz2
charconv-a0cd51f0e62cef118c17cfc308ff2a67bef49338.tar.xz
Add turned
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to '')
-rw-r--r--libcharconv_turned.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/libcharconv_turned.c b/libcharconv_turned.c
new file mode 100644
index 0000000..8436305
--- /dev/null
+++ b/libcharconv_turned.c
@@ -0,0 +1,78 @@
+/* See LICENSE file for copyright and license details. */
+#include "lib-common.h"
+
+
+static struct {
+ uint_least32_t a;
+ uint_least32_t b;
+} pairs[] = {
+ {0x0032, 0x218A},
+ {0x0033, 0x218B},
+ {0x2616, 0x26C9},
+ {0x2617, 0x26CA},
+ {0x0021, 0x00A1},
+ {0x003F, 0x00BF},
+ {0x2E35, 0x003B},
+ {0x203D, 0x2E18}
+};
+
+
+enum libcharconv_result
+libcharconv_turned(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;
+ }
+
+ if (UINT32_C(0x1F031) <= c && c <= UINT32_C(0x1F061)) {
+ c -= UINT32_C(0x1F031);
+ c = c % 7u * 7u + c / 7u;
+ c += UINT32_C(0x1F031);
+ goto conv;
+ } else if (UINT32_C(0x1F063) <= c && c <= UINT32_C(0x1F093)) {
+ c -= UINT32_C(0x1F063);
+ c = c % 7u * 7u + c / 7u;
+ c += UINT32_C(0x1F063);
+ goto conv;
+ } else {
+ 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;
+}