added hio_rotate_uchars() and hio_rotate_bchars()

This commit is contained in:
hyung-hwan 2022-01-04 14:21:14 +00:00
parent 2e9fa561a8
commit 0882d0eda2
5 changed files with 138 additions and 0 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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;

View File

@ -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 --

View File

@ -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)
{