revised moo_compucharsucstr() and similar functions

This commit is contained in:
hyunghwan.chung 2018-01-03 06:56:41 +00:00
parent 5474ae242d
commit 8f69172898
2 changed files with 21 additions and 35 deletions

View File

@ -108,66 +108,52 @@ int moo_compucbcstr (const moo_uch_t* str1, const moo_bch_t* str2)
} }
int moo_compucharsucstr (const moo_uch_t* str1, moo_oow_t len, const moo_uch_t* str2) int moo_compucharsucstr (const moo_uch_t* str1, moo_oow_t len, const moo_uch_t* str2)
{
const moo_uch_t* end = str1 + len;
while (str1 < end && *str2 != '\0' && *str1 == *str2) str1++, str2++;
if (str1 == end)
{
return (*str2 == '\0')? 0: -1;
}
else /* str1 < end */
{ {
/* for "abc\0" of length 4 vs "abc", the fourth character /* for "abc\0" of length 4 vs "abc", the fourth character
* of the first string is equal to the terminating null of * of the first string is equal to the terminating null of
* the second string. the first string is still considered * the second string. the first string is still considered
* bigger */ * bigger */
return (*str1 >= *str2)? 1: -1; const moo_uch_t* end = str1 + len;
while (str1 < end && *str2 != '\0')
{
if (*str1 != *str2) return (*str1 > *str2)? 1: -1;
str1++; str2++;
} }
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
} }
int moo_compucharsbcstr (const moo_uch_t* str1, moo_oow_t len, const moo_bch_t* str2) int moo_compucharsbcstr (const moo_uch_t* str1, moo_oow_t len, const moo_bch_t* str2)
{ {
const moo_uch_t* end = str1 + len; const moo_uch_t* end = str1 + len;
while (str1 < end && *str2 != '\0' && *str1 == *str2) str1++, str2++; while (str1 < end && *str2 != '\0')
if (str1 == end)
{ {
return (*str2 == '\0')? 0: -1; if (*str1 != *str2) return (*str1 > *str2)? 1: -1;
} str1++; str2++;
else /* str1 < end */
{
return (*str1 >= *str2)? 1: -1;
} }
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
} }
int moo_compbcharsbcstr (const moo_bch_t* str1, moo_oow_t len, const moo_bch_t* str2) int moo_compbcharsbcstr (const moo_bch_t* str1, moo_oow_t len, const moo_bch_t* str2)
{ {
const moo_bch_t* end = str1 + len; const moo_bch_t* end = str1 + len;
while (str1 < end && *str2 != '\0' && *str1 == *str2) str1++, str2++; while (str1 < end && *str2 != '\0')
if (str1 == end)
{ {
return (*str2 == '\0')? 0: -1; if (*str1 != *str2) return (*str1 > *str2)? 1: -1;
} str1++; str2++;
else
{
return (*str1 >= *str2)? 1: -1;
} }
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
} }
int moo_compbcharsucstr (const moo_bch_t* str1, moo_oow_t len, const moo_uch_t* str2) int moo_compbcharsucstr (const moo_bch_t* str1, moo_oow_t len, const moo_uch_t* str2)
{ {
const moo_bch_t* end = str1 + len; const moo_bch_t* end = str1 + len;
while (str1 < end && *str2 != '\0' && *str1 == *str2) str1++, str2++; while (str1 < end && *str2 != '\0')
if (str1 == end)
{ {
return (*str2 == '\0')? 0: -1; if (*str1 != *str2) return (*str1 > *str2)? 1: -1;
str1++; str2++;
} }
else return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
{
return (*str1 >= *str2)? 1: -1;
} }
}
void moo_copyuchars (moo_uch_t* dst, const moo_uch_t* src, moo_oow_t len) void moo_copyuchars (moo_uch_t* dst, const moo_uch_t* src, moo_oow_t len)
{ {