Skip to content

Commit 025832c

Browse files
byrootnobu
authored andcommitted
[ruby/prism] Use a locale-insensitive version of tolower
[Bug #21161] The `tolower` function provided by the libc is locale dependent and can behave in ways you wouldn't expect for some value of `LC_CTYPE`. ruby/prism@e3488256b4 Co-Authored-By: Nobuyoshi Nakada <[email protected]>
1 parent d4b8da6 commit 025832c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

prism/util/pm_strncasecmp.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#include "prism/util/pm_strncasecmp.h"
22

3+
/**
4+
* A locale-insensitive version of `tolower(3)`
5+
*/
6+
static inline int
7+
pm_tolower(int c)
8+
{
9+
if ('A' <= c && c <= 'Z') {
10+
return c | 0x20;
11+
}
12+
return c;
13+
}
14+
315
/**
416
* Compare two strings, ignoring case, up to the given length. Returns 0 if the
517
* strings are equal, a negative number if string1 is less than string2, or a
@@ -16,7 +28,7 @@ pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length) {
1628

1729
while (offset < length && string1[offset] != '\0') {
1830
if (string2[offset] == '\0') return string1[offset];
19-
if ((difference = tolower(string1[offset]) - tolower(string2[offset])) != 0) return difference;
31+
if ((difference = pm_tolower(string1[offset]) - pm_tolower(string2[offset])) != 0) return difference;
2032
offset++;
2133
}
2234

0 commit comments

Comments
 (0)