File tree 3 files changed +10
-4
lines changed 3 files changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -313,6 +313,7 @@ class object : protected internal::tape_ref {
313
313
314
314
/* *
315
315
* Get the value associated with the given key in a case-insensitive manner.
316
+ * It is only guaranteed to work over ASCII inputs.
316
317
*
317
318
* Note: The key will be matched against **unescaped** JSON.
318
319
*
Original file line number Diff line number Diff line change @@ -686,10 +686,10 @@ inline simdjson_result<element> object::at_key_case_insensitive(const std::strin
686
686
for (iterator field = begin (); field != end_field; ++field) {
687
687
auto field_key = field.key ();
688
688
if (key.length () == field_key.length ()) {
689
- bool equal = true ;
690
- for ( size_t i= 0 ; i<field_key. length (); i++) {
691
- equal = equal && std::tolower (key[i]) == std::tolower (field_key[i]);
692
- }
689
+ // See For case-insensitive string comparisons, avoid char-by-char functions
690
+ // https://lemire.me/blog/2020/04/30/for-case-insensitive-string-comparisons-avoid-char-by-char-functions/
691
+ // Note that it might be worth rolling our own strncasecmp function, with vectorization.
692
+ const bool equal = ( simdjson_strncasecmp (key. data (), field_key. data (), key. length ()) == 0 );
693
693
if (equal) { return field.value (); }
694
694
}
695
695
}
Original file line number Diff line number Diff line change @@ -147,8 +147,13 @@ compiling for a known 64-bit platform."
147
147
// regular visual studio and clang under visual studio.
148
148
// clang under Windows has _stricmp (like visual studio) but not strcasecmp (as clang normally has)
149
149
#define simdjson_strcasecmp _stricmp
150
+ #define simdjson_strncasecmp _strnicmp
150
151
#else
152
+ // The strcasecmp, strncasecmp, and strcasestr functions do not work with multibyte strings (e.g. UTF-8).
153
+ // So they are only useful for ASCII in our context.
154
+ // https://www.gnu.org/software/libunistring/manual/libunistring.html#char-_002a-strings
151
155
#define simdjson_strcasecmp strcasecmp
156
+ #define simdjson_strncasecmp strncasecmp
152
157
#endif
153
158
154
159
namespace simdjson {
You can’t perform that action at this time.
0 commit comments