ruby-changes:53405
From: shyouhei <ko1@a...>
Date: Thu, 8 Nov 2018 13:24:31 +0900 (JST)
Subject: [ruby-changes:53405] shyouhei:r65621 (trunk): st.c: straight-forward comparison of characters
shyouhei 2018-11-08 13:24:26 +0900 (Thu, 08 Nov 2018) New Revision: 65621 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65621 Log: 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. Modified files: trunk/st.c Index: st.c =================================================================== --- st.c (revision 65620) +++ st.c (revision 65621) @@ -2011,18 +2011,18 @@ strhash(st_data_t arg) https://github.com/ruby/ruby/blob/trunk/st.c#L2011 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 c https://github.com/ruby/ruby/blob/trunk/st.c#L2035 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; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/