diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-string.c | 46 | ||||
-rw-r--r-- | src/core/wee-utf8.c | 46 | ||||
-rw-r--r-- | src/core/wee-utf8.h | 1 |
3 files changed, 62 insertions, 31 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 8d172767b..80844ceff 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -81,7 +81,7 @@ string_tolower (char *string) { if ((string[0] >= 'A') && (string[0] <= 'Z')) string[0] += ('a' - 'A'); - string++; + string = utf8_next_char (string); } } @@ -96,7 +96,7 @@ string_toupper (char *string) { if ((string[0] >= 'a') && (string[0] <= 'z')) string[0] -= ('a' - 'A'); - string++; + string = utf8_next_char (string); } } @@ -107,27 +107,19 @@ string_toupper (char *string) int string_strcasecmp (char *string1, char *string2) { - int c1, c2; + int diff; if (!string1 || !string2) return (string1) ? 1 : ((string2) ? -1 : 0); while (string1[0] && string2[0]) { - c1 = (int)((unsigned char) string1[0]); - c2 = (int)((unsigned char) string2[0]); - - if ((c1 >= 'A') && (c1 <= 'Z')) - c1 += ('a' - 'A'); - - if ((c2 >= 'A') && (c2 <= 'Z')) - c2 += ('a' - 'A'); - - if ((c1 - c2) != 0) - return c1 - c2; - - string1++; - string2++; + diff = utf8_charcasecmp (string1, string2); + if (diff != 0) + return diff; + + string1 = utf8_next_char (string1); + string2 = utf8_next_char (string2); } return (string1[0]) ? 1 : ((string2[0]) ? -1 : 0); @@ -141,7 +133,7 @@ string_strcasecmp (char *string1, char *string2) int string_strncasecmp (char *string1, char *string2, int max) { - int c1, c2, count; + int count, diff; if (!string1 || !string2) return (string1) ? 1 : ((string2) ? -1 : 0); @@ -149,20 +141,12 @@ string_strncasecmp (char *string1, char *string2, int max) count = 0; while ((count < max) && string1[0] && string2[0]) { - c1 = (int)((unsigned char) string1[0]); - c2 = (int)((unsigned char) string2[0]); - - if ((c1 >= 'A') && (c1 <= 'Z')) - c1 += ('a' - 'A'); - - if ((c2 >= 'A') && (c2 <= 'Z')) - c2 += ('a' - 'A'); - - if ((c1 - c2) != 0) - return c1 - c2; + diff = utf8_charcasecmp (string1, string2); + if (diff != 0) + return diff; - string1++; - string2++; + string1 = utf8_next_char (string1); + string2 = utf8_next_char (string2); count++; } diff --git a/src/core/wee-utf8.c b/src/core/wee-utf8.c index c7b8596d3..085e6e6dc 100644 --- a/src/core/wee-utf8.c +++ b/src/core/wee-utf8.c @@ -316,6 +316,52 @@ utf8_strlen_screen (char *string) } /* + * utf8_charcasecmp: compare two utf8 chars (case is ignored) + */ + +int +utf8_charcasecmp (char *string1, char *string2) +{ + int length1, length2, i, char1, char2, diff; + + if (!string1 || !string2) + return (string1) ? 1 : ((string2) ? -1 : 0); + + length1 = utf8_char_size (string1); + length2 = utf8_char_size (string2); + + char1 = (int)((unsigned char) string1[0]); + char2 = (int)((unsigned char) string2[0]); + + if ((char1 >= 'A') && (char1 <= 'Z')) + char1 += ('a' - 'A'); + + if ((char2 >= 'A') && (char2 <= 'Z')) + char2 += ('a' - 'A'); + + diff = char1 - char2; + if (diff != 0) + return diff; + + i = 1; + while ((i < length1) && (i < length2)) + { + diff = (int)((unsigned char) string1[0]) - (int)((unsigned char) string2[0]); + if (diff != 0) + return diff; + i++; + } + // string1 == string2 ? + if ((i == length1) && (i == length2)) + return 0; + // string1 < string2 ? + if (i == length1) + return 1; + // string1 > string2 + return -1; +} + +/* * utf8_char_size_screen: return number of chars needed on screen to display * UTF-8 char */ diff --git a/src/core/wee-utf8.h b/src/core/wee-utf8.h index af11e2c67..b66996864 100644 --- a/src/core/wee-utf8.h +++ b/src/core/wee-utf8.h @@ -42,6 +42,7 @@ extern int utf8_char_size (char *); extern int utf8_strlen (char *); extern int utf8_strnlen (char *, int); extern int utf8_strlen_screen (char *); +extern int utf8_charcasecmp (char *, char *); extern int utf8_char_size_screen (char *); extern char *utf8_add_offset (char *, int); extern int utf8_real_pos (char *, int); |