diff options
Diffstat (limited to 'zap/source/any/str')
-rw-r--r-- | zap/source/any/str/fmt.cc | 43 | ||||
-rw-r--r-- | zap/source/any/str/fmtlen.cc | 37 | ||||
-rw-r--r-- | zap/source/any/str/numdig.hh | 12 | ||||
-rw-r--r-- | zap/source/any/str/streq.c | 17 | ||||
-rw-r--r-- | zap/source/any/str/strlen.c | 13 | ||||
-rw-r--r-- | zap/source/any/str/utf8dec.c | 47 | ||||
-rw-r--r-- | zap/source/any/str/utf8declen.c | 29 | ||||
-rw-r--r-- | zap/source/any/str/utf8enc.c | 44 | ||||
-rw-r--r-- | zap/source/any/str/utf8enclen.c | 29 | ||||
-rw-r--r-- | zap/source/any/str/win1252dec.c | 107 | ||||
-rw-r--r-- | zap/source/any/str/win1252enc.c | 109 |
11 files changed, 487 insertions, 0 deletions
diff --git a/zap/source/any/str/fmt.cc b/zap/source/any/str/fmt.cc new file mode 100644 index 0000000..b2c0c75 --- /dev/null +++ b/zap/source/any/str/fmt.cc @@ -0,0 +1,43 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +#include "numdig.hh" + +namespace zap { + namespace impl { + template<typename signtyp> zap_priv_inln inline static auto fmt(::zap::i02 * buf,signtyp signval,::zap::i8 const bs,::zap::i8 const rtl) noexcept -> void { + using typ = typename ::zap::usign<signtyp>; + char32_t const * digs = U"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + if (bs == 0xCu) digs = U"0123456789\u218A\u218B"; + if (signval < 0x0) { + signval = -signval; + *buf++ = 0x2212u; + } + typ val = static_cast<typ>(signval); + if (rtl) { + buf += ::zap::impl::numdig(val,bs) - 0x1u; + for (;val > 0x0u;val /= bs) *buf-- = static_cast<::zap::i02>(digs[static_cast<::zap::sz>(val % static_cast<typ>(bs))]); + return; + } + for (;val > 0x0u;val /= bs) *buf++ = static_cast<::zap::i02>(digs[static_cast<::zap::sz>(val % static_cast<typ>(bs))]); + } + } +} + +extern "C" { + auto zap_fmti( ::zap::i02 * const buf,int const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtl( ::zap::i02 * const buf,long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtll( ::zap::i02 * const buf,long long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmts( ::zap::i02 * const buf,short const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtsc( ::zap::i02 * const buf,signed char const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtuc( ::zap::i02 * const buf,unsigned char const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtui( ::zap::i02 * const buf,unsigned int const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtul( ::zap::i02 * const buf,unsigned long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtull(::zap::i02 * const buf,unsigned long long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtus( ::zap::i02 * const buf,unsigned short const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} +} diff --git a/zap/source/any/str/fmtlen.cc b/zap/source/any/str/fmtlen.cc new file mode 100644 index 0000000..866d011 --- /dev/null +++ b/zap/source/any/str/fmtlen.cc @@ -0,0 +1,37 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/math.h> +#include <zap/str.h> + +#include "numdig.hh" + +namespace zap { + namespace impl { + template<typename typ> zap_priv_inln inline static auto fmtlen(typ val,::zap::i8 const bs) noexcept -> ::zap::i8 { + ::zap::i8 len = 0x0u; + if (val < 0x0) { + val = static_cast<typ>(::zap::abs(val)); + len = 0x1u; + } + len += ::zap::impl::numdig(val,bs); + return len; + } + } +} + +extern "C" { + auto zap_fmtleni( int const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenl( long const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenll( long long const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlens( short const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlensc( signed char const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenuc( unsigned char const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenui( unsigned int const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenul( unsigned long const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenull(unsigned long long const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenus( unsigned short const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} +} diff --git a/zap/source/any/str/numdig.hh b/zap/source/any/str/numdig.hh new file mode 100644 index 0000000..65756b9 --- /dev/null +++ b/zap/source/any/str/numdig.hh @@ -0,0 +1,12 @@ +#pragma once + +namespace zap { + namespace impl { + template<typename typ> zap_priv_inln inline static auto numdig(typ fmtval,::zap::i8 const bs) noexcept -> ::zap::i8 { + ::zap::i8 len = 0x0u; + if (fmtval == typ {0x0}) return 0x1u; + for (typ val = fmtval;val > 0x0;val /= static_cast<typ>(bs)) ++len; + return len; + } + } +} diff --git a/zap/source/any/str/streq.c b/zap/source/any/str/streq.c new file mode 100644 index 0000000..6b1002d --- /dev/null +++ b/zap/source/any/str/streq.c @@ -0,0 +1,17 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +zap_i8 zap_streq(char const * lstr,char const * rstr) { + for (;;++lstr,++rstr) { + char const lchr = *lstr; + char const rchr = *rstr; + if (lchr != rchr) return 0x0u; + if (lchr == '\x0') break; + } + return zap_maxval8; +} diff --git a/zap/source/any/str/strlen.c b/zap/source/any/str/strlen.c new file mode 100644 index 0000000..e6e9019 --- /dev/null +++ b/zap/source/any/str/strlen.c @@ -0,0 +1,13 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +zap_sz zap_strlen(char const * str) { + char const * const start = str; + while (*str++); + return (zap_sz)(str - start) - 0x1u; +} diff --git a/zap/source/any/str/utf8dec.c b/zap/source/any/str/utf8dec.c new file mode 100644 index 0000000..add8697 --- /dev/null +++ b/zap/source/any/str/utf8dec.c @@ -0,0 +1,47 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +void zap_utf8dec(zap_i02 * dest,zap_i8 const * src) { + for (;;++dest) { + zap_i8 const oct = *src; + if (oct == 0x0u) break; + if (oct >= 0xF0u) { /* Four octets. */ + zap_i02 chr = ((zap_i02)oct ^ 0xF0u) << 0x12u; + ++src; + chr += ((zap_i02)*src ^ 0x80u) << 0xCu; + ++src; + chr += ((zap_i02)*src ^ 0x80u) << 0x6u; + ++src; + chr += (zap_i02)*src ^ 0x80u; + ++src; + *dest = chr; + continue; + } + if (oct >= 0xE0u) { /* Three octets. */ + zap_i02 chr = ((zap_i02)oct ^ 0xE0u) << 0xCu; + ++src; + chr += ((zap_i02)*src ^ 0x80u) << 0x6u; + ++src; + chr += (zap_i02)*src ^ 0x80u; + ++src; + *dest = chr; + continue; + } + if (oct >= 0xC0u) { /* Two octets. */ + zap_i02 chr = ((zap_i02)oct ^ 0xC0u) << 0x6u; + ++src; + chr += (zap_i02)*src ^ 0x80u; + ++src; + *dest = chr; + continue; + } + /* One octet. */ + *dest = oct; + ++src; + } +} diff --git a/zap/source/any/str/utf8declen.c b/zap/source/any/str/utf8declen.c new file mode 100644 index 0000000..f8b7ae8 --- /dev/null +++ b/zap/source/any/str/utf8declen.c @@ -0,0 +1,29 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +zap_sz zap_utf8declen(zap_i8 const * buf) { + zap_sz len = 0x0u; + for (;;++len) { + zap_i8 const oct = *buf; + if (oct == 0x0u) break; + if (oct >= 0xF0u) { + buf += 0x4u; + continue; + } + if (oct >= 0xE0u) { + buf += 0x3u; + continue; + } + if (oct >= 0xC0u) { + buf += 0x2u; + continue; + } + ++buf; + } + return len; +} diff --git a/zap/source/any/str/utf8enc.c b/zap/source/any/str/utf8enc.c new file mode 100644 index 0000000..a6b31c6 --- /dev/null +++ b/zap/source/any/str/utf8enc.c @@ -0,0 +1,44 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +void zap_utf8enc(zap_i8 * dest,zap_i02 const * src) { + for (;;++src) { + zap_i02 const chr = *src; + if (chr > 0xFFFFu) { /* Four octets. */ + *dest = 0xF0u + (chr >> 0x12u); + ++dest; + *dest = 0x80u + (chr >> 0xCu & 0x3Fu); + ++dest; + *dest = 0x80u + (chr >> 0x6u & 0x3Fu); + ++dest; + *dest = 0x80u + (chr & 0x3Fu); + ++dest; + continue; + } + if (chr >= 0x7FFu) { /* Three octets. */ + *dest = 0xE0u + (zap_i8)(chr >> 0xCu); + ++dest; + *dest = 0x80u + (zap_i8)(chr >> 0x6u & 0x3Fu); + ++dest; + *dest = 0x80u + (zap_i8)(chr & 0x3Fu); + ++dest; + continue; + } + if (chr >= 0x7Fu) { /* Two octets. */ + *dest = 0xC0u + (zap_i8)(chr >> 0x6u); + ++dest; + *dest = 0x80u + (zap_i8)(chr & 0x3Fu); + ++dest; + continue; + } + /* One octet. */ + *dest = chr; + ++dest; + if (chr == 0x0u) break; + } +} diff --git a/zap/source/any/str/utf8enclen.c b/zap/source/any/str/utf8enclen.c new file mode 100644 index 0000000..f49031e --- /dev/null +++ b/zap/source/any/str/utf8enclen.c @@ -0,0 +1,29 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +zap_sz zap_utf8enclen(zap_i02 const * buf) { + zap_sz len = 0x0u; + for (;;++buf) { + zap_i02 const chr = *buf; + if (chr == 0x0u) break; + if (chr >= 0x10000u) { + len += 0x4u; + continue; + } + if (chr >= 0x800u) { + len += 0x3u; + continue; + } + if (chr >= 0x80u) { + len += 0x2u; + continue; + } + ++len; + } + return len; +} diff --git a/zap/source/any/str/win1252dec.c b/zap/source/any/str/win1252dec.c new file mode 100644 index 0000000..0e29a5a --- /dev/null +++ b/zap/source/any/str/win1252dec.c @@ -0,0 +1,107 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +void zap_win1252dec(zap_i02 * dest,zap_i8 const * src) { + for (;;++src,++dest) { + zap_i8 const chr = *src; + if (chr == 0x0u) break; + switch (chr) { + default: + *dest = *src; + break; + case 0x81: /* Bad characters. */ + case 0x8D: + case 0x8F: + case 0x90: + case 0x9D: + *dest = 0xFFFDu; /* REPLACEMENT CHARACTER */ + break; + case 0x80: + *dest = 0x20ACu; + break; + case 0x82: + *dest = 0x201Au; + break; + case 0x83: + *dest = 0x192u; + break; + case 0x84: + *dest = 0x201Eu; + break; + case 0x85: + *dest = 0x2026u; + break; + case 0x86: + *dest = 0x2020u; + break; + case 0x87: + *dest = 0x2021u; + break; + case 0x88: + *dest = 0x2C6u; + break; + case 0x89: + *dest = 0x2030u; + break; + case 0x8A: + *dest = 0x160u; + break; + case 0x8B: + *dest = 0x2039u; + break; + case 0x8C: + *dest = 0x152u; + break; + case 0x8E: + *dest = 0x17Du; + break; + case 0x91: + *dest = 0x2018u; + break; + case 0x92: + *dest = 0x2019u; + break; + case 0x93: + *dest = 0x201Cu; + break; + case 0x94: + *dest = 0x201Du; + break; + case 0x95: + *dest = 0x2022u; + break; + case 0x96: + *dest = 0x2013u; + break; + case 0x97: + *dest = 0x2014u; + break; + case 0x98: + *dest = 0x2DCu; + break; + case 0x99: + *dest = 0x2122u; + break; + case 0x9A: + *dest = 0x161u; + break; + case 0x9B: + *dest = 0x203Au; + break; + case 0x9C: + *dest = 0x153u; + break; + case 0x9E: + *dest = 0x17Eu; + break; + case 0x9F: + *dest = 0x178u; + break; + } + } +} diff --git a/zap/source/any/str/win1252enc.c b/zap/source/any/str/win1252enc.c new file mode 100644 index 0000000..d7ae548 --- /dev/null +++ b/zap/source/any/str/win1252enc.c @@ -0,0 +1,109 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +void zap_win1252enc(zap_i8 * dest,zap_i02 const * src) { + for (;;++src,++dest) { + zap_i02 const chr = *src; + if (chr == 0x0u) break; + zap_i8 const bad = 0x3Fu; + switch (chr) { + default: + if (chr > 0xFFu) { + *dest = bad; + break; + } + if (chr >= 0x80u && chr <= 0x9Fu) { + *dest = bad; + break; + } + *dest = *src; + break; + case 0x20ACu: + *dest = 0x80u; + break; + case 0x201Au: + *dest = 0x82u; + break; + case 0x192u: + *dest = 0x83u; + break; + case 0x201Eu: + *dest = 0x84u; + break; + case 0x2026u: + *dest = 0x85u; + break; + case 0x2020u: + *dest = 0x86u; + break; + case 0x2021u: + *dest = 0x87u; + break; + case 0x2C6u: + *dest = 0x88u; + break; + case 0x2030u: + *dest = 0x89u; + break; + case 0x160u: + *dest = 0x8Au; + break; + case 0x2039u: + *dest = 0x8Bu; + break; + case 0x152u: + *dest = 0x8Cu; + break; + case 0x17Du: + *dest = 0x8Eu; + break; + case 0x2018u: + *dest = 0x91u; + break; + case 0x2019u: + *dest = 0x92u; + break; + case 0x201Cu: + *dest = 0x93u; + break; + case 0x201Du: + *dest = 0x94u; + break; + case 0x2022u: + *dest = 0x95u; + break; + case 0x2013u: + *dest = 0x96u; + break; + case 0x2014u: + *dest = 0x97u; + break; + case 0x2DCu: + *dest = 0x98u; + break; + case 0x2122u: + *dest = 0x99u; + break; + case 0x161u: + *dest = 0x9Au; + break; + case 0x203Au: + *dest = 0x9Bu; + break; + case 0x153u: + *dest = 0x9Cu; + break; + case 0x17Eu: + *dest = 0x9Eu; + break; + case 0x178u: + *dest = 0x9Fu; + break; + } + } +} |