aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-01-25 15:23:27 +0100
committerMattias Andrée <m@maandree.se>2026-01-25 15:23:27 +0100
commit85a011b52dc512c6fe4b0de2d07ca113494ab10e (patch)
treeb2510af30748e9f62c6a55f8075005a53699a6d3
parentAdd vulgar fractions (diff)
downloadcharconv-85a011b52dc512c6fe4b0de2d07ca113494ab10e.tar.gz
charconv-85a011b52dc512c6fe4b0de2d07ca113494ab10e.tar.bz2
charconv-85a011b52dc512c6fe4b0de2d07ca113494ab10e.tar.xz
Add flipped
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to '')
-rw-r--r--Makefile6
-rw-r--r--convert-to-flipped.c4
-rw-r--r--libcharconv.h5
-rw-r--r--libcharconv_flipped.c59
4 files changed, 72 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index ab81ea1..55369ae 100644
--- a/Makefile
+++ b/Makefile
@@ -60,7 +60,8 @@ BIN =\
convert-to-yijing-trigrams\
convert-to-yijing-tetragrams\
convert-to-yijing-hexagrams\
- convert-to-vulgar-fractions
+ convert-to-vulgar-fractions\
+ convert-to-flipped
LIBOBJ =\
libcharconv_decode_utf8_.o\
@@ -109,7 +110,8 @@ LIBOBJ =\
libcharconv_yijing_trigrams.o\
libcharconv_yijing_tetragrams.o\
libcharconv_yijing_hexagrams.o\
- libcharconv_vulgar_fractions.o
+ libcharconv_vulgar_fractions.o\
+ libcharconv_flipped.o
LOBJ = $(LIBOBJ:.o=.lo)
diff --git a/convert-to-flipped.c b/convert-to-flipped.c
new file mode 100644
index 0000000..f3e42f9
--- /dev/null
+++ b/convert-to-flipped.c
@@ -0,0 +1,4 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+SIMPLE(libcharconv_flipped)
diff --git a/libcharconv.h b/libcharconv.h
index 402c839..4aa9d27 100644
--- a/libcharconv.h
+++ b/libcharconv.h
@@ -328,6 +328,11 @@ LIBCHARCONV_FUNC_(libcharconv_yijing_hexagrams);
*/
LIBCHARCONV_FUNC_(libcharconv_vulgar_fractions);
+/**
+ * Flip characters vertically
+ */
+LIBCHARCONV_FUNC_(libcharconv_flipped);
+
#undef LIBCHARCONV_FUNC_
#endif
diff --git a/libcharconv_flipped.c b/libcharconv_flipped.c
new file mode 100644
index 0000000..43893fc
--- /dev/null
+++ b/libcharconv_flipped.c
@@ -0,0 +1,59 @@
+/* See LICENSE file for copyright and license details. */
+#include "lib-common.h"
+
+
+static struct {
+ uint_least32_t a;
+ uint_least32_t b;
+} pairs[] = {
+ {0x00A1, 0x0021}
+};
+
+
+enum libcharconv_result
+libcharconv_flipped(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;
+}