diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-string.c | 118 | ||||
-rw-r--r-- | src/core/wee-utf8.c | 74 |
2 files changed, 80 insertions, 112 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 4bf43094e..3c258ec94 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -394,12 +394,14 @@ string_toupper (const char *string) } /* - * Compares two strings (locale and case independent). + * Compares two strings (case insensitive). * - * Returns: - * -1: string1 < string2 - * 0: string1 == string2 - * 1: string1 > string2 + * Returns: arithmetic result of subtracting the last compared char in string2 + * (converted to lowercase) from the last compared char in string1 (converted + * to lowercase): + * < 0: string1 < string2 + * 0: string1 == string2 + * > 0: string1 > string2 */ int @@ -407,24 +409,21 @@ string_strcasecmp (const char *string1, const char *string2) { int diff; - if (!string1 || !string2) - return (string1) ? 1 : ((string2) ? -1 : 0); - - while (string1[0] && string2[0]) + while (string1 && string1[0] && string2 && string2[0]) { diff = utf8_charcasecmp (string1, string2); if (diff != 0) - return (diff < 0) ? -1 : 1; + return diff; string1 = utf8_next_char (string1); string2 = utf8_next_char (string2); } - return (string1[0]) ? 1 : ((string2[0]) ? -1 : 0); + return utf8_charcasecmp (string1, string2); } /* - * Compares two strings (locale and case independent) using a range. + * Compares two strings (case insensitive using a range). * * The range is the number of chars which can be converted from upper to lower * case. For example 26 = all letters of alphabet, 29 = all letters + 3 chars. @@ -435,10 +434,12 @@ string_strcasecmp (const char *string1, const char *string2) * - range = 30: A-Z [ \ ] ^ ==> a-z { | } ~ * (ranges 29 and 30 are used by some protocols like IRC) * - * Returns: - * -1: string1 < string2 - * 0: string1 == string2 - * 1: string1 > string2 + * Returns: arithmetic result of subtracting the last compared char in string2 + * (converted to lowercase) from the last compared char in string1 (converted + * to lowercase): + * < 0: string1 < string2 + * 0: string1 == string2 + * > 0: string1 > string2 */ int @@ -446,29 +447,28 @@ string_strcasecmp_range (const char *string1, const char *string2, int range) { int diff; - if (!string1 || !string2) - return (string1) ? 1 : ((string2) ? -1 : 0); - - while (string1[0] && string2[0]) + while (string1 && string1[0] && string2 && string2[0]) { diff = utf8_charcasecmp_range (string1, string2, range); if (diff != 0) - return (diff < 0) ? -1 : 1; + return diff; string1 = utf8_next_char (string1); string2 = utf8_next_char (string2); } - return (string1[0]) ? 1 : ((string2[0]) ? -1 : 0); + return utf8_charcasecmp_range (string1, string2, range); } /* - * Compares two strings with max length (locale and case independent). + * Compares two strings with max length (case insensitive). * - * Returns: - * -1: string1 < string2 + * Returns: arithmetic result of subtracting the last compared char in string2 + * (converted to lowercase) from the last compared char in string1 (converted + * to lowercase): + * < 0: string1 < string2 * 0: string1 == string2 - * 1: string1 > string2 + * > 0: string1 > string2 */ int @@ -476,15 +476,12 @@ string_strncasecmp (const char *string1, const char *string2, int max) { int count, diff; - if (!string1 || !string2) - return (string1) ? 1 : ((string2) ? -1 : 0); - count = 0; - while ((count < max) && string1[0] && string2[0]) + while ((count < max) && string1 && string1[0] && string2 && string2[0]) { diff = utf8_charcasecmp (string1, string2); if (diff != 0) - return (diff < 0) ? -1 : 1; + return diff; string1 = utf8_next_char (string1); string2 = utf8_next_char (string2); @@ -494,12 +491,11 @@ string_strncasecmp (const char *string1, const char *string2, int max) if (count >= max) return 0; else - return (string1[0]) ? 1 : ((string2[0]) ? -1 : 0); + return utf8_charcasecmp (string1, string2); } /* - * Compares two strings with max length (locale and case independent) using a - * range. + * Compares two strings with max length (case insensitive using a range). * * The range is the number of chars which can be converted from upper to lower * case. For example 26 = all letters of alphabet, 29 = all letters + 3 chars. @@ -510,10 +506,12 @@ string_strncasecmp (const char *string1, const char *string2, int max) * - range = 30: A-Z [ \ ] ^ ==> a-z { | } ~ * (ranges 29 and 30 are used by some protocols like IRC) * - * Returns: - * -1: string1 < string2 - * 0: string1 == string2 - * 1: string1 > string2 + * Returns: arithmetic result of subtracting the last compared char in string2 + * (converted to lowercase) from the last compared char in string1 (converted + * to lowercase): + * < 0: string1 < string2 + * 0: string1 == string2 + * > 0: string1 > string2 */ int @@ -522,15 +520,12 @@ string_strncasecmp_range (const char *string1, const char *string2, int max, { int count, diff; - if (!string1 || !string2) - return (string1) ? 1 : ((string2) ? -1 : 0); - count = 0; - while ((count < max) && string1[0] && string2[0]) + while ((count < max) && string1 && string1[0] && string2 && string2[0]) { diff = utf8_charcasecmp_range (string1, string2, range); if (diff != 0) - return (diff < 0) ? -1 : 1; + return diff; string1 = utf8_next_char (string1); string2 = utf8_next_char (string2); @@ -540,16 +535,19 @@ string_strncasecmp_range (const char *string1, const char *string2, int max, if (count >= max) return 0; else - return (string1[0]) ? 1 : ((string2[0]) ? -1 : 0); + return utf8_charcasecmp_range (string1, string2, range); } /* * Compares two strings, ignoring some chars. * - * Returns: - * -1: string1 < string2 - * 0: string1 == string2 - * 1: string1 > string2 + * Returns: arithmetic result of subtracting the last compared char in string2 + * (converted to lowercase if case_sensitive is set to 0) from the last + * compared char in string1 (converted to lowercase if case_sensitive is set + * to 0): + * < 0: string1 < string2 + * 0: string1 == string2 + * > 0: string1 > string2 */ int @@ -558,9 +556,6 @@ string_strcmp_ignore_chars (const char *string1, const char *string2, { int diff; - if (!string1 || !string2) - return (string1) ? 1 : ((string2) ? -1 : 0); - while (string1 && string1[0] && string2 && string2[0]) { /* skip ignored chars */ @@ -574,18 +569,18 @@ string_strcmp_ignore_chars (const char *string1, const char *string2, } /* end of one (or both) string(s) ? */ - if ((!string1 || !string1[0]) && (!string2 || !string2[0])) - return 0; - if ((!string1 || !string1[0]) && string2 && string2[0]) - return -1; - if (string1 && string1[0] && (!string2 || !string2[0])) - return 1; + if (!string1 || !string1[0] || !string2 || !string2[0]) + { + return (case_sensitive) ? + utf8_charcmp (string1, string2) : + utf8_charcasecmp (string1, string2); + } /* look at diff */ diff = (case_sensitive) ? utf8_charcmp (string1, string2) : utf8_charcasecmp (string1, string2); if (diff != 0) - return (diff < 0) ? -1 : 1; + return diff; string1 = utf8_next_char (string1); string2 = utf8_next_char (string2); @@ -600,11 +595,8 @@ string_strcmp_ignore_chars (const char *string1, const char *string2, string2 = utf8_next_char (string2); } } - if ((!string1 || !string1[0]) && string2 && string2[0]) - return -1; - if (string1 && string1[0] && (!string2 || !string2[0])) - return 1; - return 0; + return (case_sensitive) ? + utf8_charcmp (string1, string2) : utf8_charcasecmp (string1, string2); } /* diff --git a/src/core/wee-utf8.c b/src/core/wee-utf8.c index 8f6a69cff..557fc592d 100644 --- a/src/core/wee-utf8.c +++ b/src/core/wee-utf8.c @@ -531,48 +531,28 @@ utf8_strlen_screen (const char *string) /* * Compares two UTF-8 chars (case sensitive). * - * Returns: - * -1: string1 < string2 - * 0: string1 == string2 - * 1: string1 > string2 + * Returns: arithmetic result of subtracting the first char in string2 + * from the first char in string1: + * < 0: string1 < string2 + * 0: string1 == string2 + * > 0: string1 > string2 */ int utf8_charcmp (const char *string1, const char *string2) { - int length1, length2, i, diff; - - if (!string1 || !string2) - return (string1) ? 1 : ((string2) ? -1 : 0); - - length1 = utf8_char_size (string1); - length2 = utf8_char_size (string2); - - i = 0; - while ((i < length1) && (i < length2)) - { - diff = (int)((unsigned char) string1[i]) - (int)((unsigned char) string2[i]); - if (diff != 0) - return (diff < 0) ? -1 : 1; - i++; - } - /* string1 == string2 ? */ - if ((i == length1) && (i == length2)) - return 0; - /* string1 < string2 ? */ - if (i == length1) - return 1; - /* string1 > string2 */ - return -1; + return utf8_char_int (string1) - utf8_char_int (string2); } /* - * Compares two UTF-8 chars (case is ignored). - * - * Returns: - * -1: string1 < string2 - * 0: string1 == string2 - * 1: string1 > string2 + * Compares two UTF-8 chars (case insensitive). + * + * Returns: arithmetic result of subtracting the first char in string2 + * (converted to lowercase) from the first char in string1 (converted + * to lowercase): + * < 0: string1 < string2 + * 0: string1 == string2 + * > 0: string1 > string2 */ int @@ -580,15 +560,12 @@ utf8_charcasecmp (const char *string1, const char *string2) { wint_t wchar1, wchar2; - if (!string1 || !string2) - return (string1) ? 1 : ((string2) ? -1 : 0); - /* * optimization for single-byte chars: only letters A-Z must be converted * to lowercase; this is faster than calling `towlower` */ - if (!((unsigned char)(string1[0]) & 0x80) - && !((unsigned char)(string2[0]) & 0x80)) + if (string1 && !((unsigned char)(string1[0]) & 0x80) + && string2 && !((unsigned char)(string2[0]) & 0x80)) { wchar1 = string1[0]; if ((wchar1 >= 'A') && (wchar1 <= 'Z')) @@ -603,11 +580,11 @@ utf8_charcasecmp (const char *string1, const char *string2) wchar2 = towlower (utf8_char_int (string2)); } - return (wchar1 < wchar2) ? -1 : ((wchar1 == wchar2) ? 0 : 1); + return wchar1 - wchar2; } /* - * Compares two UTF-8 chars (case is ignored) using a range. + * Compares two UTF-8 chars (case insensitive using a range). * * The range is the number of chars which can be converted from upper to lower * case. For example 26 = all letters of alphabet, 29 = all letters + 3 chars. @@ -618,10 +595,12 @@ utf8_charcasecmp (const char *string1, const char *string2) * - range = 30: A-Z [ \ ] ^ ==> a-z { | } ~ * (ranges 29 and 30 are used by some protocols like IRC) * - * Returns: - * < 0: char1 < char2 - * 0: char1 == char2 - * > 0: char1 > char2 + * Returns: arithmetic result of subtracting the last compared char in string2 + * (converted to lowercase) from the last compared char in string1 (converted + * to lowercase): + * < 0: string1 < string2 + * 0: string1 == string2 + * > 0: string1 > string2 */ int @@ -629,9 +608,6 @@ utf8_charcasecmp_range (const char *string1, const char *string2, int range) { wchar_t wchar1, wchar2; - if (!string1 || !string2) - return (string1) ? 1 : ((string2) ? -1 : 0); - wchar1 = utf8_char_int (string1); if ((wchar1 >= (wchar_t)'A') && (wchar1 < (wchar_t)('A' + range))) wchar1 += ('a' - 'A'); @@ -640,7 +616,7 @@ utf8_charcasecmp_range (const char *string1, const char *string2, int range) if ((wchar2 >= (wchar_t)'A') && (wchar2 < (wchar_t)('A' + range))) wchar2 += ('a' - 'A'); - return (wchar1 < wchar2) ? -1 : ((wchar1 == wchar2) ? 0 : 1); + return wchar1 - wchar2; } /* |