diff --git a/hio/bin/t01.c b/hio/bin/t01.c index 4e9c954..f81bc2b 100644 --- a/hio/bin/t01.c +++ b/hio/bin/t01.c @@ -41,6 +41,7 @@ #include #include #include +#include #include @@ -691,7 +692,7 @@ int z = 0; z++; if ((y % 2) && (z >5)) { - write (iop->wfd, 0, HIO_NULL); + write (iop->wfd, HIO_NULL, 0); break; } } diff --git a/hio/lib/Makefile.am b/hio/lib/Makefile.am index c55d6d5..d73bcab 100644 --- a/hio/lib/Makefile.am +++ b/hio/lib/Makefile.am @@ -9,12 +9,12 @@ CFLAGS_ALL_COMMON = LDFLAGS_ALL_COMMON = -L$(abs_builddir) -L$(libdir) EXTRA_DIST = \ - hio-utl.h.m4 \ + hio-str.h.m4 \ utl-str.c.m4 \ utl-str.m4 BUILT_SOURCES = \ - hio-utl.h \ + hio-str.h \ utl-str.c ################################################## @@ -58,6 +58,7 @@ include_HEADERS = \ hio-sck.h \ hio-shw.h \ hio-skad.h \ + hio-str.h \ hio-thr.h \ hio-upac.h \ hio-utl.h \ @@ -144,8 +145,8 @@ install-data-hook: uninstall-hook: @rm -f "$(DESTDIR)$(includedir)/hio-cfg.h" -hio-utl.h: utl-str.m4 hio-utl.h.m4 - m4 -I$(srcdir) $(srcdir)/hio-utl.h.m4 > $(srcdir)/hio-utl.h +hio-str.h: utl-str.m4 hio-str.h.m4 + m4 -I$(srcdir) $(srcdir)/hio-str.h.m4 > $(srcdir)/hio-str.h utl-str.c: utl-str.m4 utl-str.c.m4 m4 -I$(srcdir) $(srcdir)/utl-str.c.m4 > $(srcdir)/utl-str.c diff --git a/hio/lib/Makefile.in b/hio/lib/Makefile.in index e304ded..459e1f1 100644 --- a/hio/lib/Makefile.in +++ b/hio/lib/Makefile.in @@ -256,8 +256,8 @@ am__include_HEADERS_DIST = hio-chr.h hio-cmn.h hio-dhcp.h hio-dns.h \ hio-ecs.h hio-fmt.h hio-htb.h hio-htrd.h hio-htre.h hio-http.h \ hio-json.h hio-md5.h hio-nwif.h hio-opt.h hio-pac1.h \ hio-path.h hio-pipe.h hio-pro.h hio-pty.h hio-rad.h hio-sck.h \ - hio-shw.h hio-skad.h hio-thr.h hio-upac.h hio-utl.h hio.h \ - hio-mar.h + hio-shw.h hio-skad.h hio-str.h hio-thr.h hio-upac.h hio-utl.h \ + hio.h hio-mar.h HEADERS = $(include_HEADERS) am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \ hio-cfg.h.in @@ -430,12 +430,12 @@ CPPFLAGS_ALL_COMMON = \ CFLAGS_ALL_COMMON = LDFLAGS_ALL_COMMON = -L$(abs_builddir) -L$(libdir) EXTRA_DIST = \ - hio-utl.h.m4 \ + hio-str.h.m4 \ utl-str.c.m4 \ utl-str.m4 BUILT_SOURCES = \ - hio-utl.h \ + hio-str.h \ utl-str.c @@ -459,8 +459,8 @@ include_HEADERS = hio-chr.h hio-cmn.h hio-dhcp.h hio-dns.h hio-ecs.h \ hio-fmt.h hio-htb.h hio-htrd.h hio-htre.h hio-http.h \ hio-json.h hio-md5.h hio-nwif.h hio-opt.h hio-pac1.h \ hio-path.h hio-pipe.h hio-pro.h hio-pty.h hio-rad.h hio-sck.h \ - hio-shw.h hio-skad.h hio-thr.h hio-upac.h hio-utl.h hio.h \ - $(am__append_1) + hio-shw.h hio-skad.h hio-str.h hio-thr.h hio-upac.h hio-utl.h \ + hio.h $(am__append_1) lib_LTLIBRARIES = libhio.la libhio_la_SOURCES = chr.c dhcp-svr.c dns.c dns-cli.c ecs.c ecs-imp.h \ err.c fmt.c fmt-imp.h htb.c htrd.c htre.c http.c http-cgi.c \ @@ -1286,8 +1286,8 @@ install-data-hook: uninstall-hook: @rm -f "$(DESTDIR)$(includedir)/hio-cfg.h" -hio-utl.h: utl-str.m4 hio-utl.h.m4 - m4 -I$(srcdir) $(srcdir)/hio-utl.h.m4 > $(srcdir)/hio-utl.h +hio-str.h: utl-str.m4 hio-str.h.m4 + m4 -I$(srcdir) $(srcdir)/hio-str.h.m4 > $(srcdir)/hio-str.h utl-str.c: utl-str.m4 utl-str.c.m4 m4 -I$(srcdir) $(srcdir)/utl-str.c.m4 > $(srcdir)/utl-str.c diff --git a/hio/lib/hio-str.h.m4 b/hio/lib/hio-str.h.m4 new file mode 100644 index 0000000..c15dd0b --- /dev/null +++ b/hio/lib/hio-str.h.m4 @@ -0,0 +1,709 @@ +/* + Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Do NOT edit hio-str.h. Edit hio-str.h.m4 instead. + * + * Generate hio-str.h.m4 with m4 + * $ m4 hio-str.h.m4 > hio-str.h + */ + +#ifndef _HIO_STR_H_ +#define _HIO_STR_H_ + +#include + +dnl --------------------------------------------------------------------------- +dnl include utl-str.m4 for c++ template functions far below +include(`utl-str.m4') +dnl --------------------------------------------------------------------------- + +/* ========================================================================= + * STRING + * ========================================================================= */ + +enum hio_trim_oochars_flag_t +{ + HIO_TRIM_OOCHARS_LEFT = (1 << 0), /**< trim leading spaces */ +#define HIO_TRIM_OOCHARS_LEFT HIO_TRIM_OOCHARS_LEFT +#define HIO_TRIM_UCHARS_LEFT HIO_TRIM_OOCHARS_LEFT +#define HIO_TRIM_BCHARS_LEFT HIO_TRIM_OOCHARS_LEFT + HIO_TRIM_OOCHARS_RIGHT = (1 << 1) /**< trim trailing spaces */ +#define HIO_TRIM_OOCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT +#define HIO_TRIM_UCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT +#define HIO_TRIM_BCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT +}; + +#if defined(__cplusplus) +extern "C" { +#endif + + +/* ------------------------------------ */ + +HIO_EXPORT int hio_comp_uchars ( + const hio_uch_t* str1, + hio_oow_t len1, + const hio_uch_t* str2, + hio_oow_t len2, + int ignorecase +); + +HIO_EXPORT int hio_comp_bchars ( + const hio_bch_t* str1, + hio_oow_t len1, + const hio_bch_t* str2, + hio_oow_t len2, + int ignorecase +); + +HIO_EXPORT int hio_comp_ucstr ( + const hio_uch_t* str1, + const hio_uch_t* str2, + int ignorecase +); + +HIO_EXPORT int hio_comp_bcstr ( + const hio_bch_t* str1, + const hio_bch_t* str2, + int ignorecase +); + +HIO_EXPORT int hio_comp_ucstr_limited ( + const hio_uch_t* str1, + const hio_uch_t* str2, + hio_oow_t maxlen, + int ignorecase +); + +HIO_EXPORT int hio_comp_bcstr_limited ( + const hio_bch_t* str1, + const hio_bch_t* str2, + hio_oow_t maxlen, + int ignorecase +); + +HIO_EXPORT int hio_comp_uchars_ucstr ( + const hio_uch_t* str1, + hio_oow_t len, + const hio_uch_t* str2, + int ignorecase +); + +HIO_EXPORT int hio_comp_bchars_bcstr ( + const hio_bch_t* str1, + hio_oow_t len, + const hio_bch_t* str2, + int ignorecase +); + +/* ------------------------------------ */ + +HIO_EXPORT hio_oow_t hio_concat_uchars_to_ucstr ( + hio_uch_t* buf, + hio_oow_t bsz, + const hio_uch_t* src, + hio_oow_t len +); + +HIO_EXPORT hio_oow_t hio_concat_bchars_to_bcstr ( + hio_bch_t* buf, + hio_oow_t bsz, + const hio_bch_t* src, + hio_oow_t len +); + +HIO_EXPORT hio_oow_t hio_concat_ucstr ( + hio_uch_t* buf, + hio_oow_t bsz, + const hio_uch_t* src +); + +HIO_EXPORT hio_oow_t hio_concat_bcstr ( + hio_bch_t* buf, + hio_oow_t bsz, + const hio_bch_t* src +); + +/* ------------------------------------ */ + +HIO_EXPORT void hio_copy_uchars ( + hio_uch_t* dst, + const hio_uch_t* src, + hio_oow_t len +); + +HIO_EXPORT void hio_copy_bchars ( + hio_bch_t* dst, + const hio_bch_t* src, + hio_oow_t len +); + +HIO_EXPORT void hio_copy_bchars_to_uchars ( + hio_uch_t* dst, + const hio_bch_t* src, + hio_oow_t len +); +HIO_EXPORT void hio_copy_uchars_to_bchars ( + hio_bch_t* dst, + const hio_uch_t* src, + hio_oow_t len +); + +HIO_EXPORT hio_oow_t hio_copy_uchars_to_ucstr ( + hio_uch_t* dst, + hio_oow_t dlen, + const hio_uch_t* src, + hio_oow_t slen +); + +HIO_EXPORT hio_oow_t hio_copy_bchars_to_bcstr ( + hio_bch_t* dst, + hio_oow_t dlen, + const hio_bch_t* src, + hio_oow_t slen +); + +HIO_EXPORT hio_oow_t hio_copy_uchars_to_ucstr_unlimited ( + hio_uch_t* dst, + const hio_uch_t* src, + hio_oow_t len +); + +HIO_EXPORT hio_oow_t hio_copy_bchars_to_bcstr_unlimited ( + hio_bch_t* dst, + const hio_bch_t* src, + hio_oow_t len +); + +HIO_EXPORT hio_oow_t hio_copy_ucstr_to_uchars ( + hio_uch_t* dst, + hio_oow_t dlen, + const hio_uch_t* src +); + +HIO_EXPORT hio_oow_t hio_copy_bcstr_to_bchars ( + hio_bch_t* dst, + hio_oow_t dlen, + const hio_bch_t* src +); + +HIO_EXPORT hio_oow_t hio_copy_ucstr ( + hio_uch_t* dst, + hio_oow_t len, + const hio_uch_t* src +); + +HIO_EXPORT hio_oow_t hio_copy_bcstr ( + hio_bch_t* dst, + hio_oow_t len, + const hio_bch_t* src +); + +HIO_EXPORT hio_oow_t hio_copy_ucstr_unlimited ( + hio_uch_t* dst, + const hio_uch_t* src +); + +HIO_EXPORT hio_oow_t hio_copy_bcstr_unlimited ( + hio_bch_t* dst, + const hio_bch_t* src +); + +HIO_EXPORT hio_oow_t hio_copy_fmt_ucstrs_to_ucstr ( + hio_uch_t* buf, + hio_oow_t bsz, + const hio_uch_t* fmt, + const hio_uch_t* str[] +); + +HIO_EXPORT hio_oow_t hio_copy_fmt_bcstrs_to_bcstr ( + hio_bch_t* buf, + hio_oow_t bsz, + const hio_bch_t* fmt, + const hio_bch_t* str[] +); + +HIO_EXPORT hio_oow_t hio_copy_fmt_ucses_to_ucstr ( + hio_uch_t* buf, + hio_oow_t bsz, + const hio_uch_t* fmt, + const hio_ucs_t str[] +); + +HIO_EXPORT hio_oow_t hio_copy_fmt_bcses_to_bcstr ( + hio_bch_t* buf, + hio_oow_t bsz, + const hio_bch_t* fmt, + const hio_bcs_t str[] +); + +/* ------------------------------------ */ + +HIO_EXPORT hio_oow_t hio_count_ucstr ( + const hio_uch_t* str +); + +HIO_EXPORT hio_oow_t hio_count_bcstr ( + const hio_bch_t* str +); + +HIO_EXPORT hio_oow_t hio_count_ucstr_limited ( + const hio_uch_t* str, + hio_oow_t maxlen +); + +HIO_EXPORT hio_oow_t hio_count_bcstr_limited ( + const hio_bch_t* str, + hio_oow_t maxlen +); + +/* ------------------------------------ */ + +/** + * The hio_equal_uchars() function determines equality of two strings + * of the same length \a len. + */ +HIO_EXPORT int hio_equal_uchars ( + const hio_uch_t* str1, + const hio_uch_t* str2, + hio_oow_t len +); + +HIO_EXPORT int hio_equal_bchars ( + const hio_bch_t* str1, + const hio_bch_t* str2, + hio_oow_t len +); + +/* ------------------------------------ */ + + +HIO_EXPORT void hio_fill_uchars ( + hio_uch_t* dst, + const hio_uch_t ch, + hio_oow_t len +); + +HIO_EXPORT void hio_fill_bchars ( + hio_bch_t* dst, + const hio_bch_t ch, + hio_oow_t len +); + +/* ------------------------------------ */ + +HIO_EXPORT const hio_bch_t* hio_find_bcstr_word_in_bcstr ( + const hio_bch_t* str, + const hio_bch_t* word, + hio_bch_t extra_delim, + int ignorecase +); + +HIO_EXPORT const hio_uch_t* hio_find_ucstr_word_in_ucstr ( + const hio_uch_t* str, + const hio_uch_t* word, + hio_uch_t extra_delim, + int ignorecase +); + +HIO_EXPORT hio_uch_t* hio_find_uchar_in_uchars ( + const hio_uch_t* ptr, + hio_oow_t len, + hio_uch_t c +); + +HIO_EXPORT hio_bch_t* hio_find_bchar_in_bchars ( + const hio_bch_t* ptr, + hio_oow_t len, + hio_bch_t c +); + +HIO_EXPORT hio_uch_t* hio_rfind_uchar_in_uchars ( + const hio_uch_t* ptr, + hio_oow_t len, + hio_uch_t c +); + +HIO_EXPORT hio_bch_t* hio_rfind_bchar_in_bchars ( + const hio_bch_t* ptr, + hio_oow_t len, + hio_bch_t c +); + +HIO_EXPORT hio_uch_t* hio_find_uchar_in_ucstr ( + const hio_uch_t* ptr, + hio_uch_t c +); + +HIO_EXPORT hio_bch_t* hio_find_bchar_in_bcstr ( + const hio_bch_t* ptr, + hio_bch_t c +); + +HIO_EXPORT hio_uch_t* hio_rfind_uchar_in_ucstr ( + const hio_uch_t* ptr, + hio_uch_t c +); + +HIO_EXPORT hio_bch_t* hio_rfind_bchar_in_bcstr ( + const hio_bch_t* ptr, + hio_bch_t c +); + +HIO_EXPORT hio_uch_t* hio_find_uchars_in_uchars ( + const hio_uch_t* str, + hio_oow_t strsz, + const hio_uch_t* sub, + hio_oow_t subsz, + int inorecase +); + +HIO_EXPORT hio_bch_t* hio_find_bchars_in_bchars ( + const hio_bch_t* str, + hio_oow_t strsz, + const hio_bch_t* sub, + hio_oow_t subsz, + int inorecase +); + +HIO_EXPORT hio_uch_t* hio_rfind_uchars_in_uchars ( + const hio_uch_t* str, + hio_oow_t strsz, + const hio_uch_t* sub, + hio_oow_t subsz, + int inorecase +); + +HIO_EXPORT hio_bch_t* hio_rfind_bchars_in_bchars ( + const hio_bch_t* str, + hio_oow_t strsz, + const hio_bch_t* sub, + hio_oow_t subsz, + int inorecase +); + +/* ------------------------------------ */ + +HIO_EXPORT hio_oow_t hio_rotate_uchars ( + hio_uch_t* str, + hio_oow_t len, + int dir, + hio_oow_t n +); + +HIO_EXPORT hio_oow_t hio_rotate_bchars ( + hio_bch_t* str, + hio_oow_t len, + int dir, + hio_oow_t n +); + + +HIO_EXPORT hio_uch_t* hio_trim_uchars ( + const hio_uch_t* str, + hio_oow_t* len, + int flags +); + +HIO_EXPORT hio_bch_t* hio_trim_bchars ( + const hio_bch_t* str, + hio_oow_t* len, + int flags +); + +HIO_EXPORT int hio_split_ucstr ( + hio_uch_t* s, + const hio_uch_t* delim, + hio_uch_t lquote, + hio_uch_t rquote, + hio_uch_t escape +); + +HIO_EXPORT int hio_split_bcstr ( + hio_bch_t* s, + const hio_bch_t* delim, + hio_bch_t lquote, + hio_bch_t rquote, + hio_bch_t escape +); + + +#if defined(HIO_OOCH_IS_UCH) +# define hio_count_oocstr hio_count_ucstr +# define hio_count_oocstr_limited hio_count_ucstr_limited + +# define hio_equal_oochars hio_equal_uchars +# define hio_comp_oochars hio_comp_uchars +# define hio_comp_oocstr_bcstr hio_comp_ucstr_bcstr +# define hio_comp_oochars_bcstr hio_comp_uchars_bcstr +# define hio_comp_oochars_ucstr hio_comp_uchars_ucstr +# define hio_comp_oochars_oocstr hio_comp_uchars_ucstr +# define hio_comp_oocstr hio_comp_ucstr + +# define hio_copy_oochars hio_copy_uchars +# define hio_copy_bchars_to_oochars hio_copy_bchars_to_uchars +# define hio_copy_oochars_to_bchars hio_copy_uchars_to_bchars +# define hio_copy_uchars_to_oochars hio_copy_uchars +# define hio_copy_oochars_to_uchars hio_copy_uchars + +# define hio_copy_oochars_to_oocstr hio_copy_uchars_to_ucstr +# define hio_copy_oochars_to_oocstr_unlimited hio_copy_uchars_to_ucstr_unlimited +# define hio_copy_oocstr hio_copy_ucstr +# define hio_copy_oocstr_unlimited hio_copy_ucstr_unlimited +# define hio_copy_fmt_oocses_to_oocstr hio_copy_fmt_ucses_to_ucstr +# define hio_copy_fmt_oocstr_to_oocstr hio_copy_fmt_ucstr_to_ucstr + +# define hio_concat_oochars_to_ucstr hio_concat_uchars_to_ucstr +# define hio_concat_oocstr hio_concat_ucstr + +# define hio_fill_oochars hio_fill_uchars +# define hio_find_oocstr_word_in_oocstr hio_find_ucstr_word_in_ucstr +# define hio_find_oochar_in_oochars hio_find_uchar_in_uchars +# define hio_rfind_oochar_in_oochars hio_rfind_uchar_in_uchars +# define hio_find_oochar_in_oocstr hio_find_uchar_in_ucstr + +# define hio_trim_oochars hio_trim_uchars +# define hio_split_oocstr hio_split_ucstr +#else +# define hio_count_oocstr hio_count_bcstr +# define hio_count_oocstr_limited hio_count_bcstr_limited + +# define hio_equal_oochars hio_equal_bchars +# define hio_comp_oochars hio_comp_bchars +# define hio_comp_oocstr_bcstr hio_comp_bcstr +# define hio_comp_oochars_bcstr hio_comp_bchars_bcstr +# define hio_comp_oochars_ucstr hio_comp_bchars_ucstr +# define hio_comp_oochars_oocstr hio_comp_bchars_bcstr +# define hio_comp_oocstr hio_comp_bcstr + +# define hio_copy_oochars hio_copy_bchars +# define hio_copy_bchars_to_oochars hio_copy_bchars +# define hio_copy_oochars_to_bchars hio_copy_bchars +# define hio_copy_uchars_to_oochars hio_copy_uchars_to_bchars +# define hio_copy_oochars_to_uchars hio_copy_bchars_to_uchars + +# define hio_copy_oochars_to_oocstr hio_copy_bchars_to_bcstr +# define hio_copy_oochars_to_oocstr_unlimited hio_copy_bchars_to_bcstr_unlimited +# define hio_copy_oocstr hio_copy_bcstr +# define hio_copy_oocstr_unlimited hio_copy_bcstr_unlimited +# define hio_copy_fmt_oocses_to_oocstr hio_copy_fmt_bcses_to_bcstr +# define hio_copy_fmt_oocstr_to_oocstr hio_copy_fmt_bcstr_to_bcstr + +# define hio_concat_oochars_to_bcstr hio_concat_bchars_to_bcstr +# define hio_concat_oocstr hio_concat_bcstr + +# define hio_fill_oochars hio_fill_bchars +# define hio_find_oocstr_word_in_oocstr hio_find_bcstr_word_in_bcstr +# define hio_find_oochar_in_oochars hio_find_bchar_in_bchars +# define hio_rfind_oochar_in_oochars hio_rfind_bchar_in_bchars +# define hio_find_oochar_in_oocstr hio_find_bchar_in_bcstr + +# define hio_trim_oochars hio_trim_bchars +# define hio_split_oocstr hio_split_bcstr +#endif + +/* ------------------------------------------------------------------------- */ + +#define HIO_BYTE_TO_BCSTR_RADIXMASK (0xFF) +#define HIO_BYTE_TO_BCSTR_LOWERCASE (1 << 8) + +HIO_EXPORT hio_oow_t hio_byte_to_bcstr ( + hio_uint8_t byte, + hio_bch_t* buf, + hio_oow_t size, + int flagged_radix, + hio_bch_t fill +); + +#define HIO_BYTE_TO_UCSTR_RADIXMASK (0xFF) +#define HIO_BYTE_TO_UCSTR_LOWERCASE (1 << 8) + +HIO_EXPORT hio_oow_t hio_byte_to_ucstr ( + hio_uint8_t byte, + hio_uch_t* buf, + hio_oow_t size, + int flagged_radix, + hio_uch_t fill +); + +#if defined(HIO_OOCH_IS_UCH) +# define hio_byte_to_oocstr hio_byte_to_ucstr +#else +# define hio_byte_to_oocstr hio_byte_to_bcstr +#endif + +/* ------------------------------------------------------------------------- */ + +HIO_EXPORT hio_oow_t hio_intmax_to_ucstr ( + hio_intmax_t value, + int radix, + const hio_uch_t* prefix, + hio_uch_t* buf, + hio_oow_t size +); + +HIO_EXPORT hio_oow_t hio_intmax_to_bcstr ( + hio_intmax_t value, + int radix, + const hio_bch_t* prefix, + hio_bch_t* buf, + hio_oow_t size +); + +HIO_EXPORT hio_oow_t hio_uintmax_to_ucstr ( + hio_uintmax_t value, + int radix, + const hio_uch_t* prefix, + hio_uch_t* buf, + hio_oow_t size +); + +HIO_EXPORT hio_oow_t hio_uintmax_to_bcstr ( + hio_uintmax_t value, + int radix, + const hio_bch_t* prefix, + hio_bch_t* buf, + hio_oow_t size +); + +#if defined(HIO_OOCH_IS_UCH) +# define hio_intmax_to_oocstr hio_intmax_to_ucstr +# define hio_uintmax_to_oocstr hio_uintmax_to_ucstr +#else +# define hio_intmax_to_oocstr hio_intmax_to_bcstr +# define hio_uintmax_to_oocstr hio_uintmax_to_bcstr +#endif + + +/* ------------------------------------------------------------------------- */ + +#define HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) (((!!(e)) << 0) | ((!!(ltrim)) << 2) | ((!!(rtrim)) << 3) | ((base) << 8)) +#define HIO_CHARS_TO_INT_GET_OPTION_E(option) ((option) & 1) +#define HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) ((option) & 4) +#define HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) ((option) & 8) +#define HIO_CHARS_TO_INT_GET_OPTION_BASE(option) ((option) >> 8) + +#define HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) (((!!(e)) << 0) | ((!!(ltrim)) << 2) | ((!!(rtrim)) << 3) | ((base) << 8)) +#define HIO_CHARS_TO_UINT_GET_OPTION_E(option) ((option) & 1) +#define HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) ((option) & 4) +#define HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) ((option) & 8) +#define HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) ((option) >> 8) + +#define HIO_OOCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) +#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) +#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) +#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) +#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) + +#define HIO_OOCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) +#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) +#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) +#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) +#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) + +#define HIO_UCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) +#define HIO_UCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) +#define HIO_UCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) +#define HIO_UCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) +#define HIO_UCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) + +#define HIO_BCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) +#define HIO_BCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) +#define HIO_BCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) +#define HIO_BCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) +#define HIO_BCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) + +#define HIO_UCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) +#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) +#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) +#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) +#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) + +#define HIO_BCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) +#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) +#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) +#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) +#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) + +HIO_EXPORT hio_intmax_t hio_uchars_to_intmax ( + const hio_uch_t* str, + hio_oow_t len, + int option, + const hio_uch_t** endptr, + int* is_sober +); + +HIO_EXPORT hio_intmax_t hio_bchars_to_intmax ( + const hio_bch_t* str, + hio_oow_t len, + int option, + const hio_bch_t** endptr, + int* is_sober +); + +HIO_EXPORT hio_uintmax_t hio_uchars_to_uintmax ( + const hio_uch_t* str, + hio_oow_t len, + int option, + const hio_uch_t** endptr, + int* is_sober +); + +HIO_EXPORT hio_uintmax_t hio_bchars_to_uintmax ( + const hio_bch_t* str, + hio_oow_t len, + int option, + const hio_bch_t** endptr, + int* is_sober +); +#if defined(HIO_OOCH_IS_UCH) +# define hio_oochars_to_intmax hio_uchars_to_intmax +# define hio_oochars_to_uintmax hio_uchars_to_uintmax +#else +# define hio_oochars_to_intmax hio_bchars_to_intmax +# define hio_oochars_to_uintmax hio_bchars_to_uintmax +#endif + +#if defined(__cplusplus) +} +#endif + +/* Some C++ utilities below */ +#if defined(__cplusplus) + +/* +static inline bool is_space (char c) { return isspace(c); } +static inline bool is_wspace (wchar_t c) { return iswspace(c); } +unsigned x = hio_chars_to_uint("0x12345", 7, 0, NULL, NULL); +unsigned y = hio_chars_to_uint(L"0x12345", 7, 0, NULL, NULL); +int a = hio_chars_to_int("-0x12345", 8, 0, NULL, NULL); +int b = hio_chars_to_int(L"-0x12345", 8, 0, NULL, NULL); +*/ +templatefn_chars_to_int(hio_chars_to_int, CHAR_TYPE, INT_TYPE, IS_SPACE, HIO_CHARS_TO_INT) +templatefn_chars_to_uint(hio_chars_to_uint, CHAR_TYPE, UINT_TYPE, IS_SPACE, HIO_CHARS_TO_UINT) + +#endif + +#endif diff --git a/hio/lib/hio-utl.h b/hio/lib/hio-utl.h index 8d8d4ca..13796e9 100644 --- a/hio/lib/hio-utl.h +++ b/hio/lib/hio-utl.h @@ -22,23 +22,13 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* - * Do NOT edit hio-utl.h. Edit hio-utl.h.m4 instead. - * - * Generate hio-utl.h.m4 with m4 - * $ m4 hio-utl.h.m4 > hio-utl.h - */ - #ifndef _HIO_UTL_H_ #define _HIO_UTL_H_ #include +#include #include - - - - /* ========================================================================= * ENDIAN CHANGE OF A CONSTANT * ========================================================================= */ @@ -238,79 +228,12 @@ * STRING * ========================================================================= */ -enum hio_trim_oochars_flag_t -{ - HIO_TRIM_OOCHARS_LEFT = (1 << 0), /**< trim leading spaces */ -#define HIO_TRIM_OOCHARS_LEFT HIO_TRIM_OOCHARS_LEFT -#define HIO_TRIM_UCHARS_LEFT HIO_TRIM_OOCHARS_LEFT -#define HIO_TRIM_BCHARS_LEFT HIO_TRIM_OOCHARS_LEFT - HIO_TRIM_OOCHARS_RIGHT = (1 << 1) /**< trim trailing spaces */ -#define HIO_TRIM_OOCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT -#define HIO_TRIM_UCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT -#define HIO_TRIM_BCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT -}; - -#if defined(__cplusplus) -extern "C" { -#endif - - -/* ------------------------------------ */ - -HIO_EXPORT int hio_comp_uchars ( - const hio_uch_t* str1, - hio_oow_t len1, - const hio_uch_t* str2, - hio_oow_t len2, - int ignorecase -); - -HIO_EXPORT int hio_comp_bchars ( - const hio_bch_t* str1, - hio_oow_t len1, - const hio_bch_t* str2, - hio_oow_t len2, - int ignorecase -); - -HIO_EXPORT int hio_comp_ucstr ( - const hio_uch_t* str1, - const hio_uch_t* str2, - int ignorecase -); - -HIO_EXPORT int hio_comp_bcstr ( - const hio_bch_t* str1, - const hio_bch_t* str2, - int ignorecase -); - -HIO_EXPORT int hio_comp_ucstr_limited ( - const hio_uch_t* str1, - const hio_uch_t* str2, - hio_oow_t maxlen, - int ignorecase -); - -HIO_EXPORT int hio_comp_bcstr_limited ( - const hio_bch_t* str1, - const hio_bch_t* str2, - hio_oow_t maxlen, - int ignorecase -); - HIO_EXPORT int hio_comp_ucstr_bcstr ( const hio_uch_t* str1, const hio_bch_t* str2, int ignorecase ); -HIO_EXPORT int hio_comp_uchars_ucstr ( - const hio_uch_t* str1, - hio_oow_t len, - const hio_uch_t* str2, - int ignorecase -); HIO_EXPORT int hio_comp_uchars_bcstr ( const hio_uch_t* str1, @@ -319,13 +242,6 @@ HIO_EXPORT int hio_comp_uchars_bcstr ( int ignorecase ); -HIO_EXPORT int hio_comp_bchars_bcstr ( - const hio_bch_t* str1, - hio_oow_t len, - const hio_bch_t* str2, - int ignorecase -); - HIO_EXPORT int hio_comp_bchars_ucstr ( const hio_bch_t* str1, hio_oow_t len, @@ -333,534 +249,8 @@ HIO_EXPORT int hio_comp_bchars_ucstr ( int ignorecase ); -/* ------------------------------------ */ - -HIO_EXPORT hio_oow_t hio_concat_uchars_to_ucstr ( - hio_uch_t* buf, - hio_oow_t bsz, - const hio_uch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_concat_bchars_to_bcstr ( - hio_bch_t* buf, - hio_oow_t bsz, - const hio_bch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_concat_ucstr ( - hio_uch_t* buf, - hio_oow_t bsz, - const hio_uch_t* src -); - -HIO_EXPORT hio_oow_t hio_concat_bcstr ( - hio_bch_t* buf, - hio_oow_t bsz, - const hio_bch_t* src -); - -/* ------------------------------------ */ - -HIO_EXPORT void hio_copy_uchars ( - hio_uch_t* dst, - const hio_uch_t* src, - hio_oow_t len -); - -HIO_EXPORT void hio_copy_bchars ( - hio_bch_t* dst, - const hio_bch_t* src, - hio_oow_t len -); - -HIO_EXPORT void hio_copy_bchars_to_uchars ( - hio_uch_t* dst, - const hio_bch_t* src, - hio_oow_t len -); -HIO_EXPORT void hio_copy_uchars_to_bchars ( - hio_bch_t* dst, - const hio_uch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_copy_uchars_to_ucstr ( - hio_uch_t* dst, - hio_oow_t dlen, - const hio_uch_t* src, - hio_oow_t slen -); - -HIO_EXPORT hio_oow_t hio_copy_bchars_to_bcstr ( - hio_bch_t* dst, - hio_oow_t dlen, - const hio_bch_t* src, - hio_oow_t slen -); - -HIO_EXPORT hio_oow_t hio_copy_uchars_to_ucstr_unlimited ( - hio_uch_t* dst, - const hio_uch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_copy_bchars_to_bcstr_unlimited ( - hio_bch_t* dst, - const hio_bch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_copy_ucstr_to_uchars ( - hio_uch_t* dst, - hio_oow_t dlen, - const hio_uch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_bcstr_to_bchars ( - hio_bch_t* dst, - hio_oow_t dlen, - const hio_bch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_ucstr ( - hio_uch_t* dst, - hio_oow_t len, - const hio_uch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_bcstr ( - hio_bch_t* dst, - hio_oow_t len, - const hio_bch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_ucstr_unlimited ( - hio_uch_t* dst, - const hio_uch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_bcstr_unlimited ( - hio_bch_t* dst, - const hio_bch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_fmt_ucstrs_to_ucstr ( - hio_uch_t* buf, - hio_oow_t bsz, - const hio_uch_t* fmt, - const hio_uch_t* str[] -); - -HIO_EXPORT hio_oow_t hio_copy_fmt_bcstrs_to_bcstr ( - hio_bch_t* buf, - hio_oow_t bsz, - const hio_bch_t* fmt, - const hio_bch_t* str[] -); - -HIO_EXPORT hio_oow_t hio_copy_fmt_ucses_to_ucstr ( - hio_uch_t* buf, - hio_oow_t bsz, - const hio_uch_t* fmt, - const hio_ucs_t str[] -); - -HIO_EXPORT hio_oow_t hio_copy_fmt_bcses_to_bcstr ( - hio_bch_t* buf, - hio_oow_t bsz, - const hio_bch_t* fmt, - const hio_bcs_t str[] -); - -/* ------------------------------------ */ - -HIO_EXPORT hio_oow_t hio_count_ucstr ( - const hio_uch_t* str -); - -HIO_EXPORT hio_oow_t hio_count_bcstr ( - const hio_bch_t* str -); - -HIO_EXPORT hio_oow_t hio_count_ucstr_limited ( - const hio_uch_t* str, - hio_oow_t maxlen -); - -HIO_EXPORT hio_oow_t hio_count_bcstr_limited ( - const hio_bch_t* str, - hio_oow_t maxlen -); - -/* ------------------------------------ */ - -/** - * The hio_equal_uchars() function determines equality of two strings - * of the same length \a len. - */ -HIO_EXPORT int hio_equal_uchars ( - const hio_uch_t* str1, - const hio_uch_t* str2, - hio_oow_t len -); - -HIO_EXPORT int hio_equal_bchars ( - const hio_bch_t* str1, - const hio_bch_t* str2, - hio_oow_t len -); - -/* ------------------------------------ */ - - -HIO_EXPORT void hio_fill_uchars ( - hio_uch_t* dst, - const hio_uch_t ch, - hio_oow_t len -); - -HIO_EXPORT void hio_fill_bchars ( - hio_bch_t* dst, - const hio_bch_t ch, - hio_oow_t len -); - -/* ------------------------------------ */ - -HIO_EXPORT const hio_bch_t* hio_find_bcstr_word_in_bcstr ( - const hio_bch_t* str, - const hio_bch_t* word, - hio_bch_t extra_delim, - int ignorecase -); - -HIO_EXPORT const hio_uch_t* hio_find_ucstr_word_in_ucstr ( - const hio_uch_t* str, - const hio_uch_t* word, - hio_uch_t extra_delim, - int ignorecase -); - -HIO_EXPORT hio_uch_t* hio_find_uchar_in_uchars ( - const hio_uch_t* ptr, - hio_oow_t len, - hio_uch_t c -); - -HIO_EXPORT hio_bch_t* hio_find_bchar_in_bchars ( - const hio_bch_t* ptr, - hio_oow_t len, - hio_bch_t c -); - -HIO_EXPORT hio_uch_t* hio_rfind_uchar_in_uchars ( - const hio_uch_t* ptr, - hio_oow_t len, - hio_uch_t c -); - -HIO_EXPORT hio_bch_t* hio_rfind_bchar_in_bchars ( - const hio_bch_t* ptr, - hio_oow_t len, - hio_bch_t c -); - -HIO_EXPORT hio_uch_t* hio_find_uchar_in_ucstr ( - const hio_uch_t* ptr, - hio_uch_t c -); - -HIO_EXPORT hio_bch_t* hio_find_bchar_in_bcstr ( - const hio_bch_t* ptr, - hio_bch_t c -); - -HIO_EXPORT hio_uch_t* hio_rfind_uchar_in_ucstr ( - const hio_uch_t* ptr, - hio_uch_t c -); - -HIO_EXPORT hio_bch_t* hio_rfind_bchar_in_bcstr ( - const hio_bch_t* ptr, - hio_bch_t c -); - -HIO_EXPORT hio_uch_t* hio_find_uchars_in_uchars ( - const hio_uch_t* str, - hio_oow_t strsz, - const hio_uch_t* sub, - hio_oow_t subsz, - int inorecase -); - -HIO_EXPORT hio_bch_t* hio_find_bchars_in_bchars ( - const hio_bch_t* str, - hio_oow_t strsz, - const hio_bch_t* sub, - hio_oow_t subsz, - int inorecase -); - -HIO_EXPORT hio_uch_t* hio_rfind_uchars_in_uchars ( - const hio_uch_t* str, - hio_oow_t strsz, - const hio_uch_t* sub, - hio_oow_t subsz, - int inorecase -); - -HIO_EXPORT hio_bch_t* hio_rfind_bchars_in_bchars ( - const hio_bch_t* str, - hio_oow_t strsz, - const hio_bch_t* sub, - hio_oow_t subsz, - int inorecase -); - -/* ------------------------------------ */ - -HIO_EXPORT hio_oow_t hio_rotate_uchars ( - hio_uch_t* str, - hio_oow_t len, - int dir, - hio_oow_t n -); - -HIO_EXPORT hio_oow_t hio_rotate_bchars ( - hio_bch_t* str, - hio_oow_t len, - int dir, - hio_oow_t n -); - - -HIO_EXPORT hio_uch_t* hio_trim_uchars ( - const hio_uch_t* str, - hio_oow_t* len, - int flags -); - -HIO_EXPORT hio_bch_t* hio_trim_bchars ( - const hio_bch_t* str, - hio_oow_t* len, - int flags -); - -HIO_EXPORT int hio_split_ucstr ( - hio_uch_t* s, - const hio_uch_t* delim, - hio_uch_t lquote, - hio_uch_t rquote, - hio_uch_t escape -); - -HIO_EXPORT int hio_split_bcstr ( - hio_bch_t* s, - const hio_bch_t* delim, - hio_bch_t lquote, - hio_bch_t rquote, - hio_bch_t escape -); - - -#if defined(HIO_OOCH_IS_UCH) -# define hio_count_oocstr hio_count_ucstr -# define hio_count_oocstr_limited hio_count_ucstr_limited - -# define hio_equal_oochars hio_equal_uchars -# define hio_comp_oochars hio_comp_uchars -# define hio_comp_oocstr_bcstr hio_comp_ucstr_bcstr -# define hio_comp_oochars_bcstr hio_comp_uchars_bcstr -# define hio_comp_oochars_ucstr hio_comp_uchars_ucstr -# define hio_comp_oochars_oocstr hio_comp_uchars_ucstr -# define hio_comp_oocstr hio_comp_ucstr - -# define hio_copy_oochars hio_copy_uchars -# define hio_copy_bchars_to_oochars hio_copy_bchars_to_uchars -# define hio_copy_oochars_to_bchars hio_copy_uchars_to_bchars -# define hio_copy_uchars_to_oochars hio_copy_uchars -# define hio_copy_oochars_to_uchars hio_copy_uchars - -# define hio_copy_oochars_to_oocstr hio_copy_uchars_to_ucstr -# define hio_copy_oochars_to_oocstr_unlimited hio_copy_uchars_to_ucstr_unlimited -# define hio_copy_oocstr hio_copy_ucstr -# define hio_copy_oocstr_unlimited hio_copy_ucstr_unlimited -# define hio_copy_fmt_oocses_to_oocstr hio_copy_fmt_ucses_to_ucstr -# define hio_copy_fmt_oocstr_to_oocstr hio_copy_fmt_ucstr_to_ucstr - -# define hio_concat_oochars_to_ucstr hio_concat_uchars_to_ucstr -# define hio_concat_oocstr hio_concat_ucstr - -# define hio_fill_oochars hio_fill_uchars -# define hio_find_oocstr_word_in_oocstr hio_find_ucstr_word_in_ucstr -# define hio_find_oochar_in_oochars hio_find_uchar_in_uchars -# define hio_rfind_oochar_in_oochars hio_rfind_uchar_in_uchars -# define hio_find_oochar_in_oocstr hio_find_uchar_in_ucstr - -# define hio_trim_oochars hio_trim_uchars -# define hio_split_oocstr hio_split_ucstr -#else -# define hio_count_oocstr hio_count_bcstr -# define hio_count_oocstr_limited hio_count_bcstr_limited - -# define hio_equal_oochars hio_equal_bchars -# define hio_comp_oochars hio_comp_bchars -# define hio_comp_oocstr_bcstr hio_comp_bcstr -# define hio_comp_oochars_bcstr hio_comp_bchars_bcstr -# define hio_comp_oochars_ucstr hio_comp_bchars_ucstr -# define hio_comp_oochars_oocstr hio_comp_bchars_bcstr -# define hio_comp_oocstr hio_comp_bcstr - -# define hio_copy_oochars hio_copy_bchars -# define hio_copy_bchars_to_oochars hio_copy_bchars -# define hio_copy_oochars_to_bchars hio_copy_bchars -# define hio_copy_uchars_to_oochars hio_copy_uchars_to_bchars -# define hio_copy_oochars_to_uchars hio_copy_bchars_to_uchars - -# define hio_copy_oochars_to_oocstr hio_copy_bchars_to_bcstr -# define hio_copy_oochars_to_oocstr_unlimited hio_copy_bchars_to_bcstr_unlimited -# define hio_copy_oocstr hio_copy_bcstr -# define hio_copy_oocstr_unlimited hio_copy_bcstr_unlimited -# define hio_copy_fmt_oocses_to_oocstr hio_copy_fmt_bcses_to_bcstr -# define hio_copy_fmt_oocstr_to_oocstr hio_copy_fmt_bcstr_to_bcstr - -# define hio_concat_oochars_to_bcstr hio_concat_bchars_to_bcstr -# define hio_concat_oocstr hio_concat_bcstr - -# define hio_fill_oochars hio_fill_bchars -# define hio_find_oocstr_word_in_oocstr hio_find_bcstr_word_in_bcstr -# define hio_find_oochar_in_oochars hio_find_bchar_in_bchars -# define hio_rfind_oochar_in_oochars hio_rfind_bchar_in_bchars -# define hio_find_oochar_in_oocstr hio_find_bchar_in_bcstr - -# define hio_trim_oochars hio_trim_bchars -# define hio_split_oocstr hio_split_bcstr -#endif /* ------------------------------------------------------------------------- */ -#define HIO_BYTE_TO_BCSTR_RADIXMASK (0xFF) -#define HIO_BYTE_TO_BCSTR_LOWERCASE (1 << 8) - -HIO_EXPORT hio_oow_t hio_byte_to_bcstr ( - hio_uint8_t byte, - hio_bch_t* buf, - hio_oow_t size, - int flagged_radix, - hio_bch_t fill -); - -#define HIO_BYTE_TO_UCSTR_RADIXMASK (0xFF) -#define HIO_BYTE_TO_UCSTR_LOWERCASE (1 << 8) - -HIO_EXPORT hio_oow_t hio_byte_to_ucstr ( - hio_uint8_t byte, - hio_uch_t* buf, - hio_oow_t size, - int flagged_radix, - hio_uch_t fill -); - -/* ------------------------------------------------------------------------- */ -#define HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) (((!!(e)) << 0) | ((!!(ltrim)) << 2) | ((!!(rtrim)) << 3) | ((base) << 8)) -#define HIO_CHARS_TO_INT_GET_OPTION_E(option) ((option) & 1) -#define HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) ((option) & 4) -#define HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) ((option) & 8) -#define HIO_CHARS_TO_INT_GET_OPTION_BASE(option) ((option) >> 8) - -#define HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) (((!!(e)) << 0) | ((!!(ltrim)) << 2) | ((!!(rtrim)) << 3) | ((base) << 8)) -#define HIO_CHARS_TO_UINT_GET_OPTION_E(option) ((option) & 1) -#define HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) ((option) & 4) -#define HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) ((option) & 8) -#define HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) ((option) >> 8) - -#define HIO_OOCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) -#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) -#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) -#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) - -#define HIO_OOCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) -#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) -#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) -#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) - -#define HIO_UCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_UCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) -#define HIO_UCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) -#define HIO_UCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) -#define HIO_UCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) - -#define HIO_BCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_BCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) -#define HIO_BCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) -#define HIO_BCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) -#define HIO_BCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) - -#define HIO_UCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) -#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) -#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) -#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) - -#define HIO_BCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) -#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) -#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) -#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) - -HIO_EXPORT hio_intmax_t hio_uchars_to_intmax ( - const hio_uch_t* str, - hio_oow_t len, - int option, - const hio_uch_t** endptr, - int* is_sober -); - -HIO_EXPORT hio_intmax_t hio_bchars_to_intmax ( - const hio_bch_t* str, - hio_oow_t len, - int option, - const hio_bch_t** endptr, - int* is_sober -); - -HIO_EXPORT hio_uintmax_t hio_uchars_to_uintmax ( - const hio_uch_t* str, - hio_oow_t len, - int option, - const hio_uch_t** endptr, - int* is_sober -); - -HIO_EXPORT hio_uintmax_t hio_bchars_to_uintmax ( - const hio_bch_t* str, - hio_oow_t len, - int option, - const hio_bch_t** endptr, - int* is_sober -); -#if defined(HIO_OOCH_IS_UCH) -# define hio_oochars_to_intmax hio_uchars_to_intmax -# define hio_oochars_to_uintmax hio_uchars_to_uintmax -#else -# define hio_oochars_to_intmax hio_bchars_to_intmax -# define hio_oochars_to_uintmax hio_bchars_to_uintmax -#endif - -/* ------------------------------------------------------------------------- */ - -#if defined(HIO_OOCH_IS_UCH) -# define hio_conv_oocstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hio_conv_ucstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) -# define hio_conv_oochars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hio_conv_uchars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) -#else -# define hio_conv_oocstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hio_conv_bcstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) -# define hio_conv_oochars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hio_conv_bchars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) -#endif - - HIO_EXPORT int hio_conv_bcstr_to_ucstr_with_cmgr ( const hio_bch_t* bcs, hio_oow_t* bcslen, @@ -895,6 +285,15 @@ HIO_EXPORT int hio_conv_uchars_to_bchars_with_cmgr ( hio_cmgr_t* cmgr ); +#if defined(HIO_OOCH_IS_UCH) +# define hio_conv_oocstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hio_conv_ucstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) +# define hio_conv_oochars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hio_conv_uchars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) +#else +# define hio_conv_oocstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hio_conv_bcstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) +# define hio_conv_oochars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hio_conv_bchars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) +#endif + + /* ------------------------------------------------------------------------- */ HIO_EXPORT hio_cmgr_t* hio_get_utf8_cmgr ( @@ -1310,13 +709,11 @@ templateINT_T } else if (rem >= 2 && base == 16) { - if (*p == '0' && - (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && - (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -1420,13 +817,11 @@ templateUINT } else if (rem >= 2 && base == 16) { - if (*p == '0' && - (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && - (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ diff --git a/hio/lib/hio-utl.h.m4 b/hio/lib/hio-utl.h.m4 deleted file mode 100644 index b410a95..0000000 --- a/hio/lib/hio-utl.h.m4 +++ /dev/null @@ -1,1266 +0,0 @@ -/* - Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/* - * Do NOT edit hio-utl.h. Edit hio-utl.h.m4 instead. - * - * Generate hio-utl.h.m4 with m4 - * $ m4 hio-utl.h.m4 > hio-utl.h - */ - -#ifndef _HIO_UTL_H_ -#define _HIO_UTL_H_ - -#include -#include - -dnl --------------------------------------------------------------------------- -dnl include utl-str.m4 for c++ template functions far below -include(`utl-str.m4') -dnl --------------------------------------------------------------------------- - -/* ========================================================================= - * ENDIAN CHANGE OF A CONSTANT - * ========================================================================= */ -#define HIO_CONST_BSWAP16(x) \ - ((hio_uint16_t)((((hio_uint16_t)(x) & ((hio_uint16_t)0xff << 0)) << 8) | \ - (((hio_uint16_t)(x) & ((hio_uint16_t)0xff << 8)) >> 8))) - -#define HIO_CONST_BSWAP32(x) \ - ((hio_uint32_t)((((hio_uint32_t)(x) & ((hio_uint32_t)0xff << 0)) << 24) | \ - (((hio_uint32_t)(x) & ((hio_uint32_t)0xff << 8)) << 8) | \ - (((hio_uint32_t)(x) & ((hio_uint32_t)0xff << 16)) >> 8) | \ - (((hio_uint32_t)(x) & ((hio_uint32_t)0xff << 24)) >> 24))) - -#if defined(HIO_HAVE_UINT64_T) -#define HIO_CONST_BSWAP64(x) \ - ((hio_uint64_t)((((hio_uint64_t)(x) & ((hio_uint64_t)0xff << 0)) << 56) | \ - (((hio_uint64_t)(x) & ((hio_uint64_t)0xff << 8)) << 40) | \ - (((hio_uint64_t)(x) & ((hio_uint64_t)0xff << 16)) << 24) | \ - (((hio_uint64_t)(x) & ((hio_uint64_t)0xff << 24)) << 8) | \ - (((hio_uint64_t)(x) & ((hio_uint64_t)0xff << 32)) >> 8) | \ - (((hio_uint64_t)(x) & ((hio_uint64_t)0xff << 40)) >> 24) | \ - (((hio_uint64_t)(x) & ((hio_uint64_t)0xff << 48)) >> 40) | \ - (((hio_uint64_t)(x) & ((hio_uint64_t)0xff << 56)) >> 56))) -#endif - -#if defined(HIO_HAVE_UINT128_T) -#define HIO_CONST_BSWAP128(x) \ - ((hio_uint128_t)((((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 0)) << 120) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 8)) << 104) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 16)) << 88) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 24)) << 72) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 32)) << 56) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 40)) << 40) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 48)) << 24) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 56)) << 8) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 64)) >> 8) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 72)) >> 24) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 80)) >> 40) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 88)) >> 56) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 96)) >> 72) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 104)) >> 88) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 112)) >> 104) | \ - (((hio_uint128_t)(x) & ((hio_uint128_t)0xff << 120)) >> 120))) -#endif - -#if defined(HIO_ENDIAN_LITTLE) - -# if defined(HIO_HAVE_UINT16_T) -# define HIO_CONST_NTOH16(x) HIO_CONST_BSWAP16(x) -# define HIO_CONST_HTON16(x) HIO_CONST_BSWAP16(x) -# define HIO_CONST_HTOBE16(x) HIO_CONST_BSWAP16(x) -# define HIO_CONST_HTOLE16(x) (x) -# define HIO_CONST_BE16TOH(x) HIO_CONST_BSWAP16(x) -# define HIO_CONST_LE16TOH(x) (x) -# endif - -# if defined(HIO_HAVE_UINT32_T) -# define HIO_CONST_NTOH32(x) HIO_CONST_BSWAP32(x) -# define HIO_CONST_HTON32(x) HIO_CONST_BSWAP32(x) -# define HIO_CONST_HTOBE32(x) HIO_CONST_BSWAP32(x) -# define HIO_CONST_HTOLE32(x) (x) -# define HIO_CONST_BE32TOH(x) HIO_CONST_BSWAP32(x) -# define HIO_CONST_LE32TOH(x) (x) -# endif - -# if defined(HIO_HAVE_UINT64_T) -# define HIO_CONST_NTOH64(x) HIO_CONST_BSWAP64(x) -# define HIO_CONST_HTON64(x) HIO_CONST_BSWAP64(x) -# define HIO_CONST_HTOBE64(x) HIO_CONST_BSWAP64(x) -# define HIO_CONST_HTOLE64(x) (x) -# define HIO_CONST_BE64TOH(x) HIO_CONST_BSWAP64(x) -# define HIO_CONST_LE64TOH(x) (x) -# endif - -# if defined(HIO_HAVE_UINT128_T) -# define HIO_CONST_NTOH128(x) HIO_CONST_BSWAP128(x) -# define HIO_CONST_HTON128(x) HIO_CONST_BSWAP128(x) -# define HIO_CONST_HTOBE128(x) HIO_CONST_BSWAP128(x) -# define HIO_CONST_HTOLE128(x) (x) -# define HIO_CONST_BE128TOH(x) HIO_CONST_BSWAP128(x) -# define HIO_CONST_LE128TOH(x) (x) -#endif - -#elif defined(HIO_ENDIAN_BIG) - -# if defined(HIO_HAVE_UINT16_T) -# define HIO_CONST_NTOH16(x) (x) -# define HIO_CONST_HTON16(x) (x) -# define HIO_CONST_HTOBE16(x) (x) -# define HIO_CONST_HTOLE16(x) HIO_CONST_BSWAP16(x) -# define HIO_CONST_BE16TOH(x) (x) -# define HIO_CONST_LE16TOH(x) HIO_CONST_BSWAP16(x) -# endif - -# if defined(HIO_HAVE_UINT32_T) -# define HIO_CONST_NTOH32(x) (x) -# define HIO_CONST_HTON32(x) (x) -# define HIO_CONST_HTOBE32(x) (x) -# define HIO_CONST_HTOLE32(x) HIO_CONST_BSWAP32(x) -# define HIO_CONST_BE32TOH(x) (x) -# define HIO_CONST_LE32TOH(x) HIO_CONST_BSWAP32(x) -# endif - -# if defined(HIO_HAVE_UINT64_T) -# define HIO_CONST_NTOH64(x) (x) -# define HIO_CONST_HTON64(x) (x) -# define HIO_CONST_HTOBE64(x) (x) -# define HIO_CONST_HTOLE64(x) HIO_CONST_BSWAP64(x) -# define HIO_CONST_BE64TOH(x) (x) -# define HIO_CONST_LE64TOH(x) HIO_CONST_BSWAP64(x) -# endif - -# if defined(HIO_HAVE_UINT128_T) -# define HIO_CONST_NTOH128(x) (x) -# define HIO_CONST_HTON128(x) (x) -# define HIO_CONST_HTOBE128(x) (x) -# define HIO_CONST_HTOLE128(x) HIO_CONST_BSWAP128(x) -# define HIO_CONST_BE128TOH(x) (x) -# define HIO_CONST_LE128TOH(x) HIO_CONST_BSWAP128(x) -# endif - -#else -# error UNKNOWN ENDIAN -#endif - - -/* ========================================================================= - * HASH - * ========================================================================= */ -#if (HIO_SIZEOF_OOW_T == 4) -# define HIO_HASH_FNV_MAGIC_INIT (0x811c9dc5) -# define HIO_HASH_FNV_MAGIC_PRIME (0x01000193) -#elif (HIO_SIZEOF_OOW_T == 8) -# define HIO_HASH_FNV_MAGIC_INIT (0xCBF29CE484222325) -# define HIO_HASH_FNV_MAGIC_PRIME (0x100000001B3l) -#elif (HIO_SIZEOF_OOW_T == 16) -# define HIO_HASH_FNV_MAGIC_INIT (0x6C62272E07BB014262B821756295C58D) -# define HIO_HASH_FNV_MAGIC_PRIME (0x1000000000000000000013B) -#endif - -#if defined(HIO_HASH_FNV_MAGIC_INIT) - /* FNV-1 hash */ -# define HIO_HASH_INIT HIO_HASH_FNV_MAGIC_INIT -# define HIO_HASH_VALUE(hv,v) (((hv) ^ (v)) * HIO_HASH_FNV_MAGIC_PRIME) - -#else - /* SDBM hash */ -# define HIO_HASH_INIT 0 -# define HIO_HASH_VALUE(hv,v) (((hv) << 6) + ((hv) << 16) - (hv) + (v)) -#endif - -#define HIO_HASH_VPTL(hv, ptr, len, type) do { \ - hv = HIO_HASH_INIT; \ - HIO_HASH_MORE_VPTL (hv, ptr, len, type); \ -} while(0) - -#define HIO_HASH_MORE_VPTL(hv, ptr, len, type) do { \ - type* __hio_hash_more_vptl_p = (type*)(ptr); \ - type* __hio_hash_more_vptl_q = (type*)__hio_hash_more_vptl_p + (len); \ - while (__hio_hash_more_vptl_p < __hio_hash_more_vptl_q) \ - { \ - hv = HIO_HASH_VALUE(hv, *__hio_hash_more_vptl_p); \ - __hio_hash_more_vptl_p++; \ - } \ -} while(0) - -#define HIO_HASH_VPTR(hv, ptr, type) do { \ - hv = HIO_HASH_INIT; \ - HIO_HASH_MORE_VPTR (hv, ptr, type); \ -} while(0) - -#define HIO_HASH_MORE_VPTR(hv, ptr, type) do { \ - type* __hio_hash_more_vptr_p = (type*)(ptr); \ - while (*__hio_hash_more_vptr_p) \ - { \ - hv = HIO_HASH_VALUE(hv, *__hio_hash_more_vptr_p); \ - __hio_hash_more_vptr_p++; \ - } \ -} while(0) - -#define HIO_HASH_BYTES(hv, ptr, len) HIO_HASH_VPTL(hv, ptr, len, const hio_uint8_t) -#define HIO_HASH_MORE_BYTES(hv, ptr, len) HIO_HASH_MORE_VPTL(hv, ptr, len, const hio_uint8_t) - -#define HIO_HASH_BCHARS(hv, ptr, len) HIO_HASH_VPTL(hv, ptr, len, const hio_bch_t) -#define HIO_HASH_MORE_BCHARS(hv, ptr, len) HIO_HASH_MORE_VPTL(hv, ptr, len, const hio_bch_t) - -#define HIO_HASH_UCHARS(hv, ptr, len) HIO_HASH_VPTL(hv, ptr, len, const hio_uch_t) -#define HIO_HASH_MORE_UCHARS(hv, ptr, len) HIO_HASH_MORE_VPTL(hv, ptr, len, const hio_uch_t) - -#define HIO_HASH_BCSTR(hv, ptr) HIO_HASH_VPTR(hv, ptr, const hio_bch_t) -#define HIO_HASH_MORE_BCSTR(hv, ptr) HIO_HASH_MORE_VPTR(hv, ptr, const hio_bch_t) - -#define HIO_HASH_UCSTR(hv, ptr) HIO_HASH_VPTR(hv, ptr, const hio_uch_t) -#define HIO_HASH_MORE_UCSTR(hv, ptr) HIO_HASH_MORE_VPTR(hv, ptr, const hio_uch_t) - -/* ========================================================================= - * STRING - * ========================================================================= */ - -enum hio_trim_oochars_flag_t -{ - HIO_TRIM_OOCHARS_LEFT = (1 << 0), /**< trim leading spaces */ -#define HIO_TRIM_OOCHARS_LEFT HIO_TRIM_OOCHARS_LEFT -#define HIO_TRIM_UCHARS_LEFT HIO_TRIM_OOCHARS_LEFT -#define HIO_TRIM_BCHARS_LEFT HIO_TRIM_OOCHARS_LEFT - HIO_TRIM_OOCHARS_RIGHT = (1 << 1) /**< trim trailing spaces */ -#define HIO_TRIM_OOCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT -#define HIO_TRIM_UCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT -#define HIO_TRIM_BCHARS_RIGHT HIO_TRIM_OOCHARS_RIGHT -}; - -#if defined(__cplusplus) -extern "C" { -#endif - - -/* ------------------------------------ */ - -HIO_EXPORT int hio_comp_uchars ( - const hio_uch_t* str1, - hio_oow_t len1, - const hio_uch_t* str2, - hio_oow_t len2, - int ignorecase -); - -HIO_EXPORT int hio_comp_bchars ( - const hio_bch_t* str1, - hio_oow_t len1, - const hio_bch_t* str2, - hio_oow_t len2, - int ignorecase -); - -HIO_EXPORT int hio_comp_ucstr ( - const hio_uch_t* str1, - const hio_uch_t* str2, - int ignorecase -); - -HIO_EXPORT int hio_comp_bcstr ( - const hio_bch_t* str1, - const hio_bch_t* str2, - int ignorecase -); - -HIO_EXPORT int hio_comp_ucstr_limited ( - const hio_uch_t* str1, - const hio_uch_t* str2, - hio_oow_t maxlen, - int ignorecase -); - -HIO_EXPORT int hio_comp_bcstr_limited ( - const hio_bch_t* str1, - const hio_bch_t* str2, - hio_oow_t maxlen, - int ignorecase -); - -HIO_EXPORT int hio_comp_ucstr_bcstr ( - const hio_uch_t* str1, - const hio_bch_t* str2, - int ignorecase -); - -HIO_EXPORT int hio_comp_uchars_ucstr ( - const hio_uch_t* str1, - hio_oow_t len, - const hio_uch_t* str2, - int ignorecase -); - -HIO_EXPORT int hio_comp_uchars_bcstr ( - const hio_uch_t* str1, - hio_oow_t len, - const hio_bch_t* str2, - int ignorecase -); - -HIO_EXPORT int hio_comp_bchars_bcstr ( - const hio_bch_t* str1, - hio_oow_t len, - const hio_bch_t* str2, - int ignorecase -); - -HIO_EXPORT int hio_comp_bchars_ucstr ( - const hio_bch_t* str1, - hio_oow_t len, - const hio_uch_t* str2, - int ignorecase -); - -/* ------------------------------------ */ - -HIO_EXPORT hio_oow_t hio_concat_uchars_to_ucstr ( - hio_uch_t* buf, - hio_oow_t bsz, - const hio_uch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_concat_bchars_to_bcstr ( - hio_bch_t* buf, - hio_oow_t bsz, - const hio_bch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_concat_ucstr ( - hio_uch_t* buf, - hio_oow_t bsz, - const hio_uch_t* src -); - -HIO_EXPORT hio_oow_t hio_concat_bcstr ( - hio_bch_t* buf, - hio_oow_t bsz, - const hio_bch_t* src -); - -/* ------------------------------------ */ - -HIO_EXPORT void hio_copy_uchars ( - hio_uch_t* dst, - const hio_uch_t* src, - hio_oow_t len -); - -HIO_EXPORT void hio_copy_bchars ( - hio_bch_t* dst, - const hio_bch_t* src, - hio_oow_t len -); - -HIO_EXPORT void hio_copy_bchars_to_uchars ( - hio_uch_t* dst, - const hio_bch_t* src, - hio_oow_t len -); -HIO_EXPORT void hio_copy_uchars_to_bchars ( - hio_bch_t* dst, - const hio_uch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_copy_uchars_to_ucstr ( - hio_uch_t* dst, - hio_oow_t dlen, - const hio_uch_t* src, - hio_oow_t slen -); - -HIO_EXPORT hio_oow_t hio_copy_bchars_to_bcstr ( - hio_bch_t* dst, - hio_oow_t dlen, - const hio_bch_t* src, - hio_oow_t slen -); - -HIO_EXPORT hio_oow_t hio_copy_uchars_to_ucstr_unlimited ( - hio_uch_t* dst, - const hio_uch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_copy_bchars_to_bcstr_unlimited ( - hio_bch_t* dst, - const hio_bch_t* src, - hio_oow_t len -); - -HIO_EXPORT hio_oow_t hio_copy_ucstr_to_uchars ( - hio_uch_t* dst, - hio_oow_t dlen, - const hio_uch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_bcstr_to_bchars ( - hio_bch_t* dst, - hio_oow_t dlen, - const hio_bch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_ucstr ( - hio_uch_t* dst, - hio_oow_t len, - const hio_uch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_bcstr ( - hio_bch_t* dst, - hio_oow_t len, - const hio_bch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_ucstr_unlimited ( - hio_uch_t* dst, - const hio_uch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_bcstr_unlimited ( - hio_bch_t* dst, - const hio_bch_t* src -); - -HIO_EXPORT hio_oow_t hio_copy_fmt_ucstrs_to_ucstr ( - hio_uch_t* buf, - hio_oow_t bsz, - const hio_uch_t* fmt, - const hio_uch_t* str[] -); - -HIO_EXPORT hio_oow_t hio_copy_fmt_bcstrs_to_bcstr ( - hio_bch_t* buf, - hio_oow_t bsz, - const hio_bch_t* fmt, - const hio_bch_t* str[] -); - -HIO_EXPORT hio_oow_t hio_copy_fmt_ucses_to_ucstr ( - hio_uch_t* buf, - hio_oow_t bsz, - const hio_uch_t* fmt, - const hio_ucs_t str[] -); - -HIO_EXPORT hio_oow_t hio_copy_fmt_bcses_to_bcstr ( - hio_bch_t* buf, - hio_oow_t bsz, - const hio_bch_t* fmt, - const hio_bcs_t str[] -); - -/* ------------------------------------ */ - -HIO_EXPORT hio_oow_t hio_count_ucstr ( - const hio_uch_t* str -); - -HIO_EXPORT hio_oow_t hio_count_bcstr ( - const hio_bch_t* str -); - -HIO_EXPORT hio_oow_t hio_count_ucstr_limited ( - const hio_uch_t* str, - hio_oow_t maxlen -); - -HIO_EXPORT hio_oow_t hio_count_bcstr_limited ( - const hio_bch_t* str, - hio_oow_t maxlen -); - -/* ------------------------------------ */ - -/** - * The hio_equal_uchars() function determines equality of two strings - * of the same length \a len. - */ -HIO_EXPORT int hio_equal_uchars ( - const hio_uch_t* str1, - const hio_uch_t* str2, - hio_oow_t len -); - -HIO_EXPORT int hio_equal_bchars ( - const hio_bch_t* str1, - const hio_bch_t* str2, - hio_oow_t len -); - -/* ------------------------------------ */ - - -HIO_EXPORT void hio_fill_uchars ( - hio_uch_t* dst, - const hio_uch_t ch, - hio_oow_t len -); - -HIO_EXPORT void hio_fill_bchars ( - hio_bch_t* dst, - const hio_bch_t ch, - hio_oow_t len -); - -/* ------------------------------------ */ - -HIO_EXPORT const hio_bch_t* hio_find_bcstr_word_in_bcstr ( - const hio_bch_t* str, - const hio_bch_t* word, - hio_bch_t extra_delim, - int ignorecase -); - -HIO_EXPORT const hio_uch_t* hio_find_ucstr_word_in_ucstr ( - const hio_uch_t* str, - const hio_uch_t* word, - hio_uch_t extra_delim, - int ignorecase -); - -HIO_EXPORT hio_uch_t* hio_find_uchar_in_uchars ( - const hio_uch_t* ptr, - hio_oow_t len, - hio_uch_t c -); - -HIO_EXPORT hio_bch_t* hio_find_bchar_in_bchars ( - const hio_bch_t* ptr, - hio_oow_t len, - hio_bch_t c -); - -HIO_EXPORT hio_uch_t* hio_rfind_uchar_in_uchars ( - const hio_uch_t* ptr, - hio_oow_t len, - hio_uch_t c -); - -HIO_EXPORT hio_bch_t* hio_rfind_bchar_in_bchars ( - const hio_bch_t* ptr, - hio_oow_t len, - hio_bch_t c -); - -HIO_EXPORT hio_uch_t* hio_find_uchar_in_ucstr ( - const hio_uch_t* ptr, - hio_uch_t c -); - -HIO_EXPORT hio_bch_t* hio_find_bchar_in_bcstr ( - const hio_bch_t* ptr, - hio_bch_t c -); - -HIO_EXPORT hio_uch_t* hio_rfind_uchar_in_ucstr ( - const hio_uch_t* ptr, - hio_uch_t c -); - -HIO_EXPORT hio_bch_t* hio_rfind_bchar_in_bcstr ( - const hio_bch_t* ptr, - hio_bch_t c -); - -HIO_EXPORT hio_uch_t* hio_find_uchars_in_uchars ( - const hio_uch_t* str, - hio_oow_t strsz, - const hio_uch_t* sub, - hio_oow_t subsz, - int inorecase -); - -HIO_EXPORT hio_bch_t* hio_find_bchars_in_bchars ( - const hio_bch_t* str, - hio_oow_t strsz, - const hio_bch_t* sub, - hio_oow_t subsz, - int inorecase -); - -HIO_EXPORT hio_uch_t* hio_rfind_uchars_in_uchars ( - const hio_uch_t* str, - hio_oow_t strsz, - const hio_uch_t* sub, - hio_oow_t subsz, - int inorecase -); - -HIO_EXPORT hio_bch_t* hio_rfind_bchars_in_bchars ( - const hio_bch_t* str, - hio_oow_t strsz, - const hio_bch_t* sub, - hio_oow_t subsz, - int inorecase -); - -/* ------------------------------------ */ - -HIO_EXPORT hio_oow_t hio_rotate_uchars ( - hio_uch_t* str, - hio_oow_t len, - int dir, - hio_oow_t n -); - -HIO_EXPORT hio_oow_t hio_rotate_bchars ( - hio_bch_t* str, - hio_oow_t len, - int dir, - hio_oow_t n -); - - -HIO_EXPORT hio_uch_t* hio_trim_uchars ( - const hio_uch_t* str, - hio_oow_t* len, - int flags -); - -HIO_EXPORT hio_bch_t* hio_trim_bchars ( - const hio_bch_t* str, - hio_oow_t* len, - int flags -); - -HIO_EXPORT int hio_split_ucstr ( - hio_uch_t* s, - const hio_uch_t* delim, - hio_uch_t lquote, - hio_uch_t rquote, - hio_uch_t escape -); - -HIO_EXPORT int hio_split_bcstr ( - hio_bch_t* s, - const hio_bch_t* delim, - hio_bch_t lquote, - hio_bch_t rquote, - hio_bch_t escape -); - - -#if defined(HIO_OOCH_IS_UCH) -# define hio_count_oocstr hio_count_ucstr -# define hio_count_oocstr_limited hio_count_ucstr_limited - -# define hio_equal_oochars hio_equal_uchars -# define hio_comp_oochars hio_comp_uchars -# define hio_comp_oocstr_bcstr hio_comp_ucstr_bcstr -# define hio_comp_oochars_bcstr hio_comp_uchars_bcstr -# define hio_comp_oochars_ucstr hio_comp_uchars_ucstr -# define hio_comp_oochars_oocstr hio_comp_uchars_ucstr -# define hio_comp_oocstr hio_comp_ucstr - -# define hio_copy_oochars hio_copy_uchars -# define hio_copy_bchars_to_oochars hio_copy_bchars_to_uchars -# define hio_copy_oochars_to_bchars hio_copy_uchars_to_bchars -# define hio_copy_uchars_to_oochars hio_copy_uchars -# define hio_copy_oochars_to_uchars hio_copy_uchars - -# define hio_copy_oochars_to_oocstr hio_copy_uchars_to_ucstr -# define hio_copy_oochars_to_oocstr_unlimited hio_copy_uchars_to_ucstr_unlimited -# define hio_copy_oocstr hio_copy_ucstr -# define hio_copy_oocstr_unlimited hio_copy_ucstr_unlimited -# define hio_copy_fmt_oocses_to_oocstr hio_copy_fmt_ucses_to_ucstr -# define hio_copy_fmt_oocstr_to_oocstr hio_copy_fmt_ucstr_to_ucstr - -# define hio_concat_oochars_to_ucstr hio_concat_uchars_to_ucstr -# define hio_concat_oocstr hio_concat_ucstr - -# define hio_fill_oochars hio_fill_uchars -# define hio_find_oocstr_word_in_oocstr hio_find_ucstr_word_in_ucstr -# define hio_find_oochar_in_oochars hio_find_uchar_in_uchars -# define hio_rfind_oochar_in_oochars hio_rfind_uchar_in_uchars -# define hio_find_oochar_in_oocstr hio_find_uchar_in_ucstr - -# define hio_trim_oochars hio_trim_uchars -# define hio_split_oocstr hio_split_ucstr -#else -# define hio_count_oocstr hio_count_bcstr -# define hio_count_oocstr_limited hio_count_bcstr_limited - -# define hio_equal_oochars hio_equal_bchars -# define hio_comp_oochars hio_comp_bchars -# define hio_comp_oocstr_bcstr hio_comp_bcstr -# define hio_comp_oochars_bcstr hio_comp_bchars_bcstr -# define hio_comp_oochars_ucstr hio_comp_bchars_ucstr -# define hio_comp_oochars_oocstr hio_comp_bchars_bcstr -# define hio_comp_oocstr hio_comp_bcstr - -# define hio_copy_oochars hio_copy_bchars -# define hio_copy_bchars_to_oochars hio_copy_bchars -# define hio_copy_oochars_to_bchars hio_copy_bchars -# define hio_copy_uchars_to_oochars hio_copy_uchars_to_bchars -# define hio_copy_oochars_to_uchars hio_copy_bchars_to_uchars - -# define hio_copy_oochars_to_oocstr hio_copy_bchars_to_bcstr -# define hio_copy_oochars_to_oocstr_unlimited hio_copy_bchars_to_bcstr_unlimited -# define hio_copy_oocstr hio_copy_bcstr -# define hio_copy_oocstr_unlimited hio_copy_bcstr_unlimited -# define hio_copy_fmt_oocses_to_oocstr hio_copy_fmt_bcses_to_bcstr -# define hio_copy_fmt_oocstr_to_oocstr hio_copy_fmt_bcstr_to_bcstr - -# define hio_concat_oochars_to_bcstr hio_concat_bchars_to_bcstr -# define hio_concat_oocstr hio_concat_bcstr - -# define hio_fill_oochars hio_fill_bchars -# define hio_find_oocstr_word_in_oocstr hio_find_bcstr_word_in_bcstr -# define hio_find_oochar_in_oochars hio_find_bchar_in_bchars -# define hio_rfind_oochar_in_oochars hio_rfind_bchar_in_bchars -# define hio_find_oochar_in_oocstr hio_find_bchar_in_bcstr - -# define hio_trim_oochars hio_trim_bchars -# define hio_split_oocstr hio_split_bcstr -#endif -/* ------------------------------------------------------------------------- */ - -#define HIO_BYTE_TO_BCSTR_RADIXMASK (0xFF) -#define HIO_BYTE_TO_BCSTR_LOWERCASE (1 << 8) - -HIO_EXPORT hio_oow_t hio_byte_to_bcstr ( - hio_uint8_t byte, - hio_bch_t* buf, - hio_oow_t size, - int flagged_radix, - hio_bch_t fill -); - -#define HIO_BYTE_TO_UCSTR_RADIXMASK (0xFF) -#define HIO_BYTE_TO_UCSTR_LOWERCASE (1 << 8) - -HIO_EXPORT hio_oow_t hio_byte_to_ucstr ( - hio_uint8_t byte, - hio_uch_t* buf, - hio_oow_t size, - int flagged_radix, - hio_uch_t fill -); - -/* ------------------------------------------------------------------------- */ -#define HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) (((!!(e)) << 0) | ((!!(ltrim)) << 2) | ((!!(rtrim)) << 3) | ((base) << 8)) -#define HIO_CHARS_TO_INT_GET_OPTION_E(option) ((option) & 1) -#define HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) ((option) & 4) -#define HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) ((option) & 8) -#define HIO_CHARS_TO_INT_GET_OPTION_BASE(option) ((option) >> 8) - -#define HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) (((!!(e)) << 0) | ((!!(ltrim)) << 2) | ((!!(rtrim)) << 3) | ((base) << 8)) -#define HIO_CHARS_TO_UINT_GET_OPTION_E(option) ((option) & 1) -#define HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) ((option) & 4) -#define HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) ((option) & 8) -#define HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) ((option) >> 8) - -#define HIO_OOCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) -#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) -#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) -#define HIO_OOCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) - -#define HIO_OOCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) -#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) -#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) -#define HIO_OOCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) - -#define HIO_UCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_UCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) -#define HIO_UCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) -#define HIO_UCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) -#define HIO_UCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) - -#define HIO_BCHARS_TO_INTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_INT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_BCHARS_TO_INTMAX_GET_OPTION_E(option) HIO_CHARS_TO_INT_GET_OPTION_E(option) -#define HIO_BCHARS_TO_INTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option) -#define HIO_BCHARS_TO_INTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option) -#define HIO_BCHARS_TO_INTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_INT_GET_OPTION_BASE(option) - -#define HIO_UCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) -#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) -#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) -#define HIO_UCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) - -#define HIO_BCHARS_TO_UINTMAX_MAKE_OPTION(e,ltrim,rtrim,base) HIO_CHARS_TO_UINT_MAKE_OPTION(e,ltrim,rtrim,base) -#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_E(option) HIO_CHARS_TO_UINT_GET_OPTION_E(option) -#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option) -#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option) HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option) -#define HIO_BCHARS_TO_UINTMAX_GET_OPTION_BASE(option) HIO_CHARS_TO_UINT_GET_OPTION_BASE(option) - -HIO_EXPORT hio_intmax_t hio_uchars_to_intmax ( - const hio_uch_t* str, - hio_oow_t len, - int option, - const hio_uch_t** endptr, - int* is_sober -); - -HIO_EXPORT hio_intmax_t hio_bchars_to_intmax ( - const hio_bch_t* str, - hio_oow_t len, - int option, - const hio_bch_t** endptr, - int* is_sober -); - -HIO_EXPORT hio_uintmax_t hio_uchars_to_uintmax ( - const hio_uch_t* str, - hio_oow_t len, - int option, - const hio_uch_t** endptr, - int* is_sober -); - -HIO_EXPORT hio_uintmax_t hio_bchars_to_uintmax ( - const hio_bch_t* str, - hio_oow_t len, - int option, - const hio_bch_t** endptr, - int* is_sober -); -#if defined(HIO_OOCH_IS_UCH) -# define hio_oochars_to_intmax hio_uchars_to_intmax -# define hio_oochars_to_uintmax hio_uchars_to_uintmax -#else -# define hio_oochars_to_intmax hio_bchars_to_intmax -# define hio_oochars_to_uintmax hio_bchars_to_uintmax -#endif - -/* ------------------------------------------------------------------------- */ - -#if defined(HIO_OOCH_IS_UCH) -# define hio_conv_oocstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hio_conv_ucstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) -# define hio_conv_oochars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hio_conv_uchars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) -#else -# define hio_conv_oocstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hio_conv_bcstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) -# define hio_conv_oochars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hio_conv_bchars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) -#endif - - -HIO_EXPORT int hio_conv_bcstr_to_ucstr_with_cmgr ( - const hio_bch_t* bcs, - hio_oow_t* bcslen, - hio_uch_t* ucs, - hio_oow_t* ucslen, - hio_cmgr_t* cmgr, - int all -); - -HIO_EXPORT int hio_conv_bchars_to_uchars_with_cmgr ( - const hio_bch_t* bcs, - hio_oow_t* bcslen, - hio_uch_t* ucs, - hio_oow_t* ucslen, - hio_cmgr_t* cmgr, - int all -); - -HIO_EXPORT int hio_conv_ucstr_to_bcstr_with_cmgr ( - const hio_uch_t* ucs, - hio_oow_t* ucslen, - hio_bch_t* bcs, - hio_oow_t* bcslen, - hio_cmgr_t* cmgr -); - -HIO_EXPORT int hio_conv_uchars_to_bchars_with_cmgr ( - const hio_uch_t* ucs, - hio_oow_t* ucslen, - hio_bch_t* bcs, - hio_oow_t* bcslen, - hio_cmgr_t* cmgr -); - -/* ------------------------------------------------------------------------- */ - -HIO_EXPORT hio_cmgr_t* hio_get_utf8_cmgr ( - void -); - -/** - * The hio_conv_uchars_to_utf8() function converts a unicode character string \a ucs - * to a UTF8 string and writes it into the buffer pointed to by \a bcs, but - * not more than \a bcslen bytes including the terminating null. - * - * Upon return, \a bcslen is modified to the actual number of bytes written to - * \a bcs excluding the terminating null; \a ucslen is modified to the number of - * wide characters converted. - * - * You may pass #HIO_NULL for \a bcs to dry-run conversion or to get the - * required buffer size for conversion. -2 is never returned in this case. - * - * \return - * - 0 on full conversion, - * - -1 on no or partial conversion for an illegal character encountered, - * - -2 on no or partial conversion for a small buffer. - * - * \code - * const hio_uch_t ucs[] = { 'H', 'e', 'l', 'l', 'o' }; - * hio_bch_t bcs[10]; - * hio_oow_t ucslen = 5; - * hio_oow_t bcslen = HIO_COUNTOF(bcs); - * n = hio_conv_uchars_to_utf8 (ucs, &ucslen, bcs, &bcslen); - * if (n <= -1) - * { - * // conversion error - * } - * \endcode - */ -HIO_EXPORT int hio_conv_uchars_to_utf8 ( - const hio_uch_t* ucs, - hio_oow_t* ucslen, - hio_bch_t* bcs, - hio_oow_t* bcslen -); - -/** - * The hio_conv_utf8_to_uchars() function converts a UTF8 string to a uncide string. - * - * It never returns -2 if \a ucs is #HIO_NULL. - * - * \code - * const hio_bch_t* bcs = "test string"; - * hio_uch_t ucs[100]; - * hio_oow_t ucslen = HIO_COUNTOF(buf), n; - * hio_oow_t bcslen = 11; - * int n; - * n = hio_conv_utf8_to_uchars (bcs, &bcslen, ucs, &ucslen); - * if (n <= -1) { invalid/incomplenete sequence or buffer to small } - * \endcode - * - * The resulting \a ucslen can still be greater than 0 even if the return - * value is negative. The value indiates the number of characters converted - * before the error has occurred. - * - * \return 0 on success. - * -1 if \a bcs contains an illegal character. - * -2 if the wide-character string buffer is too small. - * -3 if \a bcs is not a complete sequence. - */ -HIO_EXPORT int hio_conv_utf8_to_uchars ( - const hio_bch_t* bcs, - hio_oow_t* bcslen, - hio_uch_t* ucs, - hio_oow_t* ucslen -); - - -HIO_EXPORT int hio_conv_ucstr_to_utf8 ( - const hio_uch_t* ucs, - hio_oow_t* ucslen, - hio_bch_t* bcs, - hio_oow_t* bcslen -); - -HIO_EXPORT int hio_conv_utf8_to_ucstr ( - const hio_bch_t* bcs, - hio_oow_t* bcslen, - hio_uch_t* ucs, - hio_oow_t* ucslen -); - - -HIO_EXPORT hio_oow_t hio_uc_to_utf8 ( - hio_uch_t uc, - hio_bch_t* utf8, - hio_oow_t size -); - -HIO_EXPORT hio_oow_t hio_utf8_to_uc ( - const hio_bch_t* utf8, - hio_oow_t size, - hio_uch_t* uc -); - -/* ========================================================================= - * TIME CALCULATION WITH OVERFLOW/UNDERFLOW DETECTION - * ========================================================================= */ - -/** - * The hio_add_ntime() function adds two time structures pointed to by \a x and \a y - * and stores the result in the structure pointed to by \a z. If it detects overflow/ - * underflow, it stores the largest/least possible value respectively. - * You may use the HIO_ADD_NTIME() macro if overflow/underflow check isn't needed. - */ -HIO_EXPORT void hio_add_ntime ( - hio_ntime_t* z, - const hio_ntime_t* x, - const hio_ntime_t* y -); - -/** - * The hio_sub_ntime() function subtracts the time value \a y from the time value \a x - * and stores the result in the structure pointed to by \a z. If it detects overflow/ - * underflow, it stores the largest/least possible value respectively. - * You may use the HIO_SUB_NTIME() macro if overflow/underflow check isn't needed. - */ -HIO_EXPORT void hio_sub_ntime ( - hio_ntime_t* z, - const hio_ntime_t* x, - const hio_ntime_t* y -); - - -/* ========================================================================= - * BIT SWAP - * ========================================================================= */ -#if defined(HIO_HAVE_INLINE) - -#if defined(HIO_HAVE_UINT16_T) -static HIO_INLINE hio_uint16_t hio_bswap16 (hio_uint16_t x) -{ -#if defined(HIO_HAVE_BUILTIN_BSWAP16) - return __builtin_bswap16(x); -#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64) || defined(__i386) || defined(i386)) - __asm__ volatile ("xchgb %b0, %h0" : "=Q"(x): "0"(x)); - return x; -#else - return (x << 8) | (x >> 8); -#endif -} -#endif - -#if defined(HIO_HAVE_UINT32_T) -static HIO_INLINE hio_uint32_t hio_bswap32 (hio_uint32_t x) -{ -#if defined(HIO_HAVE_BUILTIN_BSWAP32) - return __builtin_bswap32(x); -#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64) || defined(__i386) || defined(i386)) - __asm__ volatile ("bswapl %0" : "=r"(x) : "0"(x)); - return x; -#else - return ((x >> 24)) | - ((x >> 8) & ((hio_uint32_t)0xff << 8)) | - ((x << 8) & ((hio_uint32_t)0xff << 16)) | - ((x << 24)); -#endif -} -#endif - -#if defined(HIO_HAVE_UINT64_T) -static HIO_INLINE hio_uint64_t hio_bswap64 (hio_uint64_t x) -{ -#if defined(HIO_HAVE_BUILTIN_BSWAP64) - return __builtin_bswap64(x); -#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64)) - __asm__ volatile ("bswapq %0" : "=r"(x) : "0"(x)); - return x; -#else - return ((x >> 56)) | - ((x >> 40) & ((hio_uint64_t)0xff << 8)) | - ((x >> 24) & ((hio_uint64_t)0xff << 16)) | - ((x >> 8) & ((hio_uint64_t)0xff << 24)) | - ((x << 8) & ((hio_uint64_t)0xff << 32)) | - ((x << 24) & ((hio_uint64_t)0xff << 40)) | - ((x << 40) & ((hio_uint64_t)0xff << 48)) | - ((x << 56)); -#endif -} -#endif - -#if defined(HIO_HAVE_UINT128_T) -static HIO_INLINE hio_uint128_t hio_bswap128 (hio_uint128_t x) -{ - return ((x >> 120)) | - ((x >> 104) & ((hio_uint128_t)0xff << 8)) | - ((x >> 88) & ((hio_uint128_t)0xff << 16)) | - ((x >> 72) & ((hio_uint128_t)0xff << 24)) | - ((x >> 56) & ((hio_uint128_t)0xff << 32)) | - ((x >> 40) & ((hio_uint128_t)0xff << 40)) | - ((x >> 24) & ((hio_uint128_t)0xff << 48)) | - ((x >> 8) & ((hio_uint128_t)0xff << 56)) | - ((x << 8) & ((hio_uint128_t)0xff << 64)) | - ((x << 24) & ((hio_uint128_t)0xff << 72)) | - ((x << 40) & ((hio_uint128_t)0xff << 80)) | - ((x << 56) & ((hio_uint128_t)0xff << 88)) | - ((x << 72) & ((hio_uint128_t)0xff << 96)) | - ((x << 88) & ((hio_uint128_t)0xff << 104)) | - ((x << 104) & ((hio_uint128_t)0xff << 112)) | - ((x << 120)); -} -#endif - -#else - -#if defined(HIO_HAVE_UINT16_T) -# define hio_bswap16(x) ((hio_uint16_t)(((hio_uint16_t)(x)) << 8) | (((hio_uint16_t)(x)) >> 8)) -#endif - -#if defined(HIO_HAVE_UINT32_T) -# define hio_bswap32(x) ((hio_uint32_t)(((((hio_uint32_t)(x)) >> 24)) | \ - ((((hio_uint32_t)(x)) >> 8) & ((hio_uint32_t)0xff << 8)) | \ - ((((hio_uint32_t)(x)) << 8) & ((hio_uint32_t)0xff << 16)) | \ - ((((hio_uint32_t)(x)) << 24)))) -#endif - -#if defined(HIO_HAVE_UINT64_T) -# define hio_bswap64(x) ((hio_uint64_t)(((((hio_uint64_t)(x)) >> 56)) | \ - ((((hio_uint64_t)(x)) >> 40) & ((hio_uint64_t)0xff << 8)) | \ - ((((hio_uint64_t)(x)) >> 24) & ((hio_uint64_t)0xff << 16)) | \ - ((((hio_uint64_t)(x)) >> 8) & ((hio_uint64_t)0xff << 24)) | \ - ((((hio_uint64_t)(x)) << 8) & ((hio_uint64_t)0xff << 32)) | \ - ((((hio_uint64_t)(x)) << 24) & ((hio_uint64_t)0xff << 40)) | \ - ((((hio_uint64_t)(x)) << 40) & ((hio_uint64_t)0xff << 48)) | \ - ((((hio_uint64_t)(x)) << 56)))) -#endif - -#if defined(HIO_HAVE_UINT128_T) -# define hio_bswap128(x) ((hio_uint128_t)(((((hio_uint128_t)(x)) >> 120)) | \ - ((((hio_uint128_t)(x)) >> 104) & ((hio_uint128_t)0xff << 8)) | \ - ((((hio_uint128_t)(x)) >> 88) & ((hio_uint128_t)0xff << 16)) | \ - ((((hio_uint128_t)(x)) >> 72) & ((hio_uint128_t)0xff << 24)) | \ - ((((hio_uint128_t)(x)) >> 56) & ((hio_uint128_t)0xff << 32)) | \ - ((((hio_uint128_t)(x)) >> 40) & ((hio_uint128_t)0xff << 40)) | \ - ((((hio_uint128_t)(x)) >> 24) & ((hio_uint128_t)0xff << 48)) | \ - ((((hio_uint128_t)(x)) >> 8) & ((hio_uint128_t)0xff << 56)) | \ - ((((hio_uint128_t)(x)) << 8) & ((hio_uint128_t)0xff << 64)) | \ - ((((hio_uint128_t)(x)) << 24) & ((hio_uint128_t)0xff << 72)) | \ - ((((hio_uint128_t)(x)) << 40) & ((hio_uint128_t)0xff << 80)) | \ - ((((hio_uint128_t)(x)) << 56) & ((hio_uint128_t)0xff << 88)) | \ - ((((hio_uint128_t)(x)) << 72) & ((hio_uint128_t)0xff << 96)) | \ - ((((hio_uint128_t)(x)) << 88) & ((hio_uint128_t)0xff << 104)) | \ - ((((hio_uint128_t)(x)) << 104) & ((hio_uint128_t)0xff << 112)) | \ - ((((hio_uint128_t)(x)) << 120)))) -#endif - -#endif /* HIO_HAVE_INLINE */ - - -#if defined(HIO_ENDIAN_LITTLE) - -# if defined(HIO_HAVE_UINT16_T) -# define hio_hton16(x) hio_bswap16(x) -# define hio_ntoh16(x) hio_bswap16(x) -# define hio_htobe16(x) hio_bswap16(x) -# define hio_be16toh(x) hio_bswap16(x) -# define hio_htole16(x) ((hio_uint16_t)(x)) -# define hio_le16toh(x) ((hio_uint16_t)(x)) -# endif - -# if defined(HIO_HAVE_UINT32_T) -# define hio_hton32(x) hio_bswap32(x) -# define hio_ntoh32(x) hio_bswap32(x) -# define hio_htobe32(x) hio_bswap32(x) -# define hio_be32toh(x) hio_bswap32(x) -# define hio_htole32(x) ((hio_uint32_t)(x)) -# define hio_le32toh(x) ((hio_uint32_t)(x)) -# endif - -# if defined(HIO_HAVE_UINT64_T) -# define hio_hton64(x) hio_bswap64(x) -# define hio_ntoh64(x) hio_bswap64(x) -# define hio_htobe64(x) hio_bswap64(x) -# define hio_be64toh(x) hio_bswap64(x) -# define hio_htole64(x) ((hio_uint64_t)(x)) -# define hio_le64toh(x) ((hio_uint64_t)(x)) -# endif - -# if defined(HIO_HAVE_UINT128_T) - -# define hio_hton128(x) hio_bswap128(x) -# define hio_ntoh128(x) hio_bswap128(x) -# define hio_htobe128(x) hio_bswap128(x) -# define hio_be128toh(x) hio_bswap128(x) -# define hio_htole128(x) ((hio_uint128_t)(x)) -# define hio_le128toh(x) ((hio_uint128_t)(x)) -# endif - -#elif defined(HIO_ENDIAN_BIG) - -# if defined(HIO_HAVE_UINT16_T) -# define hio_hton16(x) ((hio_uint16_t)(x)) -# define hio_ntoh16(x) ((hio_uint16_t)(x)) -# define hio_htobe16(x) ((hio_uint16_t)(x)) -# define hio_be16toh(x) ((hio_uint16_t)(x)) -# define hio_htole16(x) hio_bswap16(x) -# define hio_le16toh(x) hio_bswap16(x) -# endif - -# if defined(HIO_HAVE_UINT32_T) -# define hio_hton32(x) ((hio_uint32_t)(x)) -# define hio_ntoh32(x) ((hio_uint32_t)(x)) -# define hio_htobe32(x) ((hio_uint32_t)(x)) -# define hio_be32toh(x) ((hio_uint32_t)(x)) -# define hio_htole32(x) hio_bswap32(x) -# define hio_le32toh(x) hio_bswap32(x) -# endif - -# if defined(HIO_HAVE_UINT64_T) -# define hio_hton64(x) ((hio_uint64_t)(x)) -# define hio_ntoh64(x) ((hio_uint64_t)(x)) -# define hio_htobe64(x) ((hio_uint64_t)(x)) -# define hio_be64toh(x) ((hio_uint64_t)(x)) -# define hio_htole64(x) hio_bswap64(x) -# define hio_le64toh(x) hio_bswap64(x) -# endif - -# if defined(HIO_HAVE_UINT128_T) -# define hio_hton128(x) ((hio_uint128_t)(x)) -# define hio_ntoh128(x) ((hio_uint128_t)(x)) -# define hio_htobe128(x) ((hio_uint128_t)(x)) -# define hio_be128toh(x) ((hio_uint128_t)(x)) -# define hio_htole128(x) hio_bswap128(x) -# define hio_le128toh(x) hio_bswap128(x) -# endif - -#else -# error UNKNOWN ENDIAN -#endif - - -/* ========================================================================= - * SIP-HASH-PRF - * ========================================================================= */ -HIO_EXPORT void hio_sip_hash_24 ( - const hio_uint8_t key[16], - const void* dptr, - hio_oow_t dlen, - hio_uint8_t out[8] -); - -#if defined(__cplusplus) -} -#endif - -/* Some C++ utilities below */ -#if defined(__cplusplus) - -/* -static inline bool is_space (char c) { return isspace(c); } -static inline bool is_wspace (wchar_t c) { return iswspace(c); } -unsigned x = hio_chars_to_uint("0x12345", 7, 0, NULL, NULL); -unsigned y = hio_chars_to_uint(L"0x12345", 7, 0, NULL, NULL); -int a = hio_chars_to_int("-0x12345", 8, 0, NULL, NULL); -int b = hio_chars_to_int(L"-0x12345", 8, 0, NULL, NULL); -*/ -templatefn_chars_to_int(hio_chars_to_int, CHAR_TYPE, INT_TYPE, IS_SPACE, HIO_CHARS_TO_INT) -templatefn_chars_to_uint(hio_chars_to_uint, CHAR_TYPE, UINT_TYPE, IS_SPACE, HIO_CHARS_TO_UINT) - -#endif - -#endif diff --git a/hio/lib/pty.c b/hio/lib/pty.c index ed3db81..ad27719 100644 --- a/hio/lib/pty.c +++ b/hio/lib/pty.c @@ -33,6 +33,7 @@ #include #include #include +#include #if defined(HAVE_PTY_H) # include diff --git a/hio/lib/utl-str.c b/hio/lib/utl-str.c index ceb23ec..b6e8b42 100644 --- a/hio/lib/utl-str.c +++ b/hio/lib/utl-str.c @@ -1737,6 +1737,750 @@ exit_point: return cnt; } +hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch_t* delim, hio_oow_t delim_len, hio_ucs_t* tok, int ignorecase) +{ + const hio_uch_t* p = s, *d; + const hio_uch_t* end = s + len; + const hio_uch_t* sp = HIO_NULL, * ep = HIO_NULL; + const hio_uch_t* delim_end = delim + delim_len; + hio_uch_t c; + int delim_mode; + +#define __DELIM_NULL 0 +#define __DELIM_EMPTY 1 +#define __DELIM_SPACES 2 +#define __DELIM_NOSPACES 3 +#define __DELIM_COMPOSITE 4 + if (delim == HIO_NULL) delim_mode = __DELIM_NULL; + else + { + delim_mode = __DELIM_EMPTY; + + for (d = delim; d < delim_end; d++) + { + if (hio_is_uch_space(*d)) + { + if (delim_mode == __DELIM_EMPTY) + delim_mode = __DELIM_SPACES; + else if (delim_mode == __DELIM_NOSPACES) + { + delim_mode = __DELIM_COMPOSITE; + break; + } + } + else + { + if (delim_mode == __DELIM_EMPTY) + delim_mode = __DELIM_NOSPACES; + else if (delim_mode == __DELIM_SPACES) + { + delim_mode = __DELIM_COMPOSITE; + break; + } + } + } + + /* TODO: verify the following statement... */ + if (delim_mode == __DELIM_SPACES && delim_len == 1 && delim[0] != ' ') delim_mode = __DELIM_NOSPACES; + } + + if (delim_mode == __DELIM_NULL) + { + /* when HIO_NULL is given as "delim", it trims off the + * leading and trailing spaces characters off the source + * string "s" eventually. */ + + while (p < end && hio_is_uch_space(*p)) p++; + while (p < end) + { + c = *p; + + if (!hio_is_uch_space(c)) + { + if (sp == HIO_NULL) sp = p; + ep = p; + } + p++; + } + } + else if (delim_mode == __DELIM_EMPTY) + { + /* each character in the source string "s" becomes a token. */ + if (p < end) + { + c = *p; + sp = p; + ep = p++; + } + } + else if (delim_mode == __DELIM_SPACES) + { + /* each token is delimited by space characters. all leading + * and trailing spaces are removed. */ + + while (p < end && hio_is_uch_space(*p)) p++; + while (p < end) + { + c = *p; + if (hio_is_uch_space(c)) break; + if (sp == HIO_NULL) sp = p; + ep = p++; + } + while (p < end && hio_is_uch_space(*p)) p++; + } + else if (delim_mode == __DELIM_NOSPACES) + { + /* each token is delimited by one of charaters + * in the delimeter set "delim". */ + if (ignorecase) + { + while (p < end) + { + c = hio_to_uch_lower(*p); + for (d = delim; d < delim_end; d++) + { + if (c == hio_to_uch_lower(*d)) goto exit_loop; + } + + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + else + { + while (p < end) + { + c = *p; + for (d = delim; d < delim_end; d++) + { + if (c == *d) goto exit_loop; + } + + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + } + else /* if (delim_mode == __DELIM_COMPOSITE) */ + { + /* each token is delimited by one of non-space charaters + * in the delimeter set "delim". however, all space characters + * surrounding the token are removed */ + while (p < end && hio_is_uch_space(*p)) p++; + if (ignorecase) + { + while (p < end) + { + c = hio_to_uch_lower(*p); + if (hio_is_uch_space(c)) + { + p++; + continue; + } + for (d = delim; d < delim_end; d++) + { + if (c == hio_to_uch_lower(*d)) goto exit_loop; + } + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + else + { + while (p < end) + { + c = *p; + if (hio_is_uch_space(c)) + { + p++; + continue; + } + for (d = delim; d < delim_end; d++) + { + if (c == *d) goto exit_loop; + } + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + } + +exit_loop: + if (sp == HIO_NULL) + { + tok->ptr = HIO_NULL; + tok->len = (hio_oow_t)0; + } + else + { + tok->ptr = (hio_uch_t*)sp; + tok->len = ep - sp + 1; + } + + /* if HIO_NULL is returned, this function should not be called again */ + if (p >= end) return HIO_NULL; + if (delim_mode == __DELIM_EMPTY || delim_mode == __DELIM_SPACES) return (hio_uch_t*)p; + return (hio_uch_t*)++p; +} + +hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch_t* delim, hio_oow_t delim_len, hio_bcs_t* tok, int ignorecase) +{ + const hio_bch_t* p = s, *d; + const hio_bch_t* end = s + len; + const hio_bch_t* sp = HIO_NULL, * ep = HIO_NULL; + const hio_bch_t* delim_end = delim + delim_len; + hio_bch_t c; + int delim_mode; + +#define __DELIM_NULL 0 +#define __DELIM_EMPTY 1 +#define __DELIM_SPACES 2 +#define __DELIM_NOSPACES 3 +#define __DELIM_COMPOSITE 4 + if (delim == HIO_NULL) delim_mode = __DELIM_NULL; + else + { + delim_mode = __DELIM_EMPTY; + + for (d = delim; d < delim_end; d++) + { + if (hio_is_bch_space(*d)) + { + if (delim_mode == __DELIM_EMPTY) + delim_mode = __DELIM_SPACES; + else if (delim_mode == __DELIM_NOSPACES) + { + delim_mode = __DELIM_COMPOSITE; + break; + } + } + else + { + if (delim_mode == __DELIM_EMPTY) + delim_mode = __DELIM_NOSPACES; + else if (delim_mode == __DELIM_SPACES) + { + delim_mode = __DELIM_COMPOSITE; + break; + } + } + } + + /* TODO: verify the following statement... */ + if (delim_mode == __DELIM_SPACES && delim_len == 1 && delim[0] != ' ') delim_mode = __DELIM_NOSPACES; + } + + if (delim_mode == __DELIM_NULL) + { + /* when HIO_NULL is given as "delim", it trims off the + * leading and trailing spaces characters off the source + * string "s" eventually. */ + + while (p < end && hio_is_bch_space(*p)) p++; + while (p < end) + { + c = *p; + + if (!hio_is_bch_space(c)) + { + if (sp == HIO_NULL) sp = p; + ep = p; + } + p++; + } + } + else if (delim_mode == __DELIM_EMPTY) + { + /* each character in the source string "s" becomes a token. */ + if (p < end) + { + c = *p; + sp = p; + ep = p++; + } + } + else if (delim_mode == __DELIM_SPACES) + { + /* each token is delimited by space characters. all leading + * and trailing spaces are removed. */ + + while (p < end && hio_is_bch_space(*p)) p++; + while (p < end) + { + c = *p; + if (hio_is_bch_space(c)) break; + if (sp == HIO_NULL) sp = p; + ep = p++; + } + while (p < end && hio_is_bch_space(*p)) p++; + } + else if (delim_mode == __DELIM_NOSPACES) + { + /* each token is delimited by one of charaters + * in the delimeter set "delim". */ + if (ignorecase) + { + while (p < end) + { + c = hio_to_bch_lower(*p); + for (d = delim; d < delim_end; d++) + { + if (c == hio_to_bch_lower(*d)) goto exit_loop; + } + + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + else + { + while (p < end) + { + c = *p; + for (d = delim; d < delim_end; d++) + { + if (c == *d) goto exit_loop; + } + + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + } + else /* if (delim_mode == __DELIM_COMPOSITE) */ + { + /* each token is delimited by one of non-space charaters + * in the delimeter set "delim". however, all space characters + * surrounding the token are removed */ + while (p < end && hio_is_bch_space(*p)) p++; + if (ignorecase) + { + while (p < end) + { + c = hio_to_bch_lower(*p); + if (hio_is_bch_space(c)) + { + p++; + continue; + } + for (d = delim; d < delim_end; d++) + { + if (c == hio_to_bch_lower(*d)) goto exit_loop; + } + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + else + { + while (p < end) + { + c = *p; + if (hio_is_bch_space(c)) + { + p++; + continue; + } + for (d = delim; d < delim_end; d++) + { + if (c == *d) goto exit_loop; + } + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + } + +exit_loop: + if (sp == HIO_NULL) + { + tok->ptr = HIO_NULL; + tok->len = (hio_oow_t)0; + } + else + { + tok->ptr = (hio_bch_t*)sp; + tok->len = ep - sp + 1; + } + + /* if HIO_NULL is returned, this function should not be called again */ + if (p >= end) return HIO_NULL; + if (delim_mode == __DELIM_EMPTY || delim_mode == __DELIM_SPACES) return (hio_bch_t*)p; + return (hio_bch_t*)++p; +} + +hio_oow_t hio_byte_to_ucstr (hio_uint8_t byte, hio_uch_t* buf, hio_oow_t size, int flagged_radix, hio_uch_t fill) +{ + hio_uch_t tmp[(HIO_SIZEOF(hio_uint8_t) * HIO_BITS_PER_BYTE)]; + hio_uch_t* p = tmp, * bp = buf, * be = buf + size - 1; + int radix; + hio_uch_t radix_char; + + radix = (flagged_radix & HIO_BYTE_TO_UCSTR_RADIXMASK); + radix_char = (flagged_radix & HIO_BYTE_TO_UCSTR_LOWERCASE)? 'a': 'A'; + if (radix < 2 || radix > 36 || size <= 0) return 0; + + do + { + hio_uint8_t digit = byte % radix; + if (digit < 10) *p++ = digit + '0'; + else *p++ = digit + radix_char - 10; + byte /= radix; + } + while (byte > 0); + + if (fill != '\0') + { + while (size - 1 > p - tmp) + { + *bp++ = fill; + size--; + } + } + + while (p > tmp && bp < be) *bp++ = *--p; + *bp = '\0'; + return bp - buf; +} + +hio_oow_t hio_byte_to_bcstr (hio_uint8_t byte, hio_bch_t* buf, hio_oow_t size, int flagged_radix, hio_bch_t fill) +{ + hio_bch_t tmp[(HIO_SIZEOF(hio_uint8_t) * HIO_BITS_PER_BYTE)]; + hio_bch_t* p = tmp, * bp = buf, * be = buf + size - 1; + int radix; + hio_bch_t radix_char; + + radix = (flagged_radix & HIO_BYTE_TO_BCSTR_RADIXMASK); + radix_char = (flagged_radix & HIO_BYTE_TO_BCSTR_LOWERCASE)? 'a': 'A'; + if (radix < 2 || radix > 36 || size <= 0) return 0; + + do + { + hio_uint8_t digit = byte % radix; + if (digit < 10) *p++ = digit + '0'; + else *p++ = digit + radix_char - 10; + byte /= radix; + } + while (byte > 0); + + if (fill != '\0') + { + while (size - 1 > p - tmp) + { + *bp++ = fill; + size--; + } + } + + while (p > tmp && bp < be) *bp++ = *--p; + *bp = '\0'; + return bp - buf; +} + +hio_oow_t hio_intmax_to_ucstr (hio_intmax_t value, int radix, const hio_uch_t* prefix, hio_uch_t* buf, hio_oow_t size) +{ + hio_intmax_t t, rem; + hio_oow_t len, ret, i; + hio_oow_t prefix_len; + + prefix_len = (prefix != HIO_NULL)? hio_count_ucstr(prefix): 0; + + t = value; + if (t == 0) + { + /* zero */ + if (buf == HIO_NULL) + { + /* if buf is not given, + * return the number of bytes required */ + return prefix_len + 1; + } + + if (size < prefix_len+1) + { + /* buffer too small */ + return (hio_oow_t)-1; + } + + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + buf[prefix_len] = '0'; + if (size > prefix_len+1) buf[prefix_len+1] = '\0'; + return prefix_len+1; + } + + /* non-zero values */ + len = prefix_len; + if (t < 0) { t = -t; len++; } + while (t > 0) { len++; t /= radix; } + + if (buf == HIO_NULL) + { + /* if buf is not given, return the number of bytes required */ + return len; + } + + if (size < len) return (hio_oow_t)-1; /* buffer too small */ + if (size > len) buf[len] = '\0'; + ret = len; + + t = value; + if (t < 0) t = -t; + + while (t > 0) + { + rem = t % radix; + if (rem >= 10) + buf[--len] = (hio_uch_t)rem + 'a' - 10; + else + buf[--len] = (hio_uch_t)rem + '0'; + t /= radix; + } + + if (value < 0) + { + for (i = 1; i <= prefix_len; i++) + { + buf[i] = prefix[i-1]; + len--; + } + buf[--len] = '-'; + } + else + { + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + } + + return ret; +} + +hio_oow_t hio_intmax_to_bcstr (hio_intmax_t value, int radix, const hio_bch_t* prefix, hio_bch_t* buf, hio_oow_t size) +{ + hio_intmax_t t, rem; + hio_oow_t len, ret, i; + hio_oow_t prefix_len; + + prefix_len = (prefix != HIO_NULL)? hio_count_bcstr(prefix): 0; + + t = value; + if (t == 0) + { + /* zero */ + if (buf == HIO_NULL) + { + /* if buf is not given, + * return the number of bytes required */ + return prefix_len + 1; + } + + if (size < prefix_len+1) + { + /* buffer too small */ + return (hio_oow_t)-1; + } + + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + buf[prefix_len] = '0'; + if (size > prefix_len+1) buf[prefix_len+1] = '\0'; + return prefix_len+1; + } + + /* non-zero values */ + len = prefix_len; + if (t < 0) { t = -t; len++; } + while (t > 0) { len++; t /= radix; } + + if (buf == HIO_NULL) + { + /* if buf is not given, return the number of bytes required */ + return len; + } + + if (size < len) return (hio_oow_t)-1; /* buffer too small */ + if (size > len) buf[len] = '\0'; + ret = len; + + t = value; + if (t < 0) t = -t; + + while (t > 0) + { + rem = t % radix; + if (rem >= 10) + buf[--len] = (hio_bch_t)rem + 'a' - 10; + else + buf[--len] = (hio_bch_t)rem + '0'; + t /= radix; + } + + if (value < 0) + { + for (i = 1; i <= prefix_len; i++) + { + buf[i] = prefix[i-1]; + len--; + } + buf[--len] = '-'; + } + else + { + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + } + + return ret; +} + +hio_oow_t hio_uintmax_to_ucstr (hio_uintmax_t value, int radix, const hio_uch_t* prefix, hio_uch_t* buf, hio_oow_t size) +{ + hio_uintmax_t t, rem; + hio_oow_t len, ret, i; + hio_oow_t prefix_len; + + prefix_len = (prefix != HIO_NULL)? hio_count_ucstr(prefix): 0; + + t = value; + if (t == 0) + { + /* zero */ + if (buf == HIO_NULL) + { + /* if buf is not given, + * return the number of bytes required */ + return prefix_len + 1; + } + + if (size < prefix_len+1) + { + /* buffer too small */ + return (hio_oow_t)-1; + } + + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + buf[prefix_len] = '0'; + if (size > prefix_len+1) buf[prefix_len+1] = '\0'; + return prefix_len+1; + } + + /* non-zero values */ + len = prefix_len; + if (t < 0) { t = -t; len++; } + while (t > 0) { len++; t /= radix; } + + if (buf == HIO_NULL) + { + /* if buf is not given, return the number of bytes required */ + return len; + } + + if (size < len) return (hio_oow_t)-1; /* buffer too small */ + if (size > len) buf[len] = '\0'; + ret = len; + + t = value; + if (t < 0) t = -t; + + while (t > 0) + { + rem = t % radix; + if (rem >= 10) + buf[--len] = (hio_uch_t)rem + 'a' - 10; + else + buf[--len] = (hio_uch_t)rem + '0'; + t /= radix; + } + + if (value < 0) + { + for (i = 1; i <= prefix_len; i++) + { + buf[i] = prefix[i-1]; + len--; + } + buf[--len] = '-'; + } + else + { + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + } + + return ret; +} + +hio_oow_t hio_uintmax_to_bcstr (hio_uintmax_t value, int radix, const hio_bch_t* prefix, hio_bch_t* buf, hio_oow_t size) +{ + hio_uintmax_t t, rem; + hio_oow_t len, ret, i; + hio_oow_t prefix_len; + + prefix_len = (prefix != HIO_NULL)? hio_count_bcstr(prefix): 0; + + t = value; + if (t == 0) + { + /* zero */ + if (buf == HIO_NULL) + { + /* if buf is not given, + * return the number of bytes required */ + return prefix_len + 1; + } + + if (size < prefix_len+1) + { + /* buffer too small */ + return (hio_oow_t)-1; + } + + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + buf[prefix_len] = '0'; + if (size > prefix_len+1) buf[prefix_len+1] = '\0'; + return prefix_len+1; + } + + /* non-zero values */ + len = prefix_len; + if (t < 0) { t = -t; len++; } + while (t > 0) { len++; t /= radix; } + + if (buf == HIO_NULL) + { + /* if buf is not given, return the number of bytes required */ + return len; + } + + if (size < len) return (hio_oow_t)-1; /* buffer too small */ + if (size > len) buf[len] = '\0'; + ret = len; + + t = value; + if (t < 0) t = -t; + + while (t > 0) + { + rem = t % radix; + if (rem >= 10) + buf[--len] = (hio_bch_t)rem + 'a' - 10; + else + buf[--len] = (hio_bch_t)rem + '0'; + t /= radix; + } + + if (value < 0) + { + for (i = 1; i <= prefix_len; i++) + { + buf[i] = prefix[i-1]; + len--; + } + buf[--len] = '-'; + } + else + { + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + } + + return ret; +} + hio_intmax_t hio_uchars_to_intmax (const hio_uch_t* str, hio_oow_t len, int option, const hio_uch_t** endptr, int* is_sober) { hio_intmax_t n = 0; @@ -1790,13 +2534,11 @@ hio_intmax_t hio_uchars_to_intmax (const hio_uch_t* str, hio_oow_t len, int opti } else if (rem >= 2 && base == 16) { - if (*p == '0' && - (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && - (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -1905,13 +2647,11 @@ hio_intmax_t hio_bchars_to_intmax (const hio_bch_t* str, hio_oow_t len, int opti } else if (rem >= 2 && base == 16) { - if (*p == '0' && - (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && - (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -2015,13 +2755,11 @@ hio_uintmax_t hio_uchars_to_uintmax (const hio_uch_t* str, hio_oow_t len, int op } else if (rem >= 2 && base == 16) { - if (*p == '0' && - (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && - (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -2125,13 +2863,11 @@ hio_uintmax_t hio_bchars_to_uintmax (const hio_bch_t* str, hio_oow_t len, int op } else if (rem >= 2 && base == 16) { - if (*p == '0' && - (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && - (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ diff --git a/hio/lib/utl-str.c.m4 b/hio/lib/utl-str.c.m4 index 197e4cf..940a19c 100644 --- a/hio/lib/utl-str.c.m4 +++ b/hio/lib/utl-str.c.m4 @@ -117,6 +117,17 @@ dnl -- fn_split_cstr(hio_split_ucstr, hio_uch_t, hio_is_uch_space, hio_copy_ucstr_unlimited) fn_split_cstr(hio_split_bcstr, hio_bch_t, hio_is_bch_space, hio_copy_bcstr_unlimited) dnl -- +fn_tokenize_chars(hio_tokenize_uchars, hio_uch_t, hio_ucs_t, hio_is_uch_space, hio_to_uch_lower) +fn_tokenize_chars(hio_tokenize_bchars, hio_bch_t, hio_bcs_t, hio_is_bch_space, hio_to_bch_lower) +dnl -- +fn_byte_to_cstr(hio_byte_to_ucstr, hio_uch_t, HIO_BYTE_TO_UCSTR) +fn_byte_to_cstr(hio_byte_to_bcstr, hio_bch_t, HIO_BYTE_TO_BCSTR) +dnl -- +fn_int_to_cstr(hio_intmax_to_ucstr, hio_uch_t, hio_intmax_t, hio_count_ucstr) +fn_int_to_cstr(hio_intmax_to_bcstr, hio_bch_t, hio_intmax_t, hio_count_bcstr) +fn_int_to_cstr(hio_uintmax_to_ucstr, hio_uch_t, hio_uintmax_t, hio_count_ucstr) +fn_int_to_cstr(hio_uintmax_to_bcstr, hio_bch_t, hio_uintmax_t, hio_count_bcstr) +dnl -- fn_chars_to_int(hio_uchars_to_intmax, hio_uch_t, hio_intmax_t, hio_is_uch_space, HIO_UCHARS_TO_INTMAX) fn_chars_to_int(hio_bchars_to_intmax, hio_bch_t, hio_intmax_t, hio_is_bch_space, HIO_BCHARS_TO_INTMAX) dnl -- diff --git a/hio/lib/utl-str.m4 b/hio/lib/utl-str.m4 index f34942d..532e6e7 100644 --- a/hio/lib/utl-str.m4 +++ b/hio/lib/utl-str.m4 @@ -331,8 +331,8 @@ fini: popdef([[_fn_name_]])popdef([[_char_type_]])dnl ]])dnl dnl --------------------------------------------------------------------------- -define([[fn_copy_fmt_cses_to_cstr]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)pushdef([[_cs_t_]], $3)dnl -hio_oow_t _fn_name_ (_char_type_* buf, hio_oow_t bsz, const _char_type_* fmt, const _cs_t_ str[]) +define([[fn_copy_fmt_cses_to_cstr]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)pushdef([[_cs_type_]], $3)dnl +hio_oow_t _fn_name_ (_char_type_* buf, hio_oow_t bsz, const _char_type_* fmt, const _cs_type_ str[]) { _char_type_* b = buf; _char_type_* end = buf + bsz - 1; @@ -392,7 +392,7 @@ fini: *b = '\0'; return b - buf; } -popdef([[_fn_name_]])popdef([[_char_type_]])popdef([[_cs_t_]])dnl +popdef([[_fn_name_]])popdef([[_char_type_]])popdef([[_cs_type_]])dnl ]])dnl dnl --------------------------------------------------------------------------- define([[fn_count_cstr]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)dnl @@ -934,6 +934,310 @@ exit_point: } popdef([[_fn_name_]])popdef([[_char_type_]])popdef([[_is_space_]])popdef([[_copy_str_unlimited_]])dnl ]])dnl +define([[fn_tokenize_chars]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)pushdef([[_cs_type_]], $3)pushdef([[_is_space_]], $4)pushdef([[_to_lower_]], $5)dnl +_char_type_* _fn_name_ (const _char_type_* s, hio_oow_t len, const _char_type_* delim, hio_oow_t delim_len, _cs_type_* tok, int ignorecase) +{ + const _char_type_* p = s, *d; + const _char_type_* end = s + len; + const _char_type_* sp = HIO_NULL, * ep = HIO_NULL; + const _char_type_* delim_end = delim + delim_len; + _char_type_ c; + int delim_mode; + +#define __DELIM_NULL 0 +#define __DELIM_EMPTY 1 +#define __DELIM_SPACES 2 +#define __DELIM_NOSPACES 3 +#define __DELIM_COMPOSITE 4 + if (delim == HIO_NULL) delim_mode = __DELIM_NULL; + else + { + delim_mode = __DELIM_EMPTY; + + for (d = delim; d < delim_end; d++) + { + if (_is_space_()(*d)) + { + if (delim_mode == __DELIM_EMPTY) + delim_mode = __DELIM_SPACES; + else if (delim_mode == __DELIM_NOSPACES) + { + delim_mode = __DELIM_COMPOSITE; + break; + } + } + else + { + if (delim_mode == __DELIM_EMPTY) + delim_mode = __DELIM_NOSPACES; + else if (delim_mode == __DELIM_SPACES) + { + delim_mode = __DELIM_COMPOSITE; + break; + } + } + } + + /* TODO: verify the following statement... */ + if (delim_mode == __DELIM_SPACES && delim_len == 1 && delim[0] != ' ') delim_mode = __DELIM_NOSPACES; + } + + if (delim_mode == __DELIM_NULL) + { + /* when HIO_NULL is given as "delim", it trims off the + * leading and trailing spaces characters off the source + * string "s" eventually. */ + + while (p < end && _is_space_()(*p)) p++; + while (p < end) + { + c = *p; + + if (!_is_space_()(c)) + { + if (sp == HIO_NULL) sp = p; + ep = p; + } + p++; + } + } + else if (delim_mode == __DELIM_EMPTY) + { + /* each character in the source string "s" becomes a token. */ + if (p < end) + { + c = *p; + sp = p; + ep = p++; + } + } + else if (delim_mode == __DELIM_SPACES) + { + /* each token is delimited by space characters. all leading + * and trailing spaces are removed. */ + + while (p < end && _is_space_()(*p)) p++; + while (p < end) + { + c = *p; + if (_is_space_()(c)) break; + if (sp == HIO_NULL) sp = p; + ep = p++; + } + while (p < end && _is_space_()(*p)) p++; + } + else if (delim_mode == __DELIM_NOSPACES) + { + /* each token is delimited by one of charaters + * in the delimeter set "delim". */ + if (ignorecase) + { + while (p < end) + { + c = _to_lower_()(*p); + for (d = delim; d < delim_end; d++) + { + if (c == _to_lower_()(*d)) goto exit_loop; + } + + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + else + { + while (p < end) + { + c = *p; + for (d = delim; d < delim_end; d++) + { + if (c == *d) goto exit_loop; + } + + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + } + else /* if (delim_mode == __DELIM_COMPOSITE) */ + { + /* each token is delimited by one of non-space charaters + * in the delimeter set "delim". however, all space characters + * surrounding the token are removed */ + while (p < end && _is_space_()(*p)) p++; + if (ignorecase) + { + while (p < end) + { + c = _to_lower_()(*p); + if (_is_space_()(c)) + { + p++; + continue; + } + for (d = delim; d < delim_end; d++) + { + if (c == _to_lower_()(*d)) goto exit_loop; + } + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + else + { + while (p < end) + { + c = *p; + if (_is_space_()(c)) + { + p++; + continue; + } + for (d = delim; d < delim_end; d++) + { + if (c == *d) goto exit_loop; + } + if (sp == HIO_NULL) sp = p; + ep = p++; + } + } + } + +exit_loop: + if (sp == HIO_NULL) + { + tok->ptr = HIO_NULL; + tok->len = (hio_oow_t)0; + } + else + { + tok->ptr = (_char_type_*)sp; + tok->len = ep - sp + 1; + } + + /* if HIO_NULL is returned, this function should not be called again */ + if (p >= end) return HIO_NULL; + if (delim_mode == __DELIM_EMPTY || delim_mode == __DELIM_SPACES) return (_char_type_*)p; + return (_char_type_*)++p; +} +popdef([[_fn_name_]])popdef([[_char_type_]])popdef([[_cs_type_]])popdef([[_is_space_]])popdef([[_to_lower_]])dnl +]])dnl +dnl --------------------------------------------------------------------------- +define([[fn_byte_to_cstr]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)pushdef([[_prefix_]], $3)dnl +hio_oow_t _fn_name_ (hio_uint8_t byte, _char_type_* buf, hio_oow_t size, int flagged_radix, _char_type_ fill) +{ + _char_type_ tmp[(HIO_SIZEOF(hio_uint8_t) * HIO_BITS_PER_BYTE)]; + _char_type_* p = tmp, * bp = buf, * be = buf + size - 1; + int radix; + _char_type_ radix_char; + + radix = (flagged_radix & _prefix_()_RADIXMASK); + radix_char = (flagged_radix & _prefix_()_LOWERCASE)? 'a': 'A'; + if (radix < 2 || radix > 36 || size <= 0) return 0; + + do + { + hio_uint8_t digit = byte % radix; + if (digit < 10) *p++ = digit + '0'; + else *p++ = digit + radix_char - 10; + byte /= radix; + } + while (byte > 0); + + if (fill != '\0') + { + while (size - 1 > p - tmp) + { + *bp++ = fill; + size--; + } + } + + while (p > tmp && bp < be) *bp++ = *--p; + *bp = '\0'; + return bp - buf; +} +popdef([[_fn_name_]])popdef([[_char_type_]])popdef([[_cs_type_]])popdef([[_prefix_]])dnl +dnl --------------------------------------------------------------------------- +]])dnl +define([[fn_int_to_cstr]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)pushdef([[_int_type_]], $3)pushdef([[_count_cstr_]], $4)dnl +hio_oow_t _fn_name_ (_int_type_ value, int radix, const _char_type_* prefix, _char_type_* buf, hio_oow_t size) +{ + _int_type_ t, rem; + hio_oow_t len, ret, i; + hio_oow_t prefix_len; + + prefix_len = (prefix != HIO_NULL)? _count_cstr_()(prefix): 0; + + t = value; + if (t == 0) + { + /* zero */ + if (buf == HIO_NULL) + { + /* if buf is not given, + * return the number of bytes required */ + return prefix_len + 1; + } + + if (size < prefix_len+1) + { + /* buffer too small */ + return (hio_oow_t)-1; + } + + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + buf[prefix_len] = '0'; + if (size > prefix_len+1) buf[prefix_len+1] = '\0'; + return prefix_len+1; + } + + /* non-zero values */ + len = prefix_len; + if (t < 0) { t = -t; len++; } + while (t > 0) { len++; t /= radix; } + + if (buf == HIO_NULL) + { + /* if buf is not given, return the number of bytes required */ + return len; + } + + if (size < len) return (hio_oow_t)-1; /* buffer too small */ + if (size > len) buf[len] = '\0'; + ret = len; + + t = value; + if (t < 0) t = -t; + + while (t > 0) + { + rem = t % radix; + if (rem >= 10) + buf[--len] = (_char_type_)rem + 'a' - 10; + else + buf[--len] = (_char_type_)rem + '0'; + t /= radix; + } + + if (value < 0) + { + for (i = 1; i <= prefix_len; i++) + { + buf[i] = prefix[i-1]; + len--; + } + buf[--len] = '-'; + } + else + { + for (i = 0; i < prefix_len; i++) buf[i] = prefix[i]; + } + + return ret; +} +popdef([[_fn_name_]])popdef([[_char_type_]])popdef([[_int_type_]])popdef([[_count_cstr_]])dnl +]])dnl dnl --------------------------------------------------------------------------- define([[fn_chars_to_int]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)pushdef([[_int_type_]], $3)pushdef([[_is_space_]], $4)pushdef([[_prefix_]], $5)dnl _int_type_ _fn_name_ (const _char_type_* str, hio_oow_t len, int option, const _char_type_** endptr, int* is_sober) @@ -989,13 +1293,11 @@ _int_type_ _fn_name_ (const _char_type_* str, hio_oow_t len, int option, const _ } else if (rem >= 2 && base == 16) { - if (*p == '0' && - (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && - (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -1102,13 +1404,11 @@ _int_type_ _fn_name_ (const _char_type_* str, hio_oow_t len, int option, const _ } else if (rem >= 2 && base == 16) { - if (*p == '0' && - (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && - (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ diff --git a/hio/lib/utl.c b/hio/lib/utl.c index 3bc24cc..5d5c7b6 100644 --- a/hio/lib/utl.c +++ b/hio/lib/utl.c @@ -180,76 +180,6 @@ const hio_uch_t* hio_find_ucstr_word_in_ucstr (const hio_uch_t* str, const hio_u /* ========================================================================= */ -hio_oow_t hio_byte_to_ucstr (hio_oob_t byte, hio_uch_t* buf, hio_oow_t size, int flagged_radix, hio_uch_t fill) -{ - hio_uch_t tmp[(HIO_SIZEOF(hio_oob_t) * HIO_BITS_PER_BYTE)]; - hio_uch_t* p = tmp, * bp = buf, * be = buf + size - 1; - int radix; - hio_uch_t radix_char; - - radix = (flagged_radix & HIO_BYTE_TO_UCSTR_RADIXMASK); - radix_char = (flagged_radix & HIO_BYTE_TO_UCSTR_LOWERCASE)? 'a': 'A'; - if (radix < 2 || radix > 36 || size <= 0) return 0; - - do - { - hio_uint8_t digit = byte % radix; - if (digit < 10) *p++ = digit + '0'; - else *p++ = digit + radix_char - 10; - byte /= radix; - } - while (byte > 0); - - if (fill != '\0') - { - while (size - 1 > p - tmp) - { - *bp++ = fill; - size--; - } - } - - while (p > tmp && bp < be) *bp++ = *--p; - *bp = '\0'; - return bp - buf; -} - -hio_oow_t hio_byte_to_bcstr (hio_uint8_t byte, hio_bch_t* buf, hio_oow_t size, int flagged_radix, hio_bch_t fill) -{ - hio_bch_t tmp[(HIO_SIZEOF(hio_uint8_t) * HIO_BITS_PER_BYTE)]; - hio_bch_t* p = tmp, * bp = buf, * be = buf + size - 1; - int radix; - hio_bch_t radix_char; - - radix = (flagged_radix & HIO_BYTE_TO_BCSTR_RADIXMASK); - radix_char = (flagged_radix & HIO_BYTE_TO_BCSTR_LOWERCASE)? 'a': 'A'; - if (radix < 2 || radix > 36 || size <= 0) return 0; - - do - { - hio_uint8_t digit = byte % radix; - if (digit < 10) *p++ = digit + '0'; - else *p++ = digit + radix_char - 10; - byte /= radix; - } - while (byte > 0); - - if (fill != '\0') - { - while (size - 1 > p - tmp) - { - *bp++ = fill; - size--; - } - } - - while (p > tmp && bp < be) *bp++ = *--p; - *bp = '\0'; - return bp - buf; -} - -/* ========================================================================= */ - HIO_INLINE int hio_conv_bchars_to_uchars_with_cmgr ( const hio_bch_t* bcs, hio_oow_t* bcslen, hio_uch_t* ucs, hio_oow_t* ucslen, hio_cmgr_t* cmgr, int all)