moving a number of string functions to utl-str.c.m4

This commit is contained in:
2021-07-20 14:40:46 +00:00
parent e4afd9378b
commit 052b79f452
12 changed files with 2055 additions and 2486 deletions

View File

@ -8,6 +8,8 @@ CPPFLAGS_ALL_COMMON = \
CFLAGS_ALL_COMMON =
LDFLAGS_ALL_COMMON = -L$(abs_builddir) -L$(libdir)
EXTRA_DIST = utl-str.c.m4
##################################################
# MAIN LIBRARY
##################################################
@ -93,7 +95,8 @@ libmio_la_SOURCES = \
tmr.c \
utf8.c \
utl.c \
utl-siph.c
utl-siph.c \
utl-str.c
libmio_la_CPPFLAGS = $(CPPFLAGS_LIB_COMMON)
@ -124,3 +127,5 @@ install-data-hook:
uninstall-hook:
@rm -f "$(DESTDIR)$(includedir)/mio-cfg.h"
utl-str.c: utl-str.c.m4
m4 $(abs_srcdir)/utl-str.c.m4 > $(abs_srcdir)/utl-str.c

View File

@ -153,7 +153,7 @@ am__libmio_la_SOURCES_DIST = chr.c dns.c dns-cli.c ecs.c ecs-imp.h \
mio-prv.h mio.c nwif.c opt.c opt-imp.h path.c pipe.c pro.c \
sck.c skad.c sys.c sys-ass.c sys-err.c sys-log.c sys-mux.c \
sys-prv.h sys-tim.c thr.c uch-case.h uch-prop.h tmr.c utf8.c \
utl.c utl-siph.c mar.c mar-cli.c
utl.c utl-siph.c utl-str.c mar.c mar-cli.c
@ENABLE_MARIADB_TRUE@am__objects_1 = libmio_la-mar.lo \
@ENABLE_MARIADB_TRUE@ libmio_la-mar-cli.lo
am_libmio_la_OBJECTS = libmio_la-chr.lo libmio_la-dns.lo \
@ -168,7 +168,7 @@ am_libmio_la_OBJECTS = libmio_la-chr.lo libmio_la-dns.lo \
libmio_la-sys-ass.lo libmio_la-sys-err.lo libmio_la-sys-log.lo \
libmio_la-sys-mux.lo libmio_la-sys-tim.lo libmio_la-thr.lo \
libmio_la-tmr.lo libmio_la-utf8.lo libmio_la-utl.lo \
libmio_la-utl-siph.lo $(am__objects_1)
libmio_la-utl-siph.lo libmio_la-utl-str.lo $(am__objects_1)
libmio_la_OBJECTS = $(am_libmio_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
@ -218,6 +218,7 @@ am__depfiles_remade = ./$(DEPDIR)/libmio_la-chr.Plo \
./$(DEPDIR)/libmio_la-sys.Plo ./$(DEPDIR)/libmio_la-thr.Plo \
./$(DEPDIR)/libmio_la-tmr.Plo ./$(DEPDIR)/libmio_la-utf8.Plo \
./$(DEPDIR)/libmio_la-utl-siph.Plo \
./$(DEPDIR)/libmio_la-utl-str.Plo \
./$(DEPDIR)/libmio_la-utl.Plo
am__mv = mv -f
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
@ -420,6 +421,7 @@ CPPFLAGS_ALL_COMMON = \
CFLAGS_ALL_COMMON =
LDFLAGS_ALL_COMMON = -L$(abs_builddir) -L$(libdir)
EXTRA_DIST = utl-str.c.m4
##################################################
# MAIN LIBRARY
@ -447,7 +449,7 @@ libmio_la_SOURCES = chr.c dns.c dns-cli.c ecs.c ecs-imp.h err.c fmt.c \
mio.c nwif.c opt.c opt-imp.h path.c pipe.c pro.c sck.c skad.c \
sys.c sys-ass.c sys-err.c sys-log.c sys-mux.c sys-prv.h \
sys-tim.c thr.c uch-case.h uch-prop.h tmr.c utf8.c utl.c \
utl-siph.c $(am__append_2)
utl-siph.c utl-str.c $(am__append_2)
libmio_la_CPPFLAGS = $(CPPFLAGS_LIB_COMMON)
libmio_la_CFLAGS = $(CFLAGS_LIB_COMMON) $(am__append_3)
libmio_la_LDFLAGS = $(LDFLAGS_LIB_COMMON) $(am__append_4)
@ -583,6 +585,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-tmr.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-utf8.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-utl-siph.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-utl-str.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-utl.Plo@am__quote@ # am--include-marker
$(am__depfiles_remade):
@ -860,6 +863,13 @@ libmio_la-utl-siph.lo: utl-siph.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) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(libmio_la_CFLAGS) $(CFLAGS) -c -o libmio_la-utl-siph.lo `test -f 'utl-siph.c' || echo '$(srcdir)/'`utl-siph.c
libmio_la-utl-str.lo: utl-str.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(libmio_la_CFLAGS) $(CFLAGS) -MT libmio_la-utl-str.lo -MD -MP -MF $(DEPDIR)/libmio_la-utl-str.Tpo -c -o libmio_la-utl-str.lo `test -f 'utl-str.c' || echo '$(srcdir)/'`utl-str.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmio_la-utl-str.Tpo $(DEPDIR)/libmio_la-utl-str.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='utl-str.c' object='libmio_la-utl-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) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(libmio_la_CFLAGS) $(CFLAGS) -c -o libmio_la-utl-str.lo `test -f 'utl-str.c' || echo '$(srcdir)/'`utl-str.c
libmio_la-mar.lo: mar.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(libmio_la_CFLAGS) $(CFLAGS) -MT libmio_la-mar.lo -MD -MP -MF $(DEPDIR)/libmio_la-mar.Tpo -c -o libmio_la-mar.lo `test -f 'mar.c' || echo '$(srcdir)/'`mar.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmio_la-mar.Tpo $(DEPDIR)/libmio_la-mar.Plo
@ -1065,6 +1075,7 @@ distclean: distclean-am
-rm -f ./$(DEPDIR)/libmio_la-tmr.Plo
-rm -f ./$(DEPDIR)/libmio_la-utf8.Plo
-rm -f ./$(DEPDIR)/libmio_la-utl-siph.Plo
-rm -f ./$(DEPDIR)/libmio_la-utl-str.Plo
-rm -f ./$(DEPDIR)/libmio_la-utl.Plo
-rm -f Makefile
distclean-am: clean-am distclean-compile distclean-generic \
@ -1148,6 +1159,7 @@ maintainer-clean: maintainer-clean-am
-rm -f ./$(DEPDIR)/libmio_la-tmr.Plo
-rm -f ./$(DEPDIR)/libmio_la-utf8.Plo
-rm -f ./$(DEPDIR)/libmio_la-utl-siph.Plo
-rm -f ./$(DEPDIR)/libmio_la-utl-str.Plo
-rm -f ./$(DEPDIR)/libmio_la-utl.Plo
-rm -f Makefile
maintainer-clean-am: distclean-am maintainer-clean-generic
@ -1200,6 +1212,9 @@ install-data-hook:
uninstall-hook:
@rm -f "$(DESTDIR)$(includedir)/mio-cfg.h"
utl-str.c: utl-str.c.m4
m4 $(abs_srcdir)/utl-str.c.m4 > $(abs_srcdir)/utl-str.c
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:

View File

@ -176,6 +176,14 @@ void mio_sys_resetlog (
mio_t* mio
);
void mio_sys_locklog (
mio_t* mio
);
void mio_sys_unlocklog (
mio_t* mio
);
void mio_sys_writelog (
mio_t* mio,
mio_bitmask_t mask,

View File

@ -141,15 +141,6 @@ void mio_sys_finilog (
mio_t* mio
);
void mio_sys_locklog (
mio_t* mio
);
void mio_sys_locklog (
mio_t* mio
);
int mio_sys_initmux (
mio_t* mio
);

505
mio/lib/utl-str.c.m4 Normal file
View File

@ -0,0 +1,505 @@
/*
* $Id$
*
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 WAfRRANTIES
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 utl-str.c.
*
* Generate utl-str.c with m4
* $ m4 utl-str.c.m4 > utl-str.c
*/
#include "mio-prv.h"
#include <mio-chr.h>
dnl ---------------------------------------------------------------------------
changequote(`[[', `]]')
dnl ---------------------------------------------------------------------------
define([[fn_equal_chars]], [[ define([[fn_name]], $1) define([[char_type]], $2)
int fn_name (const char_type* str1, const char_type* str2, mio_oow_t len)
{
mio_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;
}
]])
fn_equal_chars(mio_equal_uchars, mio_uch_t)
fn_equal_chars(mio_equal_bchars, mio_bch_t)
dnl ---------------------------------------------------------------------------
define([[fn_comp_chars]], [[ define([[fn_name]], $1) define([[char_type]], $2) define([[chau_type]], $3)
int fn_name (const char_type* str1, mio_oow_t len1, const char_type* str2, mio_oow_t len2, int ignorecase)
{
chau_type c1, c2;
const char_type* end1 = str1 + len1;
const char_type* end2 = str2 + len2;
if (ignorecase)
{
while (str1 < end1)
{
c1 = $4(*str1);
if (str2 < end2)
{
c2 = $4(*str2);
if (c1 > c2) return 1;
if (c1 < c2) return -1;
}
else return 1;
str1++; str2++;
}
}
else
{
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;
}
]])
fn_comp_chars(mio_comp_uchars, mio_uch_t, mio_uchu_t, mio_to_uch_lower)
fn_comp_chars(mio_comp_bchars, mio_bch_t, mio_bchu_t, mio_to_bch_lower)
dnl ---------------------------------------------------------------------------
define([[fn_comp_cstr]], [[ define([[fn_name]], $1) define([[char_type]], $2) define([[chau_type]], $3)
int fn_name (const char_type* str1, const char_type* str2, int ignorecase)
{
if (ignorecase)
{
while ($4(*str1) == $4(*str2))
{
if (*str1 == '\0') return 0;
str1++; str2++;
}
return ((chau_type)$4(*str1) > (chau_type)$4(*str2))? 1: -1;
}
else
{
while (*str1 == *str2)
{
if (*str1 == '\0') return 0;
str1++; str2++;
}
return ((chau_type)*str1 > (chau_type)*str2)? 1: -1;
}
}
]])
fn_comp_cstr(mio_comp_ucstr, mio_uch_t, mio_uchu_t, mio_to_uch_lower)
fn_comp_cstr(mio_comp_bcstr, mio_bch_t, mio_bchu_t, mio_to_bch_lower)
dnl ---------------------------------------------------------------------------
define([[fn_fill_chars]], [[ define([[fn_name]], $1) define([[char_type]], $2)
void fn_name (char_type* dst, char_type ch, mio_oow_t len)
{
mio_oow_t i;
for (i = 0; i < len; i++) dst[i] = ch;
}
]])
fn_fill_chars(mio_fill_uchars, mio_uch_t)
fn_fill_chars(mio_fill_bchars, mio_bch_t)
dnl ---------------------------------------------------------------------------
define([[fn_count_cstr]], [[ define([[fn_name]], $1) define([[char_type]], $2)
mio_oow_t fn_name (const char_type* str)
{
const char_type* ptr = str;
while (*ptr != '\0') ptr++;
return ptr - str;
}
]])
fn_count_cstr(mio_count_ucstr, mio_uch_t)
fn_count_cstr(mio_count_bcstr, mio_bch_t)
dnl ---------------------------------------------------------------------------
define([[fn_find_char]], [[ define([[fn_name]], $1) define([[char_type]], $2)
char_type* fn_name (const char_type* ptr, mio_oow_t len, char_type c)
{
const char_type* end;
end = ptr + len;
while (ptr < end)
{
if (*ptr == c) return (char_type*)ptr;
ptr++;
}
return MIO_NULL;
}
]])
fn_find_char(mio_find_uchar, mio_uch_t)
fn_find_char(mio_find_bchar, mio_bch_t)
dnl ---------------------------------------------------------------------------
define([[fn_rfind_char]], [[ define([[fn_name]], $1) define([[char_type]], $2)
char_type* fn_name (const char_type* ptr, mio_oow_t len, char_type c)
{
const char_type* cur;
cur = ptr + len;
while (cur > ptr)
{
--cur;
if (*cur == c) return (char_type*)cur;
}
return MIO_NULL;
}
]])
fn_rfind_char(mio_rfind_uchar, mio_uch_t)
fn_rfind_char(mio_rfind_bchar, mio_bch_t)
dnl ---------------------------------------------------------------------------
define([[fn_find_char_in_cstr]], [[ define([[fn_name]], $1) define([[char_type]], $2)
char_type* fn_name (const char_type* ptr, char_type c)
{
while (*ptr != '\0')
{
if (*ptr == c) return (char_type*)ptr;
ptr++;
}
return MIO_NULL;
}
]])
fn_find_char_in_cstr(mio_find_uchar_in_ucstr, mio_uch_t)
fn_find_char_in_cstr(mio_find_bchar_in_bcstr, mio_bch_t)
dnl ---------------------------------------------------------------------------
define([[fn_trim_chars]], [[define([[fn_name]], $1) define([[char_type]], $2) dnl $3: is_space $4: prefix for option values
char_type* fn_name (const char_type* str, mio_oow_t* len, int flags)
{
const char_type* p = str, * end = str + *len;
if (p < end)
{
const char_type* s = MIO_NULL, * e = MIO_NULL;
do
{
if (!$3(*p))
{
if (s == MIO_NULL) s = p;
e = p;
}
p++;
}
while (p < end);
if (e)
{
if (flags & $4_RIGHT)
{
*len -= end - e - 1;
}
if (flags & $4_LEFT)
{
*len -= s - str;
str = s;
}
}
else
{
/* the entire string need to be deleted */
if ((flags & $4_RIGHT) ||
(flags & $4_LEFT)) *len = 0;
}
}
return (char_type*)str;
}
]])
fn_trim_chars(mio_trim_uchars, mio_uch_t, mio_is_uch_space, MIO_TRIM_UCHARS)
fn_trim_chars(mio_trim_bchars, mio_bch_t, mio_is_bch_space, MIO_TRIM_BCHARS)
dnl ---------------------------------------------------------------------------
define([[fn_split_cstr]], [[ define([[fn_name]], $1) define([[char_type]], $2) dnl: $3 is_space $4: copy_str_unlimited
int fn_name (char_type* s, const char_type* delim, char_type lquote, char_type rquote, char_type escape)
{
char_type* p = s, *d;
char_type* sp = MIO_NULL, * ep = MIO_NULL;
int delim_mode;
int cnt = 0;
if (delim == MIO_NULL) delim_mode = 0;
else
{
delim_mode = 1;
for (d = (char_type*)delim; *d != '\0'; d++)
if (!$3(*d)) delim_mode = 2;
}
if (delim_mode == 0)
{
/* skip preceding space characters */
while ($3(*p)) p++;
/* when 0 is given as "delim", it has an effect of cutting
preceding and trailing space characters off "s". */
if (lquote != '\0' && *p == lquote)
{
$4 (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
$4 (p, p + 1);
}
else
{
if (*p == rquote)
{
p++;
break;
}
}
if (sp == 0) sp = p;
ep = p;
p++;
}
while ($3(*p)) p++;
if (*p != '\0') return -1;
if (sp == 0 && ep == 0) s[0] = '\0';
else
{
ep[1] = '\0';
if (s != (char_type*)sp) $4 (s, sp);
cnt++;
}
}
else
{
while (*p)
{
if (!$3(*p))
{
if (sp == 0) sp = p;
ep = p;
}
p++;
}
if (sp == 0 && ep == 0) s[0] = '\0';
else
{
ep[1] = '\0';
if (s != (char_type*)sp) $4 (s, sp);
cnt++;
}
}
}
else if (delim_mode == 1)
{
char_type* o;
while (*p)
{
o = p;
while ($3(*p)) p++;
if (o != p) { $4 (o, p); p = o; }
if (lquote != '\0' && *p == lquote)
{
$4 (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
$4 (p, p + 1);
}
else
{
if (*p == rquote)
{
*p++ = '\0';
cnt++;
break;
}
}
p++;
}
}
else
{
o = p;
for (;;)
{
if (*p == '\0')
{
if (o != p) cnt++;
break;
}
if ($3(*p))
{
*p++ = '\0';
cnt++;
break;
}
p++;
}
}
}
}
else /* if (delim_mode == 2) */
{
char_type* o;
int ok;
while (*p != '\0')
{
o = p;
while ($3(*p)) p++;
if (o != p) { $4 (o, p); p = o; }
if (lquote != '\0' && *p == lquote)
{
$4 (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
$4 (p, p + 1);
}
else
{
if (*p == rquote)
{
*p++ = '\0';
cnt++;
break;
}
}
p++;
}
ok = 0;
while ($3(*p)) p++;
if (*p == '\0') ok = 1;
for (d = (char_type*)delim; *d != '\0'; d++)
{
if (*p == *d)
{
ok = 1;
$4 (p, p + 1);
break;
}
}
if (ok == 0) return -1;
}
else
{
o = p; sp = ep = 0;
for (;;)
{
if (*p == '\0')
{
if (ep)
{
ep[1] = '\0';
p = &ep[1];
}
cnt++;
break;
}
for (d = (char_type*)delim; *d != '\0'; d++)
{
if (*p == *d)
{
if (sp == MIO_NULL)
{
$4 (o, p); p = o;
*p++ = '\0';
}
else
{
$4 (&ep[1], p);
$4 (o, sp);
o[ep - sp + 1] = '\0';
p = &o[ep - sp + 2];
}
cnt++;
/* last empty field after delim */
if (*p == '\0') cnt++;
goto exit_point;
}
}
if (!$3(*p))
{
if (sp == MIO_NULL) sp = p;
ep = p;
}
p++;
}
exit_point:
;
}
}
}
return cnt;
}
]])
fn_split_cstr(mio_split_ucstr, mio_uch_t, mio_is_uch_space, mio_copy_ucstr_unlimited)
fn_split_cstr(mio_split_bcstr, mio_bch_t, mio_is_bch_space, mio_copy_bcstr_unlimited)

View File

@ -29,164 +29,6 @@
/* ========================================================================= */
int mio_equal_uchars (const mio_uch_t* str1, const mio_uch_t* str2, mio_oow_t len)
{
mio_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 mio_equal_bchars (const mio_bch_t* str1, const mio_bch_t* str2, mio_oow_t len)
{
mio_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 mio_comp_uchars (const mio_uch_t* str1, mio_oow_t len1, const mio_uch_t* str2, mio_oow_t len2, int ignorecase)
{
mio_uchu_t c1, c2;
const mio_uch_t* end1 = str1 + len1;
const mio_uch_t* end2 = str2 + len2;
if (ignorecase)
{
while (str1 < end1)
{
c1 = mio_to_uch_lower(*str1);
if (str2 < end2)
{
c2 = mio_to_uch_lower(*str2);
if (c1 > c2) return 1;
if (c1 < c2) return -1;
}
else return 1;
str1++; str2++;
}
}
else
{
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 mio_comp_bchars (const mio_bch_t* str1, mio_oow_t len1, const mio_bch_t* str2, mio_oow_t len2, int ignorecase)
{
mio_bchu_t c1, c2;
const mio_bch_t* end1 = str1 + len1;
const mio_bch_t* end2 = str2 + len2;
if (ignorecase)
{
while (str1 < end1)
{
c1 = mio_to_bch_lower(*str1);
if (str2 < end2)
{
c2 = mio_to_bch_lower(*str2);
if (c1 > c2) return 1;
if (c1 < c2) return -1;
}
else return 1;
str1++; str2++;
}
}
else
{
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 mio_comp_ucstr (const mio_uch_t* str1, const mio_uch_t* str2, int ignorecase)
{
if (ignorecase)
{
while (mio_to_uch_lower(*str1) == mio_to_uch_lower(*str2))
{
if (*str1 == '\0') return 0;
str1++; str2++;
}
return ((mio_uchu_t)mio_to_uch_lower(*str1) > (mio_uchu_t)mio_to_uch_lower(*str2))? 1: -1;
}
else
{
while (*str1 == *str2)
{
if (*str1 == '\0') return 0;
str1++; str2++;
}
return ((mio_uchu_t)*str1 > (mio_uchu_t)*str2)? 1: -1;
}
}
int mio_comp_bcstr (const mio_bch_t* str1, const mio_bch_t* str2, int ignorecase)
{
if (ignorecase)
{
while (mio_to_bch_lower(*str1) == mio_to_bch_lower(*str2))
{
if (*str1 == '\0') return 0;
str1++; str2++;
}
return ((mio_bchu_t)mio_to_bch_lower(*str1) > (mio_bchu_t)mio_to_bch_lower(*str2))? 1: -1;
}
else
{
while (*str1 == *str2)
{
if (*str1 == '\0') return 0;
str1++; str2++;
}
return ((mio_bchu_t)*str1 > (mio_bchu_t)*str2)? 1: -1;
}
}
int mio_comp_ucstr_limited (const mio_uch_t* str1, const mio_uch_t* str2, mio_oow_t maxlen, int ignorecase)
{
if (maxlen == 0) return 0;
@ -495,109 +337,6 @@ mio_oow_t mio_copy_bcstr_unlimited (mio_bch_t* dst, const mio_bch_t* src)
}
/* ========================================================================= */
void mio_fill_uchars (mio_uch_t* dst, mio_uch_t ch, mio_oow_t len)
{
mio_oow_t i;
for (i = 0; i < len; i++) dst[i] = ch;
}
void mio_fill_bchars (mio_bch_t* dst, mio_bch_t ch, mio_oow_t len)
{
mio_oow_t i;
for (i = 0; i < len; i++) dst[i] = ch;
}
mio_oow_t mio_count_ucstr (const mio_uch_t* str)
{
const mio_uch_t* ptr = str;
while (*ptr != '\0') ptr++;
return ptr - str;
}
mio_oow_t mio_count_bcstr (const mio_bch_t* str)
{
const mio_bch_t* ptr = str;
while (*ptr != '\0') ptr++;
return ptr - str;
}
mio_uch_t* mio_find_uchar (const mio_uch_t* ptr, mio_oow_t len, mio_uch_t c)
{
const mio_uch_t* end;
end = ptr + len;
while (ptr < end)
{
if (*ptr == c) return (mio_uch_t*)ptr;
ptr++;
}
return MIO_NULL;
}
mio_bch_t* mio_find_bchar (const mio_bch_t* ptr, mio_oow_t len, mio_bch_t c)
{
const mio_bch_t* end;
end = ptr + len;
while (ptr < end)
{
if (*ptr == c) return (mio_bch_t*)ptr;
ptr++;
}
return MIO_NULL;
}
mio_uch_t* mio_rfind_uchar (const mio_uch_t* ptr, mio_oow_t len, mio_uch_t c)
{
const mio_uch_t* cur;
cur = ptr + len;
while (cur > ptr)
{
--cur;
if (*cur == c) return (mio_uch_t*)cur;
}
return MIO_NULL;
}
mio_bch_t* mio_rfind_bchar (const mio_bch_t* ptr, mio_oow_t len, mio_bch_t c)
{
const mio_bch_t* cur;
cur = ptr + len;
while (cur > ptr)
{
--cur;
if (*cur == c) return (mio_bch_t*)cur;
}
return MIO_NULL;
}
mio_uch_t* mio_find_uchar_in_ucstr (const mio_uch_t* ptr, mio_uch_t c)
{
while (*ptr != '\0')
{
if (*ptr == c) return (mio_uch_t*)ptr;
ptr++;
}
return MIO_NULL;
}
mio_bch_t* mio_find_bchar_in_bcstr (const mio_bch_t* ptr, mio_bch_t c)
{
while (*ptr != '\0')
{
if (*ptr == c) return (mio_bch_t*)ptr;
ptr++;
}
return MIO_NULL;
}
#define IS_BCH_WORD_DELIM(x,delim) (mio_is_bch_space(x) || (x) == delim)
#define IS_UCH_WORD_DELIM(x,delim) (mio_is_uch_space(x) || (x) == delim)
@ -652,570 +391,6 @@ const mio_uch_t* mio_find_ucstr_word_in_ucstr (const mio_uch_t* str, const mio_u
/* ========================================================================= */
mio_uch_t* mio_trim_uchars (const mio_uch_t* str, mio_oow_t* len, int flags)
{
const mio_uch_t* p = str, * end = str + *len;
if (p < end)
{
const mio_uch_t* s = MIO_NULL, * e = MIO_NULL;
do
{
if (!mio_is_uch_space(*p))
{
if (s == MIO_NULL) s = p;
e = p;
}
p++;
}
while (p < end);
if (e)
{
if (flags & MIO_TRIM_UCHARS_RIGHT)
{
*len -= end - e - 1;
}
if (flags & MIO_TRIM_UCHARS_LEFT)
{
*len -= s - str;
str = s;
}
}
else
{
/* the entire string need to be deleted */
if ((flags & MIO_TRIM_UCHARS_RIGHT) ||
(flags & MIO_TRIM_UCHARS_LEFT)) *len = 0;
}
}
return (mio_uch_t*)str;
}
mio_bch_t* mio_trim_bchars (const mio_bch_t* str, mio_oow_t* len, int flags)
{
const mio_bch_t* p = str, * end = str + *len;
if (p < end)
{
const mio_bch_t* s = MIO_NULL, * e = MIO_NULL;
do
{
if (!mio_is_bch_space(*p))
{
if (s == MIO_NULL) s = p;
e = p;
}
p++;
}
while (p < end);
if (e)
{
if (flags & MIO_TRIM_BCHARS_RIGHT)
{
*len -= end - e - 1;
}
if (flags & MIO_TRIM_BCHARS_LEFT)
{
*len -= s - str;
str = s;
}
}
else
{
/* the entire string need to be deleted */
if ((flags & MIO_TRIM_BCHARS_RIGHT) ||
(flags & MIO_TRIM_BCHARS_LEFT)) *len = 0;
}
}
return (mio_bch_t*)str;
}
/* ========================================================================= */
int mio_split_ucstr (mio_uch_t* s, const mio_uch_t* delim, mio_uch_t lquote, mio_uch_t rquote, mio_uch_t escape)
{
mio_uch_t* p = s, *d;
mio_uch_t* sp = MIO_NULL, * ep = MIO_NULL;
int delim_mode;
int cnt = 0;
if (delim == MIO_NULL) delim_mode = 0;
else
{
delim_mode = 1;
for (d = (mio_uch_t*)delim; *d != '\0'; d++)
if (!mio_is_uch_space(*d)) delim_mode = 2;
}
if (delim_mode == 0)
{
/* skip preceding space characters */
while (mio_is_uch_space(*p)) p++;
/* when 0 is given as "delim", it has an effect of cutting
preceding and trailing space characters off "s". */
if (lquote != '\0' && *p == lquote)
{
mio_copy_ucstr_unlimited (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
mio_copy_ucstr_unlimited (p, p + 1);
}
else
{
if (*p == rquote)
{
p++;
break;
}
}
if (sp == 0) sp = p;
ep = p;
p++;
}
while (mio_is_uch_space(*p)) p++;
if (*p != '\0') return -1;
if (sp == 0 && ep == 0) s[0] = '\0';
else
{
ep[1] = '\0';
if (s != (mio_uch_t*)sp) mio_copy_ucstr_unlimited (s, sp);
cnt++;
}
}
else
{
while (*p)
{
if (!mio_is_uch_space(*p))
{
if (sp == 0) sp = p;
ep = p;
}
p++;
}
if (sp == 0 && ep == 0) s[0] = '\0';
else
{
ep[1] = '\0';
if (s != (mio_uch_t*)sp) mio_copy_ucstr_unlimited (s, sp);
cnt++;
}
}
}
else if (delim_mode == 1)
{
mio_uch_t* o;
while (*p)
{
o = p;
while (mio_is_uch_space(*p)) p++;
if (o != p) { mio_copy_ucstr_unlimited (o, p); p = o; }
if (lquote != '\0' && *p == lquote)
{
mio_copy_ucstr_unlimited (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
mio_copy_ucstr_unlimited (p, p + 1);
}
else
{
if (*p == rquote)
{
*p++ = '\0';
cnt++;
break;
}
}
p++;
}
}
else
{
o = p;
for (;;)
{
if (*p == '\0')
{
if (o != p) cnt++;
break;
}
if (mio_is_uch_space(*p))
{
*p++ = '\0';
cnt++;
break;
}
p++;
}
}
}
}
else /* if (delim_mode == 2) */
{
mio_uch_t* o;
int ok;
while (*p != '\0')
{
o = p;
while (mio_is_uch_space(*p)) p++;
if (o != p) { mio_copy_ucstr_unlimited (o, p); p = o; }
if (lquote != '\0' && *p == lquote)
{
mio_copy_ucstr_unlimited (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
mio_copy_ucstr_unlimited (p, p + 1);
}
else
{
if (*p == rquote)
{
*p++ = '\0';
cnt++;
break;
}
}
p++;
}
ok = 0;
while (mio_is_uch_space(*p)) p++;
if (*p == '\0') ok = 1;
for (d = (mio_uch_t*)delim; *d != '\0'; d++)
{
if (*p == *d)
{
ok = 1;
mio_copy_ucstr_unlimited (p, p + 1);
break;
}
}
if (ok == 0) return -1;
}
else
{
o = p; sp = ep = 0;
for (;;)
{
if (*p == '\0')
{
if (ep)
{
ep[1] = '\0';
p = &ep[1];
}
cnt++;
break;
}
for (d = (mio_uch_t*)delim; *d != '\0'; d++)
{
if (*p == *d)
{
if (sp == MIO_NULL)
{
mio_copy_ucstr_unlimited (o, p); p = o;
*p++ = '\0';
}
else
{
mio_copy_ucstr_unlimited (&ep[1], p);
mio_copy_ucstr_unlimited (o, sp);
o[ep - sp + 1] = '\0';
p = &o[ep - sp + 2];
}
cnt++;
/* last empty field after delim */
if (*p == '\0') cnt++;
goto exit_point;
}
}
if (!mio_is_uch_space(*p))
{
if (sp == MIO_NULL) sp = p;
ep = p;
}
p++;
}
exit_point:
;
}
}
}
return cnt;
}
int mio_split_bcstr (mio_bch_t* s, const mio_bch_t* delim, mio_bch_t lquote, mio_bch_t rquote, mio_bch_t escape)
{
mio_bch_t* p = s, *d;
mio_bch_t* sp = MIO_NULL, * ep = MIO_NULL;
int delim_mode;
int cnt = 0;
if (delim == MIO_NULL) delim_mode = 0;
else
{
delim_mode = 1;
for (d = (mio_bch_t*)delim; *d != '\0'; d++)
if (!mio_is_bch_space(*d)) delim_mode = 2;
}
if (delim_mode == 0)
{
/* skip preceding space characters */
while (mio_is_bch_space(*p)) p++;
/* when 0 is given as "delim", it has an effect of cutting
preceding and trailing space characters off "s". */
if (lquote != '\0' && *p == lquote)
{
mio_copy_bcstr_unlimited (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
mio_copy_bcstr_unlimited (p, p + 1);
}
else
{
if (*p == rquote)
{
p++;
break;
}
}
if (sp == 0) sp = p;
ep = p;
p++;
}
while (mio_is_bch_space(*p)) p++;
if (*p != '\0') return -1;
if (sp == 0 && ep == 0) s[0] = '\0';
else
{
ep[1] = '\0';
if (s != (mio_bch_t*)sp) mio_copy_bcstr_unlimited (s, sp);
cnt++;
}
}
else
{
while (*p)
{
if (!mio_is_bch_space(*p))
{
if (sp == 0) sp = p;
ep = p;
}
p++;
}
if (sp == 0 && ep == 0) s[0] = '\0';
else
{
ep[1] = '\0';
if (s != (mio_bch_t*)sp) mio_copy_bcstr_unlimited (s, sp);
cnt++;
}
}
}
else if (delim_mode == 1)
{
mio_bch_t* o;
while (*p)
{
o = p;
while (mio_is_bch_space(*p)) p++;
if (o != p) { mio_copy_bcstr_unlimited (o, p); p = o; }
if (lquote != '\0' && *p == lquote)
{
mio_copy_bcstr_unlimited (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
mio_copy_bcstr_unlimited (p, p + 1);
}
else
{
if (*p == rquote)
{
*p++ = '\0';
cnt++;
break;
}
}
p++;
}
}
else
{
o = p;
for (;;)
{
if (*p == '\0')
{
if (o != p) cnt++;
break;
}
if (mio_is_bch_space(*p))
{
*p++ = '\0';
cnt++;
break;
}
p++;
}
}
}
}
else /* if (delim_mode == 2) */
{
mio_bch_t* o;
int ok;
while (*p != '\0')
{
o = p;
while (mio_is_bch_space(*p)) p++;
if (o != p) { mio_copy_bcstr_unlimited (o, p); p = o; }
if (lquote != '\0' && *p == lquote)
{
mio_copy_bcstr_unlimited (p, p + 1);
for (;;)
{
if (*p == '\0') return -1;
if (escape != '\0' && *p == escape)
{
mio_copy_bcstr_unlimited (p, p + 1);
}
else
{
if (*p == rquote)
{
*p++ = '\0';
cnt++;
break;
}
}
p++;
}
ok = 0;
while (mio_is_bch_space(*p)) p++;
if (*p == '\0') ok = 1;
for (d = (mio_bch_t*)delim; *d != '\0'; d++)
{
if (*p == *d)
{
ok = 1;
mio_copy_bcstr_unlimited (p, p + 1);
break;
}
}
if (ok == 0) return -1;
}
else
{
o = p; sp = ep = 0;
for (;;)
{
if (*p == '\0')
{
if (ep)
{
ep[1] = '\0';
p = &ep[1];
}
cnt++;
break;
}
for (d = (mio_bch_t*)delim; *d != '\0'; d++)
{
if (*p == *d)
{
if (sp == MIO_NULL)
{
mio_copy_bcstr_unlimited (o, p); p = o;
*p++ = '\0';
}
else
{
mio_copy_bcstr_unlimited (&ep[1], p);
mio_copy_bcstr_unlimited (o, sp);
o[ep - sp + 1] = '\0';
p = &o[ep - sp + 2];
}
cnt++;
/* last empty field after delim */
if (*p == '\0') cnt++;
goto exit_point;
}
}
if (!mio_is_bch_space(*p))
{
if (sp == MIO_NULL) sp = p;
ep = p;
}
p++;
}
exit_point:
;
}
}
}
return cnt;
}
/* ========================================================================= */
mio_oow_t mio_byte_to_bcstr (mio_uint8_t byte, mio_bch_t* buf, mio_oow_t size, int flagged_radix, mio_bch_t fill)
{
mio_bch_t tmp[(MIO_SIZEOF(mio_uint8_t) * MIO_BITS_PER_BYTE)];