diff options
| author | Mattias Andrée <m@maandree.se> | 2026-01-25 17:34:21 +0100 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2026-01-25 17:34:21 +0100 |
| commit | 518637ac5a94bd2abd1f5ee77a6d116b502a9934 (patch) | |
| tree | 32f8c57e2f8221d71000c8b3c950cd65041cd903 | |
| parent | Add tally marks and ideographic tally marks (diff) | |
| download | charconv-518637ac5a94bd2abd1f5ee77a6d116b502a9934.tar.gz charconv-518637ac5a94bd2abd1f5ee77a6d116b502a9934.tar.bz2 charconv-518637ac5a94bd2abd1f5ee77a6d116b502a9934.tar.xz | |
Add negative
Signed-off-by: Mattias Andrée <m@maandree.se>
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | convert-to-negative.c | 4 | ||||
| -rw-r--r-- | libcharconv.h | 5 | ||||
| -rw-r--r-- | libcharconv_negative.c | 78 |
4 files changed, 91 insertions, 2 deletions
@@ -69,7 +69,8 @@ BIN =\ convert-to-transposed\ convert-to-sora-sompeng\ convert-to-tally-marks\ - convert-to-ideographic-tally-marks + convert-to-ideographic-tally-marks\ + convert-to-negative LIBOBJ =\ libcharconv_decode_utf8_.o\ @@ -127,7 +128,8 @@ LIBOBJ =\ libcharconv_transposed.o\ libcharconv_sora_sompeng.o\ libcharconv_tally_marks.o\ - libcharconv_ideographic_tally_marks.o + libcharconv_ideographic_tally_marks.o\ + libcharconv_negative.o LOBJ = $(LIBOBJ:.o=.lo) diff --git a/convert-to-negative.c b/convert-to-negative.c new file mode 100644 index 0000000..c64a117 --- /dev/null +++ b/convert-to-negative.c @@ -0,0 +1,4 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +SIMPLE(libcharconv_negative) diff --git a/libcharconv.h b/libcharconv.h index 5b032ee..966b8ec 100644 --- a/libcharconv.h +++ b/libcharconv.h @@ -373,6 +373,11 @@ LIBCHARCONV_FUNC_(libcharconv_tally_marks); */ LIBCHARCONV_FUNC_(libcharconv_ideographic_tally_marks); +/** + * Convert to opposite colour (between filled and outlined) + */ +LIBCHARCONV_FUNC_(libcharconv_negative); + #undef LIBCHARCONV_FUNC_ #endif 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; +} |
