From 31ea41a89559dcd5b67ca15ca5c2b632b9567caf Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Mon, 8 Jan 2018 03:33:24 +0000 Subject: [PATCH] fixed bugs in moo_compXXX() functions added moo_compbchars() and moo_compuchars() --- moo/lib/moo-cmn.h | 2 ++ moo/lib/moo-utl.h | 14 +++++++++++ moo/lib/utl.c | 64 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/moo/lib/moo-cmn.h b/moo/lib/moo-cmn.h index 25a364e..ce6e3d4 100644 --- a/moo/lib/moo-cmn.h +++ b/moo/lib/moo-cmn.h @@ -276,11 +276,13 @@ typedef char moo_bch_t; typedef int moo_bci_t; +typedef unsigned char moo_bchu_t; /* unsigned version of moo_bch_t for inner working */ #define MOO_SIZEOF_BCH_T MOO_SIZEOF_CHAR #define MOO_SIZEOF_BCI_T MOO_SIZEOF_INT typedef moo_uint16_t moo_uch_t; /* TODO ... wchar_t??? */ typedef moo_int32_t moo_uci_t; +typedef moo_uint16_t moo_uchu_t; /* same as moo_uch_t as it is already unsigned */ #define MOO_SIZEOF_UCH_T 2 #define MOO_SIZEOF_UCI_T 4 diff --git a/moo/lib/moo-utl.h b/moo/lib/moo-utl.h index d69aede..9b53f42 100644 --- a/moo/lib/moo-utl.h +++ b/moo/lib/moo-utl.h @@ -147,6 +147,20 @@ MOO_EXPORT int moo_equalbchars ( moo_oow_t len ); +MOO_EXPORT int moo_compuchars ( + const moo_uch_t* str1, + moo_oow_t len1, + const moo_uch_t* str2, + moo_oow_t len2 +); + +MOO_EXPORT int moo_compbchars ( + const moo_bch_t* str1, + moo_oow_t len1, + const moo_bch_t* str2, + moo_oow_t len2 +); + MOO_EXPORT int moo_compucstr ( const moo_uch_t* str1, const moo_uch_t* str2 diff --git a/moo/lib/utl.c b/moo/lib/utl.c index ce01fd8..f2b2fe5 100644 --- a/moo/lib/utl.c +++ b/moo/lib/utl.c @@ -54,6 +54,9 @@ int moo_equaluchars (const moo_uch_t* str1, const moo_uch_t* str2, moo_oow_t len { moo_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; @@ -66,6 +69,9 @@ int moo_equalbchars (const moo_bch_t* str1, const moo_bch_t* str2, moo_oow_t len { moo_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; @@ -74,6 +80,50 @@ int moo_equalbchars (const moo_bch_t* str1, const moo_bch_t* str2, moo_oow_t len return 1; } +int moo_compuchars (const moo_uch_t* str1, moo_oow_t len1, const moo_uch_t* str2, moo_oow_t len2) +{ + moo_uchu_t c1, c2; + const moo_uch_t* end1 = str1 + len1; + const moo_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 moo_compbchars (const moo_bch_t* str1, moo_oow_t len1, const moo_bch_t* str2, moo_oow_t len2) +{ + moo_bchu_t c1, c2; + const moo_bch_t* end1 = str1 + len1; + const moo_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 moo_compucstr (const moo_uch_t* str1, const moo_uch_t* str2) { while (*str1 == *str2) @@ -82,7 +132,7 @@ int moo_compucstr (const moo_uch_t* str1, const moo_uch_t* str2) str1++, str2++; } - return (*str1 > *str2)? 1: -1; + return ((moo_uchu_t)*str1 > (moo_uchu_t)*str2)? 1: -1; } int moo_compbcstr (const moo_bch_t* str1, const moo_bch_t* str2) @@ -93,7 +143,7 @@ int moo_compbcstr (const moo_bch_t* str1, const moo_bch_t* str2) str1++, str2++; } - return (*str1 > *str2)? 1: -1; + return ((moo_bchu_t)*str1 > (moo_bchu_t)*str2)? 1: -1; } int moo_compucbcstr (const moo_uch_t* str1, const moo_bch_t* str2) @@ -104,7 +154,7 @@ int moo_compucbcstr (const moo_uch_t* str1, const moo_bch_t* str2) str1++, str2++; } - return (*str1 > *str2)? 1: -1; + return ((moo_uchu_t)*str1 > (moo_bchu_t)*str2)? 1: -1; } int moo_compucharsucstr (const moo_uch_t* str1, moo_oow_t len, const moo_uch_t* str2) @@ -116,7 +166,7 @@ int moo_compucharsucstr (const moo_uch_t* str1, moo_oow_t len, const moo_uch_t* const moo_uch_t* end = str1 + len; while (str1 < end && *str2 != '\0') { - if (*str1 != *str2) return (*str1 > *str2)? 1: -1; + if (*str1 != *str2) return ((moo_uchu_t)*str1 > (moo_uchu_t)*str2)? 1: -1; str1++; str2++; } return (str1 < end)? 1: (*str2 == '\0'? 0: -1); @@ -127,7 +177,7 @@ int moo_compucharsbcstr (const moo_uch_t* str1, moo_oow_t len, const moo_bch_t* const moo_uch_t* end = str1 + len; while (str1 < end && *str2 != '\0') { - if (*str1 != *str2) return (*str1 > *str2)? 1: -1; + if (*str1 != *str2) return ((moo_uchu_t)*str1 > (moo_bchu_t)*str2)? 1: -1; str1++; str2++; } return (str1 < end)? 1: (*str2 == '\0'? 0: -1); @@ -138,7 +188,7 @@ int moo_compbcharsbcstr (const moo_bch_t* str1, moo_oow_t len, const moo_bch_t* const moo_bch_t* end = str1 + len; while (str1 < end && *str2 != '\0') { - if (*str1 != *str2) return (*str1 > *str2)? 1: -1; + if (*str1 != *str2) return ((moo_bchu_t)*str1 > (moo_bchu_t)*str2)? 1: -1; str1++; str2++; } return (str1 < end)? 1: (*str2 == '\0'? 0: -1); @@ -149,7 +199,7 @@ int moo_compbcharsucstr (const moo_bch_t* str1, moo_oow_t len, const moo_uch_t* const moo_bch_t* end = str1 + len; while (str1 < end && *str2 != '\0') { - if (*str1 != *str2) return (*str1 > *str2)? 1: -1; + if (*str1 != *str2) return ((moo_bchu_t)*str1 > (moo_uchu_t)*str2)? 1: -1; str1++; str2++; } return (str1 < end)? 1: (*str2 == '\0'? 0: -1);