diff --git a/bin/hclx.c b/bin/hclx.c index 2f1b01e..90b262e 100644 --- a/bin/hclx.c +++ b/bin/hclx.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include diff --git a/lib/Makefile.am b/lib/Makefile.am index ee6272f..653e660 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -54,6 +54,7 @@ pkginclude_HEADERS = \ hcl-opt.h \ hcl-pac1.h \ hcl-rbt.h \ + hcl-str.h \ hcl-upac.h \ hcl-utl.h \ hcl-xma.h @@ -85,6 +86,7 @@ libhcl_la_SOURCES = \ rbt.c \ read.c \ std.c \ + str.c \ sym.c \ uch-case.h \ uch-prop.h \ diff --git a/lib/Makefile.in b/lib/Makefile.in index e7c7290..0801a11 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -156,9 +156,9 @@ am_libhcl_la_OBJECTS = libhcl_la-bigint.lo libhcl_la-chr.lo \ libhcl_la-hcl.lo libhcl_la-heap.lo libhcl_la-mb8.lo \ libhcl_la-number.lo libhcl_la-obj.lo libhcl_la-opt.lo \ libhcl_la-prim.lo libhcl_la-print.lo libhcl_la-rbt.lo \ - libhcl_la-read.lo libhcl_la-std.lo libhcl_la-sym.lo \ - libhcl_la-utf16.lo libhcl_la-utf8.lo libhcl_la-utl.lo \ - libhcl_la-xchg.lo libhcl_la-xma.lo + libhcl_la-read.lo libhcl_la-std.lo libhcl_la-str.lo \ + libhcl_la-sym.lo libhcl_la-utf16.lo libhcl_la-utf8.lo \ + libhcl_la-utl.lo libhcl_la-xchg.lo libhcl_la-xma.lo libhcl_la_OBJECTS = $(am_libhcl_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -207,11 +207,11 @@ am__depfiles_remade = ./$(DEPDIR)/libhcl_la-bigint.Plo \ ./$(DEPDIR)/libhcl_la-obj.Plo ./$(DEPDIR)/libhcl_la-opt.Plo \ ./$(DEPDIR)/libhcl_la-prim.Plo ./$(DEPDIR)/libhcl_la-print.Plo \ ./$(DEPDIR)/libhcl_la-rbt.Plo ./$(DEPDIR)/libhcl_la-read.Plo \ - ./$(DEPDIR)/libhcl_la-std.Plo ./$(DEPDIR)/libhcl_la-sym.Plo \ - ./$(DEPDIR)/libhcl_la-utf16.Plo ./$(DEPDIR)/libhcl_la-utf8.Plo \ - ./$(DEPDIR)/libhcl_la-utl.Plo ./$(DEPDIR)/libhcl_la-xchg.Plo \ - ./$(DEPDIR)/libhcl_la-xma.Plo ./$(DEPDIR)/libhclx_la-json.Plo \ - ./$(DEPDIR)/libhclx_la-tmr.Plo \ + ./$(DEPDIR)/libhcl_la-std.Plo ./$(DEPDIR)/libhcl_la-str.Plo \ + ./$(DEPDIR)/libhcl_la-sym.Plo ./$(DEPDIR)/libhcl_la-utf16.Plo \ + ./$(DEPDIR)/libhcl_la-utf8.Plo ./$(DEPDIR)/libhcl_la-utl.Plo \ + ./$(DEPDIR)/libhcl_la-xchg.Plo ./$(DEPDIR)/libhcl_la-xma.Plo \ + ./$(DEPDIR)/libhclx_la-json.Plo ./$(DEPDIR)/libhclx_la-tmr.Plo \ ./$(DEPDIR)/libhclx_la-x-client.Plo \ ./$(DEPDIR)/libhclx_la-x-proto.Plo \ ./$(DEPDIR)/libhclx_la-x-server.Plo \ @@ -244,8 +244,8 @@ am__can_run_installinfo = \ *) (install-info --version) >/dev/null 2>&1;; \ esac am__pkginclude_HEADERS_DIST = hcl.h hcl-chr.h hcl-cmn.h hcl-fmt.h \ - hcl-opt.h hcl-pac1.h hcl-rbt.h hcl-upac.h hcl-utl.h hcl-xma.h \ - hcl-x.h hcl-tmr.h hcl-json.h + hcl-opt.h hcl-pac1.h hcl-rbt.h hcl-str.h hcl-upac.h hcl-utl.h \ + hcl-xma.h hcl-x.h hcl-tmr.h hcl-json.h HEADERS = $(pkginclude_HEADERS) am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \ hcl-cfg.h.in @@ -435,7 +435,7 @@ LIBADD_LIB_COMMON = $(LIBM) $(am__append_1) $(am__append_2) \ @WIN32_TRUE@ -DHCL_DEFAULT_PFMODPOSTFIX=\"-1.dll\" \ @WIN32_TRUE@ $(am__append_4) $(am__append_5) pkginclude_HEADERS = hcl.h hcl-chr.h hcl-cmn.h hcl-fmt.h hcl-opt.h \ - hcl-pac1.h hcl-rbt.h hcl-upac.h hcl-utl.h hcl-xma.h \ + hcl-pac1.h hcl-rbt.h hcl-str.h hcl-upac.h hcl-utl.h hcl-xma.h \ $(am__append_8) pkglib_LTLIBRARIES = libhcl.la $(am__append_7) libhcl_la_SOURCES = \ @@ -464,6 +464,7 @@ libhcl_la_SOURCES = \ rbt.c \ read.c \ std.c \ + str.c \ sym.c \ uch-case.h \ uch-prop.h \ @@ -612,6 +613,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhcl_la-rbt.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhcl_la-read.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhcl_la-std.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhcl_la-str.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhcl_la-sym.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhcl_la-utf16.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhcl_la-utf8.Plo@am__quote@ # am--include-marker @@ -810,6 +812,13 @@ libhcl_la-std.lo: std.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhcl_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libhcl_la-std.lo `test -f 'std.c' || echo '$(srcdir)/'`std.c +libhcl_la-str.lo: str.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhcl_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libhcl_la-str.lo -MD -MP -MF $(DEPDIR)/libhcl_la-str.Tpo -c -o libhcl_la-str.lo `test -f 'str.c' || echo '$(srcdir)/'`str.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhcl_la-str.Tpo $(DEPDIR)/libhcl_la-str.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='str.c' object='libhcl_la-str.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhcl_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libhcl_la-str.lo `test -f 'str.c' || echo '$(srcdir)/'`str.c + libhcl_la-sym.lo: sym.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhcl_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libhcl_la-sym.lo -MD -MP -MF $(DEPDIR)/libhcl_la-sym.Tpo -c -o libhcl_la-sym.lo `test -f 'sym.c' || echo '$(srcdir)/'`sym.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhcl_la-sym.Tpo $(DEPDIR)/libhcl_la-sym.Plo @@ -1077,6 +1086,7 @@ distclean: distclean-am -rm -f ./$(DEPDIR)/libhcl_la-rbt.Plo -rm -f ./$(DEPDIR)/libhcl_la-read.Plo -rm -f ./$(DEPDIR)/libhcl_la-std.Plo + -rm -f ./$(DEPDIR)/libhcl_la-str.Plo -rm -f ./$(DEPDIR)/libhcl_la-sym.Plo -rm -f ./$(DEPDIR)/libhcl_la-utf16.Plo -rm -f ./$(DEPDIR)/libhcl_la-utf8.Plo @@ -1158,6 +1168,7 @@ maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/libhcl_la-rbt.Plo -rm -f ./$(DEPDIR)/libhcl_la-read.Plo -rm -f ./$(DEPDIR)/libhcl_la-std.Plo + -rm -f ./$(DEPDIR)/libhcl_la-str.Plo -rm -f ./$(DEPDIR)/libhcl_la-sym.Plo -rm -f ./$(DEPDIR)/libhcl_la-utf16.Plo -rm -f ./$(DEPDIR)/libhcl_la-utf8.Plo diff --git a/lib/comp.c b/lib/comp.c index 4b8d654..b945220 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -308,19 +308,19 @@ static int find_variable_backward_with_word (hcl_t* hcl, const hcl_oocs_t* name, /* this function block has a class defined. * that is, it is in a class defintion. * variable lookup must be limited to the class scope */ - hcl_clsblk_info_t* clsbi; + hcl_clsblk_info_t* cbi; #if 0 for (j = fbi->clsblk_top + 1; j > fbi->clsblk_base; ) { - clsbi = &hcl->c->clsblk.info[--j]; + cbi = &hcl->c->clsblk.info[--j]; #endif - clsbi = &hcl->c->clsblk.info[fbi->clsblk_top]; + cbi = &hcl->c->clsblk.info[fbi->clsblk_top]; - if (clsbi->ivars_str) + if (cbi->ivars_str) { - haystack.ptr = clsbi->ivars_str; - haystack.len = hcl_count_oocstr(clsbi->ivars_str); + haystack.ptr = cbi->ivars_str; + haystack.len = hcl_count_oocstr(cbi->ivars_str); if (__find_word_in_string(&haystack, name, 1, &index) >= 0) { hcl_oow_t fi; @@ -358,10 +358,10 @@ HCL_INFO6 (hcl, "FOUND INST VAR [%.*js]...[%.*js]................ ===> ctx_offse } } - if (clsbi->cvars_str) + if (cbi->cvars_str) { - haystack.ptr = clsbi->cvars_str; - haystack.len = hcl_count_oocstr(clsbi->cvars_str); + haystack.ptr = cbi->cvars_str; + haystack.len = hcl_count_oocstr(cbi->cvars_str); if (__find_word_in_string(&haystack, name, 1, &index) >= 0) { /* TODO: VAR_CLASS_CM vs VAR_CLASS_IM, need to know if it's an instance method or a class method */ @@ -1071,6 +1071,12 @@ static int push_clsblk ( ci->nivars = nivars; ci->ncvars = ncvars; +#if 0 +XXXXXXXXXXXXXXXXXX +/* TODO: ... */ + ci->ivars. + ci->cvars = hcl_ +#endif if (nivars > 0) { HCL_ASSERT (hcl, ivars_str != HCL_NULL); diff --git a/lib/hcl-prv.h b/lib/hcl-prv.h index f800bf2..4fc3b37 100644 --- a/lib/hcl-prv.h +++ b/lib/hcl-prv.h @@ -28,6 +28,7 @@ #include #include #include +#include #include /* you can define this to either 1 or 2 */ diff --git a/lib/hcl-str.h b/lib/hcl-str.h new file mode 100644 index 0000000..c23dd2c --- /dev/null +++ b/lib/hcl-str.h @@ -0,0 +1,373 @@ +/* + Copyright (c) 2016-2018 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. + */ + +#ifndef _HCL_STR_H_ +#define _HCL_STR_H_ + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +/** + * The hcl_equal_uchars() function determines equality of two strings + * of the same length \a len. + */ +HCL_EXPORT int hcl_equal_uchars ( + const hcl_uch_t* str1, + const hcl_uch_t* str2, + hcl_oow_t len +); + +HCL_EXPORT int hcl_equal_bchars ( + const hcl_bch_t* str1, + const hcl_bch_t* str2, + hcl_oow_t len +); + +/* ------------------------------ */ + +HCL_EXPORT int hcl_comp_uchars ( + const hcl_uch_t* str1, + hcl_oow_t len1, + const hcl_uch_t* str2, + hcl_oow_t len2 +); + +HCL_EXPORT int hcl_comp_bchars ( + const hcl_bch_t* str1, + hcl_oow_t len1, + const hcl_bch_t* str2, + hcl_oow_t len2 +); + +HCL_EXPORT int hcl_comp_ucstr ( + const hcl_uch_t* str1, + const hcl_uch_t* str2 +); + +HCL_EXPORT int hcl_comp_bcstr ( + const hcl_bch_t* str1, + const hcl_bch_t* str2 +); + +HCL_EXPORT int hcl_comp_ucstr_bcstr ( + const hcl_uch_t* str1, + const hcl_bch_t* str2 +); + +HCL_EXPORT int hcl_comp_uchars_ucstr ( + const hcl_uch_t* str1, + hcl_oow_t len, + const hcl_uch_t* str2 +); + +HCL_EXPORT int hcl_comp_uchars_bcstr ( + const hcl_uch_t* str1, + hcl_oow_t len, + const hcl_bch_t* str2 +); + +HCL_EXPORT int hcl_comp_bchars_bcstr ( + const hcl_bch_t* str1, + hcl_oow_t len, + const hcl_bch_t* str2 +); + +HCL_EXPORT int hcl_comp_bchars_ucstr ( + const hcl_bch_t* str1, + hcl_oow_t len, + const hcl_uch_t* str2 +); + +/* ------------------------------ */ + +HCL_EXPORT void hcl_copy_uchars ( + hcl_uch_t* dst, + const hcl_uch_t* src, + hcl_oow_t len +); + +HCL_EXPORT void hcl_copy_bchars ( + hcl_bch_t* dst, + const hcl_bch_t* src, + hcl_oow_t len +); + +HCL_EXPORT void hcl_copy_bchars_to_uchars ( + hcl_uch_t* dst, + const hcl_bch_t* src, + hcl_oow_t len +); + +HCL_EXPORT void hcl_copy_uchars_to_bchars ( + hcl_bch_t* dst, + const hcl_uch_t* src, + hcl_oow_t len +); + +HCL_EXPORT hcl_oow_t hcl_copy_bcstr_to_ucstr ( + hcl_uch_t* dst, + hcl_oow_t len, + const hcl_bch_t* src +); + +HCL_EXPORT hcl_oow_t hcl_copy_ucstr_to_bcstr ( + hcl_bch_t* dst, + hcl_oow_t len, + const hcl_uch_t* src +); + +HCL_EXPORT hcl_oow_t hcl_copy_uchars_to_ucstr_unlimited ( + hcl_uch_t* dst, + const hcl_uch_t* src, + hcl_oow_t len +); + +HCL_EXPORT hcl_oow_t hcl_copy_bchars_to_bcstr_unlimited ( + hcl_bch_t* dst, + const hcl_bch_t* src, + hcl_oow_t len +); + +HCL_EXPORT hcl_oow_t hcl_copy_ucstr ( + hcl_uch_t* dst, + hcl_oow_t len, + const hcl_uch_t* src +); + +HCL_EXPORT hcl_oow_t hcl_copy_bcstr ( + hcl_bch_t* dst, + hcl_oow_t len, + const hcl_bch_t* src +); + +HCL_EXPORT hcl_oow_t hcl_copy_uchars_to_ucstr ( + hcl_uch_t* dst, + hcl_oow_t dlen, + const hcl_uch_t* src, + hcl_oow_t slen +); + +HCL_EXPORT hcl_oow_t hcl_copy_bchars_to_bcstr ( + hcl_bch_t* dst, + hcl_oow_t dlen, + const hcl_bch_t* src, + hcl_oow_t slen +); + +HCL_EXPORT hcl_oow_t hcl_copy_ucstr_unlimited ( + hcl_uch_t* dst, + const hcl_uch_t* src +); + +HCL_EXPORT hcl_oow_t hcl_copy_bcstr_unlimited ( + hcl_bch_t* dst, + const hcl_bch_t* src +); + +/* ------------------------------ */ + +HCL_EXPORT void hcl_fill_uchars ( + hcl_uch_t* dst, + hcl_uch_t ch, + hcl_oow_t len +); + +HCL_EXPORT void hcl_fill_bchars ( + hcl_bch_t* dst, + hcl_bch_t ch, + hcl_oow_t len +); + +HCL_EXPORT hcl_uch_t* hcl_find_uchar ( + const hcl_uch_t* ptr, + hcl_oow_t len, + hcl_uch_t c +); + +HCL_EXPORT hcl_bch_t* hcl_find_bchar ( + const hcl_bch_t* ptr, + hcl_oow_t len, + hcl_bch_t c +); + +HCL_EXPORT hcl_uch_t* hcl_rfind_uchar ( + const hcl_uch_t* ptr, + hcl_oow_t len, + hcl_uch_t c +); + +HCL_EXPORT hcl_bch_t* hcl_rfind_bchar ( + const hcl_bch_t* ptr, + hcl_oow_t len, + hcl_bch_t c +); + +HCL_EXPORT hcl_uch_t* hcl_find_uchar_in_ucstr ( + const hcl_uch_t* ptr, + hcl_uch_t c +); + +HCL_EXPORT hcl_bch_t* hcl_find_bchar_in_bcstr ( + const hcl_bch_t* ptr, + hcl_bch_t c +); + +HCL_EXPORT hcl_oow_t hcl_rotate_uchars ( + hcl_uch_t* str, + hcl_oow_t len, + int dir, + hcl_oow_t n +); + +HCL_EXPORT hcl_oow_t hcl_rotate_bchars ( + hcl_bch_t* str, + hcl_oow_t len, + int dir, + hcl_oow_t n +); + +HCL_EXPORT hcl_oow_t hcl_count_ucstr ( + const hcl_uch_t* str +); + +HCL_EXPORT hcl_oow_t hcl_count_bcstr ( + const hcl_bch_t* str +); + +#if defined(HCL_OOCH_IS_UCH) +# define hcl_equal_oochars(str1,str2,len) hcl_equal_uchars(str1,str2,len) +# define hcl_comp_oochars(str1,len1,str2,len2) hcl_comp_uchars(str1,len1,str2,len2) +# define hcl_comp_oocstr_bcstr(str1,str2) hcl_comp_ucstr_bcstr(str1,str2) +# define hcl_comp_oochars_bcstr(str1,len1,str2) hcl_comp_uchars_bcstr(str1,len1,str2) +# define hcl_comp_oochars_ucstr(str1,len1,str2) hcl_comp_uchars_ucstr(str1,len1,str2) +# define hcl_comp_oochars_oocstr(str1,len1,str2) hcl_comp_uchars_ucstr(str1,len1,str2) +# define hcl_comp_oocstr(str1,str2) hcl_comp_ucstr(str1,str2) + +# define hcl_copy_oochars(dst,src,len) hcl_copy_uchars(dst,src,len) +# define hcl_copy_bchars_to_oochars(dst,src,len) hcl_copy_bchars_to_uchars(dst,src,len) +# define hcl_copy_oochars_to_bchars(dst,src,len) hcl_copy_uchars_to_bchars(dst,src,len) +# define hcl_copy_uchars_to_oochars(dst,src,len) hcl_copy_uchars(dst,src,len) +# define hcl_copy_oochars_to_uchars(dst,src,len) hcl_copy_uchars(dst,src,len) + +# define hcl_copy_oochars_to_oocstr(dst,dlen,src,slen) hcl_copy_uchars_to_ucstr(dst,dlen,src,slen) +# define hcl_copy_oochars_to_oocstr_unlimited(dst,src,len) hcl_copy_uchars_to_ucstr_unlimited(dst,src,len) +# define hcl_copy_oocstr(dst,len,src) hcl_copy_ucstr(dst,len,src) +# define hcl_copy_oocstr_unlimited(dst,src) hcl_copy_ucstr_unlimited(dst,src) + +# define hcl_fill_oochars hcl_fill_uchars +# define hcl_find_oochar hcl_find_uchar +# define hcl_rfind_oochar hcl_rfind_uchar +# define hcl_find_oochar_in_oocstr hcl_find_uchar_in_ucstr +# define hcl_rotate_oochars hcl_rotate_uchars +# define hcl_count_oocstr hcl_count_ucstr +#else +# define hcl_equal_oochars(str1,str2,len) hcl_equal_bchars(str1,str2,len) +# define hcl_comp_oochars(str1,len1,str2,len2) hcl_comp_bchars(str1,len1,str2,len2) +# define hcl_comp_oocstr_bcstr(str1,str2) hcl_comp_bcstr(str1,str2) +# define hcl_comp_oochars_bcstr(str1,len1,str2) hcl_comp_bchars_bcstr(str1,len1,str2) +# define hcl_comp_oochars_ucstr(str1,len1,str2) hcl_comp_bchars_ucstr(str1,len1,str2) +# define hcl_comp_oochars_oocstr(str1,len1,str2) hcl_comp_bchars_bcstr(str1,len1,str2) +# define hcl_comp_oocstr(str1,str2) hcl_comp_bcstr(str1,str2) + +# define hcl_copy_oochars(dst,src,len) hcl_copy_bchars(dst,src,len) +# define hcl_copy_bchars_to_oochars(dst,src,len) hcl_copy_bchars(dst,src,len) +# define hcl_copy_oochars_to_bchars(dst,src,len) hcl_copy_bchars(dst,src,len) +# define hcl_copy_uchars_to_oochars(dst,src,len) hcl_copy_uchars_to_bchars(dst,src,len) +# define hcl_copy_oochars_to_uchars(dst,src,len) hcl_copy_bchars_to_uchars(dst,src,len) + +# define hcl_copy_oochars_to_oocstr(dst,dlen,src,slen) hcl_copy_bchars_to_bcstr(dst,dlen,src,slen) +# define hcl_copy_oochars_to_oocstr_unlimited(dst,src,len) hcl_copy_bchars_to_bcstr_unlimited(dst,src,len) +# define hcl_copy_oocstr(dst,len,src) hcl_copy_bcstr(dst,len,src) +# define hcl_copy_oocstr_unlimited(dst,src) hcl_copy_bcstr_unlimited(dst,src) + +# define hcl_fill_oochars hcl_fill_bchars +# define hcl_find_oochar hcl_find_bchar +# define hcl_rfind_oochar hcl_rfind_bchar +# define hcl_find_oochar_in_oocstr hcl_find_bchar_in_bcstr +# define hcl_rotate_oochars hcl_rotate_bchars +# define hcl_count_oocstr hcl_count_bcstr +#endif + +#define HCL_BYTE_TO_BCSTR_RADIXMASK (0xFF) +#define HCL_BYTE_TO_BCSTR_LOWERCASE (1 << 8) + +hcl_oow_t hcl_byte_to_bcstr ( + hcl_uint8_t byte, + hcl_bch_t* buf, + hcl_oow_t size, + int flagged_radix, + hcl_bch_t fill +); + + +HCL_EXPORT int hcl_conv_bcstr_to_ucstr_with_cmgr ( + const hcl_bch_t* bcs, + hcl_oow_t* bcslen, + hcl_uch_t* ucs, + hcl_oow_t* ucslen, + hcl_cmgr_t* cmgr, + int all +); + +HCL_EXPORT int hcl_conv_bchars_to_uchars_with_cmgr ( + const hcl_bch_t* bcs, + hcl_oow_t* bcslen, + hcl_uch_t* ucs, + hcl_oow_t* ucslen, + hcl_cmgr_t* cmgr, + int all +); + +HCL_EXPORT int hcl_conv_ucstr_to_bcstr_with_cmgr ( + const hcl_uch_t* ucs, + hcl_oow_t* ucslen, + hcl_bch_t* bcs, + hcl_oow_t* bcslen, + hcl_cmgr_t* cmgr +); + +HCL_EXPORT int hcl_conv_uchars_to_bchars_with_cmgr ( + const hcl_uch_t* ucs, + hcl_oow_t* ucslen, + hcl_bch_t* bcs, + hcl_oow_t* bcslen, + hcl_cmgr_t* cmgr +); + +#if defined(HCL_OOCH_IS_UCH) +# define hcl_conv_oocstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hcl_conv_ucstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) +# define hcl_conv_oochars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hcl_conv_uchars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) +#else +# define hcl_conv_oocstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hcl_conv_bcstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) +# define hcl_conv_oochars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hcl_conv_bchars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) +#endif + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/lib/hcl-utl.h b/lib/hcl-utl.h index c0eb937..938eb17 100644 --- a/lib/hcl-utl.h +++ b/lib/hcl-utl.h @@ -452,341 +452,6 @@ static HCL_INLINE hcl_oow_t hcl_hash_liwords(const hcl_liw_t* ptr, hcl_oow_t len # define hcl_hash_oochars(ptr,len) hcl_hash_bchars(ptr,len) #endif -/** - * The hcl_equal_uchars() function determines equality of two strings - * of the same length \a len. - */ -HCL_EXPORT int hcl_equal_uchars ( - const hcl_uch_t* str1, - const hcl_uch_t* str2, - hcl_oow_t len -); - -HCL_EXPORT int hcl_equal_bchars ( - const hcl_bch_t* str1, - const hcl_bch_t* str2, - hcl_oow_t len -); - -/* ------------------------------ */ - -HCL_EXPORT int hcl_comp_uchars ( - const hcl_uch_t* str1, - hcl_oow_t len1, - const hcl_uch_t* str2, - hcl_oow_t len2 -); - -HCL_EXPORT int hcl_comp_bchars ( - const hcl_bch_t* str1, - hcl_oow_t len1, - const hcl_bch_t* str2, - hcl_oow_t len2 -); - -HCL_EXPORT int hcl_comp_ucstr ( - const hcl_uch_t* str1, - const hcl_uch_t* str2 -); - -HCL_EXPORT int hcl_comp_bcstr ( - const hcl_bch_t* str1, - const hcl_bch_t* str2 -); - -HCL_EXPORT int hcl_comp_ucstr_bcstr ( - const hcl_uch_t* str1, - const hcl_bch_t* str2 -); - -HCL_EXPORT int hcl_comp_uchars_ucstr ( - const hcl_uch_t* str1, - hcl_oow_t len, - const hcl_uch_t* str2 -); - -HCL_EXPORT int hcl_comp_uchars_bcstr ( - const hcl_uch_t* str1, - hcl_oow_t len, - const hcl_bch_t* str2 -); - -HCL_EXPORT int hcl_comp_bchars_bcstr ( - const hcl_bch_t* str1, - hcl_oow_t len, - const hcl_bch_t* str2 -); - -HCL_EXPORT int hcl_comp_bchars_ucstr ( - const hcl_bch_t* str1, - hcl_oow_t len, - const hcl_uch_t* str2 -); - -/* ------------------------------ */ - -HCL_EXPORT void hcl_copy_uchars ( - hcl_uch_t* dst, - const hcl_uch_t* src, - hcl_oow_t len -); - -HCL_EXPORT void hcl_copy_bchars ( - hcl_bch_t* dst, - const hcl_bch_t* src, - hcl_oow_t len -); - -HCL_EXPORT void hcl_copy_bchars_to_uchars ( - hcl_uch_t* dst, - const hcl_bch_t* src, - hcl_oow_t len -); - -HCL_EXPORT void hcl_copy_uchars_to_bchars ( - hcl_bch_t* dst, - const hcl_uch_t* src, - hcl_oow_t len -); - -HCL_EXPORT hcl_oow_t hcl_copy_bcstr_to_ucstr ( - hcl_uch_t* dst, - hcl_oow_t len, - const hcl_bch_t* src -); - -HCL_EXPORT hcl_oow_t hcl_copy_ucstr_to_bcstr ( - hcl_bch_t* dst, - hcl_oow_t len, - const hcl_uch_t* src -); - -HCL_EXPORT hcl_oow_t hcl_copy_uchars_to_ucstr_unlimited ( - hcl_uch_t* dst, - const hcl_uch_t* src, - hcl_oow_t len -); - -HCL_EXPORT hcl_oow_t hcl_copy_bchars_to_bcstr_unlimited ( - hcl_bch_t* dst, - const hcl_bch_t* src, - hcl_oow_t len -); - -HCL_EXPORT hcl_oow_t hcl_copy_ucstr ( - hcl_uch_t* dst, - hcl_oow_t len, - const hcl_uch_t* src -); - -HCL_EXPORT hcl_oow_t hcl_copy_bcstr ( - hcl_bch_t* dst, - hcl_oow_t len, - const hcl_bch_t* src -); - -HCL_EXPORT hcl_oow_t hcl_copy_uchars_to_ucstr ( - hcl_uch_t* dst, - hcl_oow_t dlen, - const hcl_uch_t* src, - hcl_oow_t slen -); - -HCL_EXPORT hcl_oow_t hcl_copy_bchars_to_bcstr ( - hcl_bch_t* dst, - hcl_oow_t dlen, - const hcl_bch_t* src, - hcl_oow_t slen -); - -HCL_EXPORT hcl_oow_t hcl_copy_ucstr_unlimited ( - hcl_uch_t* dst, - const hcl_uch_t* src -); - -HCL_EXPORT hcl_oow_t hcl_copy_bcstr_unlimited ( - hcl_bch_t* dst, - const hcl_bch_t* src -); - -/* ------------------------------ */ - -HCL_EXPORT void hcl_fill_uchars ( - hcl_uch_t* dst, - hcl_uch_t ch, - hcl_oow_t len -); - -HCL_EXPORT void hcl_fill_bchars ( - hcl_bch_t* dst, - hcl_bch_t ch, - hcl_oow_t len -); - -HCL_EXPORT hcl_uch_t* hcl_find_uchar ( - const hcl_uch_t* ptr, - hcl_oow_t len, - hcl_uch_t c -); - -HCL_EXPORT hcl_bch_t* hcl_find_bchar ( - const hcl_bch_t* ptr, - hcl_oow_t len, - hcl_bch_t c -); - -HCL_EXPORT hcl_uch_t* hcl_rfind_uchar ( - const hcl_uch_t* ptr, - hcl_oow_t len, - hcl_uch_t c -); - -HCL_EXPORT hcl_bch_t* hcl_rfind_bchar ( - const hcl_bch_t* ptr, - hcl_oow_t len, - hcl_bch_t c -); - -HCL_EXPORT hcl_uch_t* hcl_find_uchar_in_ucstr ( - const hcl_uch_t* ptr, - hcl_uch_t c -); - -HCL_EXPORT hcl_bch_t* hcl_find_bchar_in_bcstr ( - const hcl_bch_t* ptr, - hcl_bch_t c -); - -HCL_EXPORT hcl_oow_t hcl_rotate_uchars ( - hcl_uch_t* str, - hcl_oow_t len, - int dir, - hcl_oow_t n -); - -HCL_EXPORT hcl_oow_t hcl_rotate_bchars ( - hcl_bch_t* str, - hcl_oow_t len, - int dir, - hcl_oow_t n -); - -HCL_EXPORT hcl_oow_t hcl_count_ucstr ( - const hcl_uch_t* str -); - -HCL_EXPORT hcl_oow_t hcl_count_bcstr ( - const hcl_bch_t* str -); - -#if defined(HCL_OOCH_IS_UCH) -# define hcl_equal_oochars(str1,str2,len) hcl_equal_uchars(str1,str2,len) -# define hcl_comp_oochars(str1,len1,str2,len2) hcl_comp_uchars(str1,len1,str2,len2) -# define hcl_comp_oocstr_bcstr(str1,str2) hcl_comp_ucstr_bcstr(str1,str2) -# define hcl_comp_oochars_bcstr(str1,len1,str2) hcl_comp_uchars_bcstr(str1,len1,str2) -# define hcl_comp_oochars_ucstr(str1,len1,str2) hcl_comp_uchars_ucstr(str1,len1,str2) -# define hcl_comp_oochars_oocstr(str1,len1,str2) hcl_comp_uchars_ucstr(str1,len1,str2) -# define hcl_comp_oocstr(str1,str2) hcl_comp_ucstr(str1,str2) - -# define hcl_copy_oochars(dst,src,len) hcl_copy_uchars(dst,src,len) -# define hcl_copy_bchars_to_oochars(dst,src,len) hcl_copy_bchars_to_uchars(dst,src,len) -# define hcl_copy_oochars_to_bchars(dst,src,len) hcl_copy_uchars_to_bchars(dst,src,len) -# define hcl_copy_uchars_to_oochars(dst,src,len) hcl_copy_uchars(dst,src,len) -# define hcl_copy_oochars_to_uchars(dst,src,len) hcl_copy_uchars(dst,src,len) - -# define hcl_copy_oochars_to_oocstr(dst,dlen,src,slen) hcl_copy_uchars_to_ucstr(dst,dlen,src,slen) -# define hcl_copy_oochars_to_oocstr_unlimited(dst,src,len) hcl_copy_uchars_to_ucstr_unlimited(dst,src,len) -# define hcl_copy_oocstr(dst,len,src) hcl_copy_ucstr(dst,len,src) -# define hcl_copy_oocstr_unlimited(dst,src) hcl_copy_ucstr_unlimited(dst,src) - -# define hcl_fill_oochars hcl_fill_uchars -# define hcl_find_oochar hcl_find_uchar -# define hcl_rfind_oochar hcl_rfind_uchar -# define hcl_find_oochar_in_oocstr hcl_find_uchar_in_ucstr -# define hcl_rotate_oochars hcl_rotate_uchars -# define hcl_count_oocstr hcl_count_ucstr -#else -# define hcl_equal_oochars(str1,str2,len) hcl_equal_bchars(str1,str2,len) -# define hcl_comp_oochars(str1,len1,str2,len2) hcl_comp_bchars(str1,len1,str2,len2) -# define hcl_comp_oocstr_bcstr(str1,str2) hcl_comp_bcstr(str1,str2) -# define hcl_comp_oochars_bcstr(str1,len1,str2) hcl_comp_bchars_bcstr(str1,len1,str2) -# define hcl_comp_oochars_ucstr(str1,len1,str2) hcl_comp_bchars_ucstr(str1,len1,str2) -# define hcl_comp_oochars_oocstr(str1,len1,str2) hcl_comp_bchars_bcstr(str1,len1,str2) -# define hcl_comp_oocstr(str1,str2) hcl_comp_bcstr(str1,str2) - -# define hcl_copy_oochars(dst,src,len) hcl_copy_bchars(dst,src,len) -# define hcl_copy_bchars_to_oochars(dst,src,len) hcl_copy_bchars(dst,src,len) -# define hcl_copy_oochars_to_bchars(dst,src,len) hcl_copy_bchars(dst,src,len) -# define hcl_copy_uchars_to_oochars(dst,src,len) hcl_copy_uchars_to_bchars(dst,src,len) -# define hcl_copy_oochars_to_uchars(dst,src,len) hcl_copy_bchars_to_uchars(dst,src,len) - -# define hcl_copy_oochars_to_oocstr(dst,dlen,src,slen) hcl_copy_bchars_to_bcstr(dst,dlen,src,slen) -# define hcl_copy_oochars_to_oocstr_unlimited(dst,src,len) hcl_copy_bchars_to_bcstr_unlimited(dst,src,len) -# define hcl_copy_oocstr(dst,len,src) hcl_copy_bcstr(dst,len,src) -# define hcl_copy_oocstr_unlimited(dst,src) hcl_copy_bcstr_unlimited(dst,src) - -# define hcl_fill_oochars hcl_fill_bchars -# define hcl_find_oochar hcl_find_bchar -# define hcl_rfind_oochar hcl_rfind_bchar -# define hcl_find_oochar_in_oocstr hcl_find_bchar_in_bcstr -# define hcl_rotate_oochars hcl_rotate_bchars -# define hcl_count_oocstr hcl_count_bcstr -#endif - -#define HCL_BYTE_TO_BCSTR_RADIXMASK (0xFF) -#define HCL_BYTE_TO_BCSTR_LOWERCASE (1 << 8) - -hcl_oow_t hcl_byte_to_bcstr ( - hcl_uint8_t byte, - hcl_bch_t* buf, - hcl_oow_t size, - int flagged_radix, - hcl_bch_t fill -); - - -HCL_EXPORT int hcl_conv_bcstr_to_ucstr_with_cmgr ( - const hcl_bch_t* bcs, - hcl_oow_t* bcslen, - hcl_uch_t* ucs, - hcl_oow_t* ucslen, - hcl_cmgr_t* cmgr, - int all -); - -HCL_EXPORT int hcl_conv_bchars_to_uchars_with_cmgr ( - const hcl_bch_t* bcs, - hcl_oow_t* bcslen, - hcl_uch_t* ucs, - hcl_oow_t* ucslen, - hcl_cmgr_t* cmgr, - int all -); - -HCL_EXPORT int hcl_conv_ucstr_to_bcstr_with_cmgr ( - const hcl_uch_t* ucs, - hcl_oow_t* ucslen, - hcl_bch_t* bcs, - hcl_oow_t* bcslen, - hcl_cmgr_t* cmgr -); - -HCL_EXPORT int hcl_conv_uchars_to_bchars_with_cmgr ( - const hcl_uch_t* ucs, - hcl_oow_t* ucslen, - hcl_bch_t* bcs, - hcl_oow_t* bcslen, - hcl_cmgr_t* cmgr -); - -#if defined(HCL_OOCH_IS_UCH) -# define hcl_conv_oocstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hcl_conv_ucstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) -# define hcl_conv_oochars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) hcl_conv_uchars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) -#else -# define hcl_conv_oocstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hcl_conv_bcstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) -# define hcl_conv_oochars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) hcl_conv_bchars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0) -#endif - /* ------------------------------------------------------------------------- */ HCL_EXPORT hcl_cmgr_t* hcl_get_utf8_cmgr ( @@ -815,7 +480,6 @@ HCL_EXPORT hcl_cmgr_t* hcl_get_cmgr_by_ucstr ( #define hcl_get_utf16_cmgr() hcl_get_cmgr_by_id(HCL_CMGR_UTF16) #define hcl_get_mb8_cmgr() hcl_get_cmgr_by_id(HCL_CMGR_MB8) - /** * The hcl_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 @@ -898,7 +562,6 @@ HCL_EXPORT int hcl_conv_utf8_to_ucstr ( hcl_oow_t* ucslen ); - HCL_EXPORT hcl_oow_t hcl_uc_to_utf8 ( hcl_uch_t uc, hcl_bch_t* utf8, diff --git a/lib/str.c b/lib/str.c new file mode 100644 index 0000000..2f935c4 --- /dev/null +++ b/lib/str.c @@ -0,0 +1,827 @@ +/* + Copyright (c) 2016-2018 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. + */ + +#include + +int hcl_equal_uchars (const hcl_uch_t* str1, const hcl_uch_t* str2, hcl_oow_t len) +{ + hcl_oow_t i; + + /* NOTE: you should call this function after having ensured that + * str1 and str2 are in the same length */ + + for (i = 0; i < len; i++) + { + if (str1[i] != str2[i]) return 0; + } + + return 1; +} + +int hcl_equal_bchars (const hcl_bch_t* str1, const hcl_bch_t* str2, hcl_oow_t len) +{ + hcl_oow_t i; + + /* NOTE: you should call this function after having ensured that + * str1 and str2 are in the same length */ + + for (i = 0; i < len; i++) + { + if (str1[i] != str2[i]) return 0; + } + + return 1; +} + +int hcl_comp_uchars (const hcl_uch_t* str1, hcl_oow_t len1, const hcl_uch_t* str2, hcl_oow_t len2) +{ + hcl_uchu_t c1, c2; + const hcl_uch_t* end1 = str1 + len1; + const hcl_uch_t* end2 = str2 + len2; + + while (str1 < end1) + { + c1 = *str1; + if (str2 < end2) + { + c2 = *str2; + if (c1 > c2) return 1; + if (c1 < c2) return -1; + } + else return 1; + str1++; str2++; + } + + return (str2 < end2)? -1: 0; +} + +int hcl_comp_bchars (const hcl_bch_t* str1, hcl_oow_t len1, const hcl_bch_t* str2, hcl_oow_t len2) +{ + hcl_bchu_t c1, c2; + const hcl_bch_t* end1 = str1 + len1; + const hcl_bch_t* end2 = str2 + len2; + + while (str1 < end1) + { + c1 = *str1; + if (str2 < end2) + { + c2 = *str2; + if (c1 > c2) return 1; + if (c1 < c2) return -1; + } + else return 1; + str1++; str2++; + } + + return (str2 < end2)? -1: 0; +} + +int hcl_comp_ucstr (const hcl_uch_t* str1, const hcl_uch_t* str2) +{ + while (*str1 == *str2) + { + if (*str1 == '\0') return 0; + str1++; str2++; + } + + return ((hcl_uchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1; +} + +int hcl_comp_bcstr (const hcl_bch_t* str1, const hcl_bch_t* str2) +{ + while (*str1 == *str2) + { + if (*str1 == '\0') return 0; + str1++; str2++; + } + + return ((hcl_bchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1; +} + +int hcl_comp_ucstr_bcstr (const hcl_uch_t* str1, const hcl_bch_t* str2) +{ + while (*str1 == *str2) + { + if (*str1 == '\0') return 0; + str1++; str2++; + } + + return ((hcl_uchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1; +} + +int hcl_comp_uchars_ucstr (const hcl_uch_t* str1, hcl_oow_t len, const hcl_uch_t* str2) +{ + /* for "abc\0" of length 4 vs "abc", the fourth character + * of the first string is equal to the terminating null of + * the second string. the first string is still considered + * bigger */ + const hcl_uch_t* end = str1 + len; + while (str1 < end && *str2 != '\0') + { + if (*str1 != *str2) return ((hcl_uchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1; + str1++; str2++; + } + return (str1 < end)? 1: (*str2 == '\0'? 0: -1); +} + +int hcl_comp_uchars_bcstr (const hcl_uch_t* str1, hcl_oow_t len, const hcl_bch_t* str2) +{ + const hcl_uch_t* end = str1 + len; + while (str1 < end && *str2 != '\0') + { + if (*str1 != *str2) return ((hcl_uchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1; + str1++; str2++; + } + return (str1 < end)? 1: (*str2 == '\0'? 0: -1); +} + +int hcl_comp_bchars_bcstr (const hcl_bch_t* str1, hcl_oow_t len, const hcl_bch_t* str2) +{ + const hcl_bch_t* end = str1 + len; + while (str1 < end && *str2 != '\0') + { + if (*str1 != *str2) return ((hcl_bchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1; + str1++; str2++; + } + return (str1 < end)? 1: (*str2 == '\0'? 0: -1); +} + +int hcl_comp_bchars_ucstr (const hcl_bch_t* str1, hcl_oow_t len, const hcl_uch_t* str2) +{ + const hcl_bch_t* end = str1 + len; + while (str1 < end && *str2 != '\0') + { + if (*str1 != *str2) return ((hcl_bchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1; + str1++; str2++; + } + return (str1 < end)? 1: (*str2 == '\0'? 0: -1); +} + +void hcl_copy_uchars (hcl_uch_t* dst, const hcl_uch_t* src, hcl_oow_t len) +{ + /* take note of no forced null termination */ + hcl_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; +} + +void hcl_copy_bchars (hcl_bch_t* dst, const hcl_bch_t* src, hcl_oow_t len) +{ + /* take note of no forced null termination */ + hcl_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; +} + +void hcl_copy_bchars_to_uchars (hcl_uch_t* dst, const hcl_bch_t* src, hcl_oow_t len) +{ + /* copy without conversions. + * use hcl_convbtouchars() for conversion encoding */ + hcl_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; +} + +void hcl_copy_uchars_to_bchars (hcl_bch_t* dst, const hcl_uch_t* src, hcl_oow_t len) +{ + /* copy without conversions. + * use hcl_convutobchars() for conversion encoding */ + hcl_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; +} + +hcl_oow_t hcl_copy_bcstr_to_ucstr (hcl_uch_t* dst, hcl_oow_t len, const hcl_bch_t* src) +{ + /* copy without conversions. + * the code is the same as hcl_copy_bcstr() except type of src */ + hcl_uch_t* p, * p2; + + p = dst; p2 = dst + len - 1; + + while (p < p2) + { + if (*src == '\0') break; + *p++ = *src++; + } + + if (len > 0) *p = '\0'; + return p - dst; +} + +hcl_oow_t hcl_copy_ucstr_to_bcstr (hcl_bch_t* dst, hcl_oow_t len, const hcl_uch_t* src) +{ + /* copy without conversions */ + hcl_bch_t* p, * p2; + + p = dst; p2 = dst + len - 1; + + while (p < p2) + { + if (*src == '\0') break; + *p++ = *src++; + } + + if (len > 0) *p = '\0'; + return p - dst; +} + + +hcl_oow_t hcl_copy_uchars_to_ucstr (hcl_uch_t* dst, hcl_oow_t dlen, const hcl_uch_t* src, hcl_oow_t slen) +{ + hcl_oow_t i; + if (dlen <= 0) return 0; + if (dlen <= slen) slen = dlen - 1; + for (i = 0; i < slen; i++) dst[i] = src[i]; + dst[i] = '\0'; + return i; +} + +hcl_oow_t hcl_copy_bchars_to_bcstr (hcl_bch_t* dst, hcl_oow_t dlen, const hcl_bch_t* src, hcl_oow_t slen) +{ + hcl_oow_t i; + if (dlen <= 0) return 0; + if (dlen <= slen) slen = dlen - 1; + for (i = 0; i < slen; i++) dst[i] = src[i]; + dst[i] = '\0'; + return i; +} + +hcl_oow_t hcl_copy_uchars_to_ucstr_unlimited (hcl_uch_t* dst, const hcl_uch_t* src, hcl_oow_t len) +{ + hcl_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; + dst[i] = '\0'; + return i; +} + +hcl_oow_t hcl_copy_bchars_to_bcstr_unlimited (hcl_bch_t* dst, const hcl_bch_t* src, hcl_oow_t len) +{ + hcl_oow_t i; + for (i = 0; i < len; i++) dst[i] = src[i]; + dst[i] = '\0'; + return i; +} + +hcl_oow_t hcl_copy_ucstr (hcl_uch_t* dst, hcl_oow_t len, const hcl_uch_t* src) +{ + hcl_uch_t* p, * p2; + + p = dst; p2 = dst + len - 1; + + while (p < p2) + { + if (*src == '\0') break; + *p++ = *src++; + } + + if (len > 0) *p = '\0'; + return p - dst; +} + +hcl_oow_t hcl_copy_bcstr (hcl_bch_t* dst, hcl_oow_t len, const hcl_bch_t* src) +{ + hcl_bch_t* p, * p2; + + p = dst; p2 = dst + len - 1; + + while (p < p2) + { + if (*src == '\0') break; + *p++ = *src++; + } + + if (len > 0) *p = '\0'; + return p - dst; +} + + +hcl_oow_t hcl_copy_ucstr_unlimited (hcl_uch_t* dst, const hcl_uch_t* src) +{ + hcl_uch_t* org = dst; + while ((*dst++ = *src++) != '\0'); + return dst - org - 1; +} + +hcl_oow_t hcl_copy_bcstr_unlimited (hcl_bch_t* dst, const hcl_bch_t* src) +{ + hcl_bch_t* org = dst; + while ((*dst++ = *src++) != '\0'); + return dst - org - 1; +} + +void hcl_fill_uchars (hcl_uch_t* dst, hcl_uch_t ch, hcl_oow_t len) +{ + hcl_oow_t i; + for (i = 0; i < len; i++) dst[i] = ch; +} + +void hcl_fill_bchars (hcl_bch_t* dst, hcl_bch_t ch, hcl_oow_t len) +{ + hcl_oow_t i; + for (i = 0; i < len; i++) dst[i] = ch; +} + +hcl_oow_t hcl_count_ucstr (const hcl_uch_t* str) +{ + const hcl_uch_t* ptr = str; + while (*ptr != '\0') ptr++; + return ptr - str; +} + +hcl_oow_t hcl_count_bcstr (const hcl_bch_t* str) +{ + const hcl_bch_t* ptr = str; + while (*ptr != '\0') ptr++; + return ptr - str; +} + +hcl_uch_t* hcl_find_uchar (const hcl_uch_t* ptr, hcl_oow_t len, hcl_uch_t c) +{ + const hcl_uch_t* end; + + end = ptr + len; + while (ptr < end) + { + if (*ptr == c) return (hcl_uch_t*)ptr; + ptr++; + } + + return HCL_NULL; +} + +hcl_bch_t* hcl_find_bchar (const hcl_bch_t* ptr, hcl_oow_t len, hcl_bch_t c) +{ + const hcl_bch_t* end; + + end = ptr + len; + while (ptr < end) + { + if (*ptr == c) return (hcl_bch_t*)ptr; + ptr++; + } + + return HCL_NULL; +} + +hcl_uch_t* hcl_rfind_uchar (const hcl_uch_t* ptr, hcl_oow_t len, hcl_uch_t c) +{ + const hcl_uch_t* cur; + + cur = ptr + len; + while (cur > ptr) + { + --cur; + if (*cur == c) return (hcl_uch_t*)cur; + } + + return HCL_NULL; +} + +hcl_bch_t* hcl_rfind_bchar (const hcl_bch_t* ptr, hcl_oow_t len, hcl_bch_t c) +{ + const hcl_bch_t* cur; + + cur = ptr + len; + while (cur > ptr) + { + --cur; + if (*cur == c) return (hcl_bch_t*)cur; + } + + return HCL_NULL; +} + +hcl_uch_t* hcl_find_uchar_in_ucstr (const hcl_uch_t* ptr, hcl_uch_t c) +{ + while (*ptr != '\0') + { + if (*ptr == c) return (hcl_uch_t*)ptr; + ptr++; + } + + return HCL_NULL; +} + +hcl_bch_t* hcl_find_bchar_in_bcstr (const hcl_bch_t* ptr, hcl_bch_t c) +{ + while (*ptr != '\0') + { + if (*ptr == c) return (hcl_bch_t*)ptr; + ptr++; + } + + return HCL_NULL; +} + +/* ----------------------------------------------------------------------- */ + +hcl_oow_t hcl_rotate_uchars (hcl_uch_t* str, hcl_oow_t len, int dir, hcl_oow_t n) +{ + hcl_oow_t first, last, count, index, nk; + hcl_uch_t c; + + if (dir == 0 || len == 0) return len; + if ((n %= len) == 0) return len; + + if (dir > 0) n = len - n; + first = 0; nk = len - n; count = 0; + + while (count < n) + { + last = first + nk; + index = first; + c = str[first]; + do + { + count++; + while (index < nk) + { + str[index] = str[index + n]; + index += n; + } + if (index == last) break; + str[index] = str[index - nk]; + index -= nk; + } + while (1); + str[last] = c; first++; + } + return len; +} + +hcl_oow_t hcl_rotate_bchars (hcl_bch_t* str, hcl_oow_t len, int dir, hcl_oow_t n) +{ + hcl_oow_t first, last, count, index, nk; + hcl_bch_t c; + + if (dir == 0 || len == 0) return len; + if ((n %= len) == 0) return len; + + if (dir > 0) n = len - n; + first = 0; nk = len - n; count = 0; + + while (count < n) + { + last = first + nk; + index = first; + c = str[first]; + do + { + count++; + while (index < nk) + { + str[index] = str[index + n]; + index += n; + } + if (index == last) break; + str[index] = str[index - nk]; + index -= nk; + } + while (1); + str[last] = c; first++; + } + return len; +} + +/* ----------------------------------------------------------------------- */ + +hcl_oow_t hcl_byte_to_bcstr (hcl_uint8_t byte, hcl_bch_t* buf, hcl_oow_t size, int flagged_radix, hcl_bch_t fill) +{ + hcl_bch_t tmp[(HCL_SIZEOF(hcl_uint8_t) * HCL_BITS_PER_BYTE)]; + hcl_bch_t* p = tmp, * bp = buf, * be = buf + size - 1; + int radix; + hcl_bch_t radix_char; + + radix = (flagged_radix & HCL_BYTE_TO_BCSTR_RADIXMASK); + radix_char = (flagged_radix & HCL_BYTE_TO_BCSTR_LOWERCASE)? 'a': 'A'; + if (radix < 2 || radix > 36 || size <= 0) return 0; + + do + { + hcl_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; +} + +/* ----------------------------------------------------------------------- */ + +int hcl_conv_bchars_to_uchars_with_cmgr (const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_cmgr_t* cmgr, int all) +{ + const hcl_bch_t* p; + int ret = 0; + hcl_oow_t mlen; + + if (ucs) + { + /* destination buffer is specified. + * copy the conversion result to the buffer */ + + hcl_uch_t* q, * qend; + + p = bcs; + q = ucs; + qend = ucs + *ucslen; + mlen = *bcslen; + + while (mlen > 0) + { + hcl_oow_t n; + + if (q >= qend) + { + /* buffer too small */ + ret = -2; + break; + } + + n = cmgr->bctouc(p, mlen, q); + if (n == 0) + { + /* invalid sequence */ + if (all) + { + n = 1; + *q = '?'; + } + else + { + ret = -1; + break; + } + } + if (n > mlen) + { + /* incomplete sequence */ + if (all) + { + n = 1; + *q = '?'; + } + else + { + ret = -3; + break; + } + } + + q++; + p += n; + mlen -= n; + } + + *ucslen = q - ucs; + *bcslen = p - bcs; + } + else + { + /* no destination buffer is specified. perform conversion + * but don't copy the result. the caller can call this function + * without a buffer to find the required buffer size, allocate + * a buffer with the size and call this function again with + * the buffer. */ + + hcl_uch_t w; + hcl_oow_t wlen = 0; + + p = bcs; + mlen = *bcslen; + + while (mlen > 0) + { + hcl_oow_t n; + + n = cmgr->bctouc(p, mlen, &w); + if (n == 0) + { + /* invalid sequence */ + if (all) n = 1; + else + { + ret = -1; + break; + } + } + if (n > mlen) + { + /* incomplete sequence */ + if (all) n = 1; + else + { + ret = -3; + break; + } + } + + p += n; + mlen -= n; + wlen += 1; + } + + *ucslen = wlen; + *bcslen = p - bcs; + } + + return ret; +} + +int hcl_conv_bcstr_to_ucstr_with_cmgr (const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_cmgr_t* cmgr, int all) +{ + const hcl_bch_t* bp; + hcl_oow_t mlen, wlen; + int n; + + for (bp = bcs; *bp != '\0'; bp++) /* nothing */ ; + + mlen = bp - bcs; wlen = *ucslen; + n = hcl_conv_bchars_to_uchars_with_cmgr(bcs, &mlen, ucs, &wlen, cmgr, all); + if (ucs) + { + /* null-terminate the target buffer if it has room for it. */ + if (wlen < *ucslen) ucs[wlen] = '\0'; + else n = -2; /* buffer too small */ + } + *bcslen = mlen; *ucslen = wlen; + + return n; +} + +int hcl_conv_uchars_to_bchars_with_cmgr (const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_cmgr_t* cmgr) +{ + const hcl_uch_t* p = ucs; + const hcl_uch_t* end = ucs + *ucslen; + int ret = 0; + + if (bcs) + { + hcl_oow_t rem = *bcslen; + + while (p < end) + { + hcl_oow_t n; + + if (rem <= 0) + { + ret = -2; /* buffer too small */ + break; + } + + n = cmgr->uctobc(*p, bcs, rem); + if (n == 0) + { + ret = -1; + break; /* illegal character */ + } + if (n > rem) + { + ret = -2; /* buffer too small */ + break; + } + bcs += n; rem -= n; p++; + } + + *bcslen -= rem; + } + else + { + hcl_bch_t bcsbuf[HCL_BCSIZE_MAX]; + hcl_oow_t mlen = 0; + + while (p < end) + { + hcl_oow_t n; + + n = cmgr->uctobc(*p, bcsbuf, HCL_COUNTOF(bcsbuf)); + if (n == 0) + { + ret = -1; + break; /* illegal character */ + } + + /* it assumes that bcsbuf is large enough to hold a character */ + /*HCL_ASSERT (hcl, n <= HCL_COUNTOF(bcsbuf));*/ + + p++; mlen += n; + } + + /* this length excludes the terminating null character. + * this function doesn't even null-terminate the result. */ + *bcslen = mlen; + } + + *ucslen = p - ucs; + return ret; +} + +int hcl_conv_ucstr_to_bcstr_with_cmgr (const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_cmgr_t* cmgr) +{ + const hcl_uch_t* p = ucs; + int ret = 0; + + if (bcs) + { + hcl_oow_t rem = *bcslen; + + while (*p != '\0') + { + hcl_oow_t n; + + if (rem <= 0) + { + ret = -2; + break; + } + + n = cmgr->uctobc(*p, bcs, rem); + if (n == 0) + { + ret = -1; + break; /* illegal character */ + } + if (n > rem) + { + ret = -2; + break; /* buffer too small */ + } + + bcs += n; rem -= n; p++; + } + + /* update bcslen to the length of the bcs string converted excluding + * terminating null */ + *bcslen -= rem; + + /* null-terminate the multibyte sequence if it has sufficient space */ + if (rem > 0) *bcs = '\0'; + else + { + /* if ret is -2 and cs[cslen] == '\0', + * this means that the bcs buffer was lacking one + * slot for the terminating null */ + ret = -2; /* buffer too small */ + } + } + else + { + hcl_bch_t bcsbuf[HCL_BCSIZE_MAX]; + hcl_oow_t mlen = 0; + + while (*p != '\0') + { + hcl_oow_t n; + + n = cmgr->uctobc(*p, bcsbuf, HCL_COUNTOF(bcsbuf)); + if (n == 0) + { + ret = -1; + break; /* illegal character */ + } + + /* it assumes that bcs is large enough to hold a character */ + /*HCL_ASSERT (hcl, n <= HCL_COUNTOF(bcs));*/ + + p++; mlen += n; + } + + /* this length holds the number of resulting multi-byte characters + * excluding the terminating null character */ + *bcslen = mlen; + } + + *ucslen = p - ucs; /* the number of wide characters handled. */ + return ret; +} diff --git a/lib/utl.c b/lib/utl.c index d6e80e6..25c2c23 100644 --- a/lib/utl.c +++ b/lib/utl.c @@ -42,808 +42,6 @@ hcl_oow_t hcl_hash_bytes_ (const hcl_oob_t* ptr, hcl_oow_t len) return hv % ((hcl_oow_t)HCL_SMOOI_MAX + 1); } -int hcl_equal_uchars (const hcl_uch_t* str1, const hcl_uch_t* str2, hcl_oow_t len) -{ - hcl_oow_t i; - - /* NOTE: you should call this function after having ensured that - * str1 and str2 are in the same length */ - - for (i = 0; i < len; i++) - { - if (str1[i] != str2[i]) return 0; - } - - return 1; -} - -int hcl_equal_bchars (const hcl_bch_t* str1, const hcl_bch_t* str2, hcl_oow_t len) -{ - hcl_oow_t i; - - /* NOTE: you should call this function after having ensured that - * str1 and str2 are in the same length */ - - for (i = 0; i < len; i++) - { - if (str1[i] != str2[i]) return 0; - } - - return 1; -} - -int hcl_comp_uchars (const hcl_uch_t* str1, hcl_oow_t len1, const hcl_uch_t* str2, hcl_oow_t len2) -{ - hcl_uchu_t c1, c2; - const hcl_uch_t* end1 = str1 + len1; - const hcl_uch_t* end2 = str2 + len2; - - while (str1 < end1) - { - c1 = *str1; - if (str2 < end2) - { - c2 = *str2; - if (c1 > c2) return 1; - if (c1 < c2) return -1; - } - else return 1; - str1++; str2++; - } - - return (str2 < end2)? -1: 0; -} - -int hcl_comp_bchars (const hcl_bch_t* str1, hcl_oow_t len1, const hcl_bch_t* str2, hcl_oow_t len2) -{ - hcl_bchu_t c1, c2; - const hcl_bch_t* end1 = str1 + len1; - const hcl_bch_t* end2 = str2 + len2; - - while (str1 < end1) - { - c1 = *str1; - if (str2 < end2) - { - c2 = *str2; - if (c1 > c2) return 1; - if (c1 < c2) return -1; - } - else return 1; - str1++; str2++; - } - - return (str2 < end2)? -1: 0; -} - -int hcl_comp_ucstr (const hcl_uch_t* str1, const hcl_uch_t* str2) -{ - while (*str1 == *str2) - { - if (*str1 == '\0') return 0; - str1++; str2++; - } - - return ((hcl_uchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1; -} - -int hcl_comp_bcstr (const hcl_bch_t* str1, const hcl_bch_t* str2) -{ - while (*str1 == *str2) - { - if (*str1 == '\0') return 0; - str1++; str2++; - } - - return ((hcl_bchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1; -} - -int hcl_comp_ucstr_bcstr (const hcl_uch_t* str1, const hcl_bch_t* str2) -{ - while (*str1 == *str2) - { - if (*str1 == '\0') return 0; - str1++; str2++; - } - - return ((hcl_uchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1; -} - -int hcl_comp_uchars_ucstr (const hcl_uch_t* str1, hcl_oow_t len, const hcl_uch_t* str2) -{ - /* for "abc\0" of length 4 vs "abc", the fourth character - * of the first string is equal to the terminating null of - * the second string. the first string is still considered - * bigger */ - const hcl_uch_t* end = str1 + len; - while (str1 < end && *str2 != '\0') - { - if (*str1 != *str2) return ((hcl_uchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1; - str1++; str2++; - } - return (str1 < end)? 1: (*str2 == '\0'? 0: -1); -} - -int hcl_comp_uchars_bcstr (const hcl_uch_t* str1, hcl_oow_t len, const hcl_bch_t* str2) -{ - const hcl_uch_t* end = str1 + len; - while (str1 < end && *str2 != '\0') - { - if (*str1 != *str2) return ((hcl_uchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1; - str1++; str2++; - } - return (str1 < end)? 1: (*str2 == '\0'? 0: -1); -} - -int hcl_comp_bchars_bcstr (const hcl_bch_t* str1, hcl_oow_t len, const hcl_bch_t* str2) -{ - const hcl_bch_t* end = str1 + len; - while (str1 < end && *str2 != '\0') - { - if (*str1 != *str2) return ((hcl_bchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1; - str1++; str2++; - } - return (str1 < end)? 1: (*str2 == '\0'? 0: -1); -} - -int hcl_comp_bchars_ucstr (const hcl_bch_t* str1, hcl_oow_t len, const hcl_uch_t* str2) -{ - const hcl_bch_t* end = str1 + len; - while (str1 < end && *str2 != '\0') - { - if (*str1 != *str2) return ((hcl_bchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1; - str1++; str2++; - } - return (str1 < end)? 1: (*str2 == '\0'? 0: -1); -} - -void hcl_copy_uchars (hcl_uch_t* dst, const hcl_uch_t* src, hcl_oow_t len) -{ - /* take note of no forced null termination */ - hcl_oow_t i; - for (i = 0; i < len; i++) dst[i] = src[i]; -} - -void hcl_copy_bchars (hcl_bch_t* dst, const hcl_bch_t* src, hcl_oow_t len) -{ - /* take note of no forced null termination */ - hcl_oow_t i; - for (i = 0; i < len; i++) dst[i] = src[i]; -} - -void hcl_copy_bchars_to_uchars (hcl_uch_t* dst, const hcl_bch_t* src, hcl_oow_t len) -{ - /* copy without conversions. - * use hcl_convbtouchars() for conversion encoding */ - hcl_oow_t i; - for (i = 0; i < len; i++) dst[i] = src[i]; -} - -void hcl_copy_uchars_to_bchars (hcl_bch_t* dst, const hcl_uch_t* src, hcl_oow_t len) -{ - /* copy without conversions. - * use hcl_convutobchars() for conversion encoding */ - hcl_oow_t i; - for (i = 0; i < len; i++) dst[i] = src[i]; -} - -hcl_oow_t hcl_copy_bcstr_to_ucstr (hcl_uch_t* dst, hcl_oow_t len, const hcl_bch_t* src) -{ - /* copy without conversions. - * the code is the same as hcl_copy_bcstr() except type of src */ - hcl_uch_t* p, * p2; - - p = dst; p2 = dst + len - 1; - - while (p < p2) - { - if (*src == '\0') break; - *p++ = *src++; - } - - if (len > 0) *p = '\0'; - return p - dst; -} - -hcl_oow_t hcl_copy_ucstr_to_bcstr (hcl_bch_t* dst, hcl_oow_t len, const hcl_uch_t* src) -{ - /* copy without conversions */ - hcl_bch_t* p, * p2; - - p = dst; p2 = dst + len - 1; - - while (p < p2) - { - if (*src == '\0') break; - *p++ = *src++; - } - - if (len > 0) *p = '\0'; - return p - dst; -} - - -hcl_oow_t hcl_copy_uchars_to_ucstr (hcl_uch_t* dst, hcl_oow_t dlen, const hcl_uch_t* src, hcl_oow_t slen) -{ - hcl_oow_t i; - if (dlen <= 0) return 0; - if (dlen <= slen) slen = dlen - 1; - for (i = 0; i < slen; i++) dst[i] = src[i]; - dst[i] = '\0'; - return i; -} - -hcl_oow_t hcl_copy_bchars_to_bcstr (hcl_bch_t* dst, hcl_oow_t dlen, const hcl_bch_t* src, hcl_oow_t slen) -{ - hcl_oow_t i; - if (dlen <= 0) return 0; - if (dlen <= slen) slen = dlen - 1; - for (i = 0; i < slen; i++) dst[i] = src[i]; - dst[i] = '\0'; - return i; -} - -hcl_oow_t hcl_copy_uchars_to_ucstr_unlimited (hcl_uch_t* dst, const hcl_uch_t* src, hcl_oow_t len) -{ - hcl_oow_t i; - for (i = 0; i < len; i++) dst[i] = src[i]; - dst[i] = '\0'; - return i; -} - -hcl_oow_t hcl_copy_bchars_to_bcstr_unlimited (hcl_bch_t* dst, const hcl_bch_t* src, hcl_oow_t len) -{ - hcl_oow_t i; - for (i = 0; i < len; i++) dst[i] = src[i]; - dst[i] = '\0'; - return i; -} - -hcl_oow_t hcl_copy_ucstr (hcl_uch_t* dst, hcl_oow_t len, const hcl_uch_t* src) -{ - hcl_uch_t* p, * p2; - - p = dst; p2 = dst + len - 1; - - while (p < p2) - { - if (*src == '\0') break; - *p++ = *src++; - } - - if (len > 0) *p = '\0'; - return p - dst; -} - -hcl_oow_t hcl_copy_bcstr (hcl_bch_t* dst, hcl_oow_t len, const hcl_bch_t* src) -{ - hcl_bch_t* p, * p2; - - p = dst; p2 = dst + len - 1; - - while (p < p2) - { - if (*src == '\0') break; - *p++ = *src++; - } - - if (len > 0) *p = '\0'; - return p - dst; -} - - -hcl_oow_t hcl_copy_ucstr_unlimited (hcl_uch_t* dst, const hcl_uch_t* src) -{ - hcl_uch_t* org = dst; - while ((*dst++ = *src++) != '\0'); - return dst - org - 1; -} - -hcl_oow_t hcl_copy_bcstr_unlimited (hcl_bch_t* dst, const hcl_bch_t* src) -{ - hcl_bch_t* org = dst; - while ((*dst++ = *src++) != '\0'); - return dst - org - 1; -} - -void hcl_fill_uchars (hcl_uch_t* dst, hcl_uch_t ch, hcl_oow_t len) -{ - hcl_oow_t i; - for (i = 0; i < len; i++) dst[i] = ch; -} - -void hcl_fill_bchars (hcl_bch_t* dst, hcl_bch_t ch, hcl_oow_t len) -{ - hcl_oow_t i; - for (i = 0; i < len; i++) dst[i] = ch; -} - -hcl_oow_t hcl_count_ucstr (const hcl_uch_t* str) -{ - const hcl_uch_t* ptr = str; - while (*ptr != '\0') ptr++; - return ptr - str; -} - -hcl_oow_t hcl_count_bcstr (const hcl_bch_t* str) -{ - const hcl_bch_t* ptr = str; - while (*ptr != '\0') ptr++; - return ptr - str; -} - -hcl_uch_t* hcl_find_uchar (const hcl_uch_t* ptr, hcl_oow_t len, hcl_uch_t c) -{ - const hcl_uch_t* end; - - end = ptr + len; - while (ptr < end) - { - if (*ptr == c) return (hcl_uch_t*)ptr; - ptr++; - } - - return HCL_NULL; -} - -hcl_bch_t* hcl_find_bchar (const hcl_bch_t* ptr, hcl_oow_t len, hcl_bch_t c) -{ - const hcl_bch_t* end; - - end = ptr + len; - while (ptr < end) - { - if (*ptr == c) return (hcl_bch_t*)ptr; - ptr++; - } - - return HCL_NULL; -} - -hcl_uch_t* hcl_rfind_uchar (const hcl_uch_t* ptr, hcl_oow_t len, hcl_uch_t c) -{ - const hcl_uch_t* cur; - - cur = ptr + len; - while (cur > ptr) - { - --cur; - if (*cur == c) return (hcl_uch_t*)cur; - } - - return HCL_NULL; -} - -hcl_bch_t* hcl_rfind_bchar (const hcl_bch_t* ptr, hcl_oow_t len, hcl_bch_t c) -{ - const hcl_bch_t* cur; - - cur = ptr + len; - while (cur > ptr) - { - --cur; - if (*cur == c) return (hcl_bch_t*)cur; - } - - return HCL_NULL; -} - -hcl_uch_t* hcl_find_uchar_in_ucstr (const hcl_uch_t* ptr, hcl_uch_t c) -{ - while (*ptr != '\0') - { - if (*ptr == c) return (hcl_uch_t*)ptr; - ptr++; - } - - return HCL_NULL; -} - -hcl_bch_t* hcl_find_bchar_in_bcstr (const hcl_bch_t* ptr, hcl_bch_t c) -{ - while (*ptr != '\0') - { - if (*ptr == c) return (hcl_bch_t*)ptr; - ptr++; - } - - return HCL_NULL; -} - -/* ----------------------------------------------------------------------- */ - -hcl_oow_t hcl_rotate_uchars (hcl_uch_t* str, hcl_oow_t len, int dir, hcl_oow_t n) -{ - hcl_oow_t first, last, count, index, nk; - hcl_uch_t c; - - if (dir == 0 || len == 0) return len; - if ((n %= len) == 0) return len; - - if (dir > 0) n = len - n; - first = 0; nk = len - n; count = 0; - - while (count < n) - { - last = first + nk; - index = first; - c = str[first]; - do - { - count++; - while (index < nk) - { - str[index] = str[index + n]; - index += n; - } - if (index == last) break; - str[index] = str[index - nk]; - index -= nk; - } - while (1); - str[last] = c; first++; - } - return len; -} - -hcl_oow_t hcl_rotate_bchars (hcl_bch_t* str, hcl_oow_t len, int dir, hcl_oow_t n) -{ - hcl_oow_t first, last, count, index, nk; - hcl_bch_t c; - - if (dir == 0 || len == 0) return len; - if ((n %= len) == 0) return len; - - if (dir > 0) n = len - n; - first = 0; nk = len - n; count = 0; - - while (count < n) - { - last = first + nk; - index = first; - c = str[first]; - do - { - count++; - while (index < nk) - { - str[index] = str[index + n]; - index += n; - } - if (index == last) break; - str[index] = str[index - nk]; - index -= nk; - } - while (1); - str[last] = c; first++; - } - return len; -} - -/* ----------------------------------------------------------------------- */ - -hcl_oow_t hcl_byte_to_bcstr (hcl_uint8_t byte, hcl_bch_t* buf, hcl_oow_t size, int flagged_radix, hcl_bch_t fill) -{ - hcl_bch_t tmp[(HCL_SIZEOF(hcl_uint8_t) * HCL_BITS_PER_BYTE)]; - hcl_bch_t* p = tmp, * bp = buf, * be = buf + size - 1; - int radix; - hcl_bch_t radix_char; - - radix = (flagged_radix & HCL_BYTE_TO_BCSTR_RADIXMASK); - radix_char = (flagged_radix & HCL_BYTE_TO_BCSTR_LOWERCASE)? 'a': 'A'; - if (radix < 2 || radix > 36 || size <= 0) return 0; - - do - { - hcl_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; -} - -/* ----------------------------------------------------------------------- */ - -int hcl_conv_bchars_to_uchars_with_cmgr (const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_cmgr_t* cmgr, int all) -{ - const hcl_bch_t* p; - int ret = 0; - hcl_oow_t mlen; - - if (ucs) - { - /* destination buffer is specified. - * copy the conversion result to the buffer */ - - hcl_uch_t* q, * qend; - - p = bcs; - q = ucs; - qend = ucs + *ucslen; - mlen = *bcslen; - - while (mlen > 0) - { - hcl_oow_t n; - - if (q >= qend) - { - /* buffer too small */ - ret = -2; - break; - } - - n = cmgr->bctouc(p, mlen, q); - if (n == 0) - { - /* invalid sequence */ - if (all) - { - n = 1; - *q = '?'; - } - else - { - ret = -1; - break; - } - } - if (n > mlen) - { - /* incomplete sequence */ - if (all) - { - n = 1; - *q = '?'; - } - else - { - ret = -3; - break; - } - } - - q++; - p += n; - mlen -= n; - } - - *ucslen = q - ucs; - *bcslen = p - bcs; - } - else - { - /* no destination buffer is specified. perform conversion - * but don't copy the result. the caller can call this function - * without a buffer to find the required buffer size, allocate - * a buffer with the size and call this function again with - * the buffer. */ - - hcl_uch_t w; - hcl_oow_t wlen = 0; - - p = bcs; - mlen = *bcslen; - - while (mlen > 0) - { - hcl_oow_t n; - - n = cmgr->bctouc(p, mlen, &w); - if (n == 0) - { - /* invalid sequence */ - if (all) n = 1; - else - { - ret = -1; - break; - } - } - if (n > mlen) - { - /* incomplete sequence */ - if (all) n = 1; - else - { - ret = -3; - break; - } - } - - p += n; - mlen -= n; - wlen += 1; - } - - *ucslen = wlen; - *bcslen = p - bcs; - } - - return ret; -} - -int hcl_conv_bcstr_to_ucstr_with_cmgr (const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_cmgr_t* cmgr, int all) -{ - const hcl_bch_t* bp; - hcl_oow_t mlen, wlen; - int n; - - for (bp = bcs; *bp != '\0'; bp++) /* nothing */ ; - - mlen = bp - bcs; wlen = *ucslen; - n = hcl_conv_bchars_to_uchars_with_cmgr(bcs, &mlen, ucs, &wlen, cmgr, all); - if (ucs) - { - /* null-terminate the target buffer if it has room for it. */ - if (wlen < *ucslen) ucs[wlen] = '\0'; - else n = -2; /* buffer too small */ - } - *bcslen = mlen; *ucslen = wlen; - - return n; -} - -int hcl_conv_uchars_to_bchars_with_cmgr (const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_cmgr_t* cmgr) -{ - const hcl_uch_t* p = ucs; - const hcl_uch_t* end = ucs + *ucslen; - int ret = 0; - - if (bcs) - { - hcl_oow_t rem = *bcslen; - - while (p < end) - { - hcl_oow_t n; - - if (rem <= 0) - { - ret = -2; /* buffer too small */ - break; - } - - n = cmgr->uctobc(*p, bcs, rem); - if (n == 0) - { - ret = -1; - break; /* illegal character */ - } - if (n > rem) - { - ret = -2; /* buffer too small */ - break; - } - bcs += n; rem -= n; p++; - } - - *bcslen -= rem; - } - else - { - hcl_bch_t bcsbuf[HCL_BCSIZE_MAX]; - hcl_oow_t mlen = 0; - - while (p < end) - { - hcl_oow_t n; - - n = cmgr->uctobc(*p, bcsbuf, HCL_COUNTOF(bcsbuf)); - if (n == 0) - { - ret = -1; - break; /* illegal character */ - } - - /* it assumes that bcsbuf is large enough to hold a character */ - /*HCL_ASSERT (hcl, n <= HCL_COUNTOF(bcsbuf));*/ - - p++; mlen += n; - } - - /* this length excludes the terminating null character. - * this function doesn't even null-terminate the result. */ - *bcslen = mlen; - } - - *ucslen = p - ucs; - return ret; -} - -int hcl_conv_ucstr_to_bcstr_with_cmgr (const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_cmgr_t* cmgr) -{ - const hcl_uch_t* p = ucs; - int ret = 0; - - if (bcs) - { - hcl_oow_t rem = *bcslen; - - while (*p != '\0') - { - hcl_oow_t n; - - if (rem <= 0) - { - ret = -2; - break; - } - - n = cmgr->uctobc(*p, bcs, rem); - if (n == 0) - { - ret = -1; - break; /* illegal character */ - } - if (n > rem) - { - ret = -2; - break; /* buffer too small */ - } - - bcs += n; rem -= n; p++; - } - - /* update bcslen to the length of the bcs string converted excluding - * terminating null */ - *bcslen -= rem; - - /* null-terminate the multibyte sequence if it has sufficient space */ - if (rem > 0) *bcs = '\0'; - else - { - /* if ret is -2 and cs[cslen] == '\0', - * this means that the bcs buffer was lacking one - * slot for the terminating null */ - ret = -2; /* buffer too small */ - } - } - else - { - hcl_bch_t bcsbuf[HCL_BCSIZE_MAX]; - hcl_oow_t mlen = 0; - - while (*p != '\0') - { - hcl_oow_t n; - - n = cmgr->uctobc(*p, bcsbuf, HCL_COUNTOF(bcsbuf)); - if (n == 0) - { - ret = -1; - break; /* illegal character */ - } - - /* it assumes that bcs is large enough to hold a character */ - /*HCL_ASSERT (hcl, n <= HCL_COUNTOF(bcs));*/ - - p++; mlen += n; - } - - /* this length holds the number of resulting multi-byte characters - * excluding the terminating null character */ - *bcslen = mlen; - } - - *ucslen = p - ucs; /* the number of wide characters handled. */ - return ret; -} - /* ----------------------------------------------------------------------- */ static hcl_cmgr_t builtin_cmgr[] =