From 0882d0eda2ba7dbacdd2ce4ae0a4f212a98d379f Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 4 Jan 2022 14:21:14 +0000 Subject: [PATCH] added hio_rotate_uchars() and hio_rotate_bchars() --- hio/lib/hio-utl.h | 15 ++++++++++ hio/lib/hio-utl.h.m4 | 15 ++++++++++ hio/lib/utl-str.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ hio/lib/utl-str.c.m4 | 3 ++ hio/lib/utl-str.m4 | 37 ++++++++++++++++++++++++ 5 files changed, 138 insertions(+) diff --git a/hio/lib/hio-utl.h b/hio/lib/hio-utl.h index dddee11..10146f3 100644 --- a/hio/lib/hio-utl.h +++ b/hio/lib/hio-utl.h @@ -511,6 +511,21 @@ HIO_EXPORT hio_bch_t* hio_find_bchar_in_bcstr ( hio_bch_t c ); +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, diff --git a/hio/lib/hio-utl.h.m4 b/hio/lib/hio-utl.h.m4 index 363bc1d..6ab7bbc 100644 --- a/hio/lib/hio-utl.h.m4 +++ b/hio/lib/hio-utl.h.m4 @@ -511,6 +511,21 @@ HIO_EXPORT hio_bch_t* hio_find_bchar_in_bcstr ( hio_bch_t c ); +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, diff --git a/hio/lib/utl-str.c b/hio/lib/utl-str.c index 55afb0d..61f6be7 100644 --- a/hio/lib/utl-str.c +++ b/hio/lib/utl-str.c @@ -437,6 +437,74 @@ hio_bch_t* hio_find_bchar_in_bcstr (const hio_bch_t* ptr, hio_bch_t c) return HIO_NULL; } +hio_oow_t hio_rotate_uchars (hio_uch_t* str, hio_oow_t len, int dir, hio_oow_t n) +{ + hio_oow_t first, last, count, index, nk; + hio_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; +} + +hio_oow_t hio_rotate_bchars (hio_bch_t* str, hio_oow_t len, int dir, hio_oow_t n) +{ + hio_oow_t first, last, count, index, nk; + hio_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; +} + hio_uch_t* hio_trim_uchars (const hio_uch_t* str, hio_oow_t* len, int flags) { const hio_uch_t* p = str, * end = str + *len; diff --git a/hio/lib/utl-str.c.m4 b/hio/lib/utl-str.c.m4 index 25ed1a6..b23d7f2 100644 --- a/hio/lib/utl-str.c.m4 +++ b/hio/lib/utl-str.c.m4 @@ -69,6 +69,9 @@ dnl -- fn_find_char_in_cstr(hio_find_uchar_in_ucstr, hio_uch_t) fn_find_char_in_cstr(hio_find_bchar_in_bcstr, hio_bch_t) dnl -- +fn_rotate_chars(hio_rotate_uchars, hio_uch_t) +fn_rotate_chars(hio_rotate_bchars, hio_bch_t) +dnl -- fn_trim_chars(hio_trim_uchars, hio_uch_t, hio_is_uch_space, HIO_TRIM_UCHARS) fn_trim_chars(hio_trim_bchars, hio_bch_t, hio_is_bch_space, HIO_TRIM_BCHARS) dnl -- diff --git a/hio/lib/utl-str.m4 b/hio/lib/utl-str.m4 index af333a7..abf2403 100644 --- a/hio/lib/utl-str.m4 +++ b/hio/lib/utl-str.m4 @@ -237,6 +237,43 @@ _char_type_* _fn_name_ (const _char_type_* ptr, _char_type_ c) popdef([[_fn_name_]])popdef([[_char_type_]])dnl ]])dnl dnl --------------------------------------------------------------------------- +define([[fn_rotate_chars]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)dnl +hio_oow_t _fn_name_ (_char_type_* str, hio_oow_t len, int dir, hio_oow_t n) +{ + hio_oow_t first, last, count, index, nk; + _char_type_ 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; +} +popdef([[_fn_name_]])popdef([[_char_type_]])dnl +]])dnl +dnl --------------------------------------------------------------------------- define([[fn_trim_chars]], [[pushdef([[_fn_name_]], $1)pushdef([[_char_type_]], $2)pushdef([[_is_space_]], $3)pushdef([[_prefix_]], $4)dnl _char_type_* _fn_name_ (const _char_type_* str, hio_oow_t* len, int flags) {