From 96af6823c1b49a41e873fb4d9ffa16dfce7ffdd9 Mon Sep 17 00:00:00 2001 From: shyouhei Date: Thu, 8 Nov 2018 04:24:26 +0000 Subject: st.c: straight-forward comparison of characters These functions are used in strcasehash, which is used to store encoding names. Encoding names often include hyphens (e.g. "UTF-8"), and ` '-' - 'A' ` is negative (cannot express in unsigned int). Don't be tricky, just do what to do. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65621 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- st.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'st.c') diff --git a/st.c b/st.c index 18b2be7bcf..ee3dbb125d 100644 --- a/st.c +++ b/st.c @@ -2011,18 +2011,18 @@ strhash(st_data_t arg) int st_locale_insensitive_strcasecmp(const char *s1, const char *s2) { - unsigned int c1, c2; + char c1, c2; while (1) { - c1 = (unsigned char)*s1++; - c2 = (unsigned char)*s2++; + c1 = *s1++; + c2 = *s2++; if (c1 == '\0' || c2 == '\0') { if (c1 != '\0') return 1; if (c2 != '\0') return -1; return 0; } - if ((unsigned int)(c1 - 'A') <= ('Z' - 'A')) c1 += 'a' - 'A'; - if ((unsigned int)(c2 - 'A') <= ('Z' - 'A')) c2 += 'a' - 'A'; + if (('A' <= c1) && (c1 <= 'Z')) c1 += 'a' - 'A'; + if (('A' <= c2) && (c2 <= 'Z')) c2 += 'a' - 'A'; if (c1 != c2) { if (c1 > c2) return 1; @@ -2035,18 +2035,18 @@ st_locale_insensitive_strcasecmp(const char *s1, const char *s2) int st_locale_insensitive_strncasecmp(const char *s1, const char *s2, size_t n) { - unsigned int c1, c2; + char c1, c2; while (n--) { - c1 = (unsigned char)*s1++; - c2 = (unsigned char)*s2++; + c1 = *s1++; + c2 = *s2++; if (c1 == '\0' || c2 == '\0') { if (c1 != '\0') return 1; if (c2 != '\0') return -1; return 0; } - if ((unsigned int)(c1 - 'A') <= ('Z' - 'A')) c1 += 'a' - 'A'; - if ((unsigned int)(c2 - 'A') <= ('Z' - 'A')) c2 += 'a' - 'A'; + if (('A' <= c1) && (c1 <= 'Z')) c1 += 'a' - 'A'; + if (('A' <= c2) && (c2 <= 'Z')) c2 += 'a' - 'A'; if (c1 != c2) { if (c1 > c2) return 1; -- cgit v1.2.3