diff options
| author | Mattias Andrée <m@maandree.se> | 2026-01-25 15:06:50 +0100 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2026-01-25 15:06:50 +0100 |
| commit | 53160a89e5d723fb8315ebb087426405973b01ad (patch) | |
| tree | 7b7b6e695e686b2e6c659a57a32bc887d00badd9 | |
| parent | Use a macro for all utils that do not take arguments (diff) | |
| download | charconv-53160a89e5d723fb8315ebb087426405973b01ad.tar.gz charconv-53160a89e5d723fb8315ebb087426405973b01ad.tar.bz2 charconv-53160a89e5d723fb8315ebb087426405973b01ad.tar.xz | |
Add vulgar fractions
Signed-off-by: Mattias Andrée <m@maandree.se>
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | convert-to-vulgar-fractions.c | 4 | ||||
| -rw-r--r-- | libcharconv.h | 6 | ||||
| -rw-r--r-- | libcharconv_latin.c | 23 | ||||
| -rw-r--r-- | libcharconv_vulgar_fractions.c | 108 |
5 files changed, 145 insertions, 2 deletions
@@ -59,7 +59,8 @@ BIN =\ convert-to-yijing-digrams\ convert-to-yijing-trigrams\ convert-to-yijing-tetragrams\ - convert-to-yijing-hexagrams + convert-to-yijing-hexagrams\ + convert-to-vulgar-fractions LIBOBJ =\ libcharconv_decode_utf8_.o\ @@ -107,7 +108,8 @@ LIBOBJ =\ libcharconv_yijing_digrams.o\ libcharconv_yijing_trigrams.o\ libcharconv_yijing_tetragrams.o\ - libcharconv_yijing_hexagrams.o + libcharconv_yijing_hexagrams.o\ + libcharconv_vulgar_fractions.o LOBJ = $(LIBOBJ:.o=.lo) diff --git a/convert-to-vulgar-fractions.c b/convert-to-vulgar-fractions.c new file mode 100644 index 0000000..e8975d4 --- /dev/null +++ b/convert-to-vulgar-fractions.c @@ -0,0 +1,4 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +SIMPLE(libcharconv_vulgar_fractions) diff --git a/libcharconv.h b/libcharconv.h index 4a7d4db..402c839 100644 --- a/libcharconv.h +++ b/libcharconv.h @@ -322,6 +322,12 @@ LIBCHARCONV_FUNC_(libcharconv_yijing_tetragrams); */ LIBCHARCONV_FUNC_(libcharconv_yijing_hexagrams); +/** + * Convert from ASCII to VULGAR FRACTIONs and FRACTION NUMERATORs, + * or from SOLIDUS to FRACTION SLASH + */ +LIBCHARCONV_FUNC_(libcharconv_vulgar_fractions); + #undef LIBCHARCONV_FUNC_ #endif diff --git a/libcharconv_latin.c b/libcharconv_latin.c index 232eb5c..c52044c 100644 --- a/libcharconv_latin.c +++ b/libcharconv_latin.c @@ -603,6 +603,29 @@ libcharconv_latin(const char *s, size_t slen, size_t *n, uint_least32_t *cp, siz case UINT32_C(0x1D304): c1 = '3'; c2 = '2'; goto conv2; case UINT32_C(0x1D305): c1 = '3'; c2 = '3'; goto conv2; + /* vulgar fractions */ + case UINT32_C(0x2044): c1 = '/'; goto conv1; + case UINT32_C(0x215F): c1 = '1'; c2 = '/'; goto conv2; + case UINT32_C(0x2189): c1 = '0'; c2 = '/'; c3 = '3'; goto conv3; + case UINT32_C(0x00BD): c1 = '1'; c2 = '/'; c3 = '2'; goto conv3; + case UINT32_C(0x2153): c1 = '1'; c2 = '/'; c3 = '3'; goto conv3; + case UINT32_C(0x00BC): c1 = '1'; c2 = '/'; c3 = '4'; goto conv3; + case UINT32_C(0x2155): c1 = '1'; c2 = '/'; c3 = '5'; goto conv3; + case UINT32_C(0x2159): c1 = '1'; c2 = '/'; c3 = '6'; goto conv3; + case UINT32_C(0x2150): c1 = '1'; c2 = '/'; c3 = '7'; goto conv3; + case UINT32_C(0x215B): c1 = '1'; c2 = '/'; c3 = '8'; goto conv3; + case UINT32_C(0x2151): c1 = '1'; c2 = '/'; c3 = '9'; goto conv3; + case UINT32_C(0x2154): c1 = '2'; c2 = '/'; c3 = '3'; goto conv3; + case UINT32_C(0x2156): c1 = '2'; c2 = '/'; c3 = '5'; goto conv3; + case UINT32_C(0x00BE): c1 = '3'; c2 = '/'; c3 = '4'; goto conv3; + case UINT32_C(0x2157): c1 = '3'; c2 = '/'; c3 = '5'; goto conv3; + case UINT32_C(0x215C): c1 = '3'; c2 = '/'; c3 = '8'; goto conv3; + case UINT32_C(0x2158): c1 = '4'; c2 = '/'; c3 = '5'; goto conv3; + case UINT32_C(0x215A): c1 = '5'; c2 = '/'; c3 = '6'; goto conv3; + case UINT32_C(0x215D): c1 = '5'; c2 = '/'; c3 = '8'; goto conv3; + case UINT32_C(0x215E): c1 = '7'; c2 = '/'; c3 = '8'; goto conv3; + case UINT32_C(0x2152): c1 = '1'; c2 = '/'; c3 = '1'; c4 = '0'; goto conv4; + default: no_match: *n += clen; diff --git a/libcharconv_vulgar_fractions.c b/libcharconv_vulgar_fractions.c new file mode 100644 index 0000000..a89a02f --- /dev/null +++ b/libcharconv_vulgar_fractions.c @@ -0,0 +1,108 @@ +/* See LICENSE file for copyright and license details. */ +#include "lib-common.h" + + +enum libcharconv_result +libcharconv_vulgar_fractions(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp) +{ + uint_least32_t c; + *n = 0; + for (; slen--; s++) { + if (s[0] == '/') { + if (*n) + goto no_conv; + c = UINT32_C(0x2044); + *n = 1u; + goto conv; + } else if (('0' <= s[0] && s[0] <= '5') || s[0] <= '7') { + if (*n) + goto no_conv; + if (slen == 0u) + return LIBCHARCONV_INDETERMINATE; + if (s[1] != '/') + goto no_match; + if (slen == 1u) { + if (s[0] == '1') { + c = UINT32_C(0x215F); + *n = 2u; + if (*ncp) + *cp = c; + *ncp = 1u; + return LIBCHARCONV_CONVERT_IF_END; + } else { + return LIBCHARCONV_INDETERMINATE; + } + } + if (s[0] == '0' && s[2] == '3') { + c = UINT32_C(0x2189); + } else if (s[0] == '2' && s[2] == '3') { + c = UINT32_C(0x2154); + } else if (s[0] == '2' && s[2] == '5') { + c = UINT32_C(0x2156); + } else if (s[0] == '3' && s[2] == '4') { + c = UINT32_C(0x00BE); + } else if (s[0] == '3' && s[2] == '5') { + c = UINT32_C(0x2157); + } else if (s[0] == '3' && s[2] == '8') { + c = UINT32_C(0x215C); + } else if (s[0] == '4' && s[2] == '5') { + c = UINT32_C(0x2158); + } else if (s[0] == '5' && s[2] == '6') { + c = UINT32_C(0x215A); + } else if (s[0] == '5' && s[2] == '8') { + c = UINT32_C(0x215D); + } else if (s[0] == '7' && s[2] == '8') { + c = UINT32_C(0x215E); + } else if (s[0] == '1' && s[2] == '2') { + c = UINT32_C(0x00BD); + } else if (s[0] == '1' && s[2] == '3') { + c = UINT32_C(0x2153); + } else if (s[0] == '1' && s[2] == '4') { + c = UINT32_C(0x00BC); + } else if (s[0] == '1' && s[2] == '5') { + c = UINT32_C(0x2155); + } else if (s[0] == '1' && s[2] == '6') { + c = UINT32_C(0x2159); + } else if (s[0] == '1' && s[2] == '7') { + c = UINT32_C(0x2150); + } else if (s[0] == '1' && s[2] == '8') { + c = UINT32_C(0x215B); + } else if (s[0] == '1' && s[2] == '9') { + c = UINT32_C(0x2151); + } else if (s[0] == '1' && s[2] == '1') { + if (slen == 2u) + return LIBCHARCONV_INDETERMINATE; + if (s[3] == '0') { + c = UINT32_C(0x2152); + *n = 4u; + if (*ncp) + *cp = c; + *ncp = 1u; + return LIBCHARCONV_CONVERTED; + } else { + goto denum_1; + } + } else if (s[0] == '1') { + denum_1: + *n = 2u; + c = UINT32_C(0x215F); + goto conv; + } else { + goto no_match; + } + *n = 3u; + goto conv; + } else { + no_match: + *n += 1u; + } + } +no_conv: + return LIBCHARCONV_NO_CONVERT; + +conv: + if (*ncp) + *cp = c; + *ncp = 1u; + return LIBCHARCONV_CONVERTED; +} |
