diff options
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | convert-to-mirrored.c | 4 | ||||
| -rw-r--r-- | libcharconv.h | 5 | ||||
| -rw-r--r-- | libcharconv_latin.c | 5 | ||||
| -rw-r--r-- | libcharconv_mirrored.c | 64 |
5 files changed, 82 insertions, 2 deletions
@@ -63,7 +63,8 @@ BIN =\ convert-to-vulgar-fractions\ convert-to-flipped\ convert-to-overlaid\ - convert-to-joined + convert-to-joined\ + convert-to-mirrored LIBOBJ =\ libcharconv_decode_utf8_.o\ @@ -115,7 +116,8 @@ LIBOBJ =\ libcharconv_vulgar_fractions.o\ libcharconv_flipped.o\ libcharconv_overlaid.o\ - libcharconv_joined.o + libcharconv_joined.o\ + libcharconv_mirrored.o LOBJ = $(LIBOBJ:.o=.lo) diff --git a/convert-to-mirrored.c b/convert-to-mirrored.c new file mode 100644 index 0000000..65b6c2b --- /dev/null +++ b/convert-to-mirrored.c @@ -0,0 +1,4 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +SIMPLE(libcharconv_mirrored) diff --git a/libcharconv.h b/libcharconv.h index 731c7ce..f5eba1e 100644 --- a/libcharconv.h +++ b/libcharconv.h @@ -343,6 +343,11 @@ LIBCHARCONV_FUNC_(libcharconv_overlaid); */ LIBCHARCONV_FUNC_(libcharconv_joined); +/** + * Mirror characters horizontally + */ +LIBCHARCONV_FUNC_(libcharconv_mirrored); + #undef LIBCHARCONV_FUNC_ #endif diff --git a/libcharconv_latin.c b/libcharconv_latin.c index a4b9059..6baf82b 100644 --- a/libcharconv_latin.c +++ b/libcharconv_latin.c @@ -636,6 +636,11 @@ libcharconv_latin(const char *s, size_t slen, size_t *n, uint_least32_t *cp, siz case UINT32_C(0x203C): c1 = '!'; c2 = '!'; goto conv2; case UINT32_C(0x2049): c1 = '!'; c2 = '?'; goto conv2; + /* mirrored */ + case UINT32_C(0x204F): c = UINT32_C(0x003B); goto conv; + case UINT32_C(0x2E2E): c = UINT32_C(0x003F); goto conv; + case UINT32_C(0x2143): c = UINT32_C(0x004C); goto conv; + default: no_match: *n += clen; 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; +} |
