aboutsummaryrefslogtreecommitdiffstats
path: root/libcharconv_negative.c
diff options
context:
space:
mode:
Diffstat (limited to 'libcharconv_negative.c')
-rw-r--r--libcharconv_negative.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/libcharconv_negative.c b/libcharconv_negative.c
new file mode 100644
index 0000000..dfdc353
--- /dev/null
+++ b/libcharconv_negative.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[] = {
+ {UINT32_C(0x0020), UINT32_C(0x2588)},
+ {UINT32_C(0x1FBA3), UINT32_C(0x1FBBE)},
+ {UINT32_C(0x1FBAE), UINT32_C(0x1FBBF)},
+ {UINT32_C(0x1FBBD), UINT32_C(0x2573)},
+ {UINT32_C(0x1F030), UINT32_C(0x1F031)},
+ {UINT32_C(0x1F062), UINT32_C(0x1F063)},
+ {UINT32_C(0x2686), UINT32_C(0x2688)},
+ {UINT32_C(0x2687), UINT32_C(0x2689)},
+ {UINT32_C(0x2616), UINT32_C(0x26C9)},
+ {UINT32_C(0x2617), UINT32_C(0x26CA)},
+ {UINT32_C(0x26C0), UINT32_C(0x26C2)},
+ {UINT32_C(0x26C1), UINT32_C(0x26C3)}
+};
+
+
+enum libcharconv_result
+libcharconv_negative(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(0x1FA60) <= c && c <= UINT32_C(0x1FA66)) {
+ c += 7u;
+ goto conv;
+ } else if (UINT32_C(0x1FA67) <= c && c <= UINT32_C(0x1FA6D)) {
+ c -= 7u;
+ 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;
+}