File tree Expand file tree Collapse file tree 3 files changed +289
-3
lines changed
Expand file tree Collapse file tree 3 files changed +289
-3
lines changed Original file line number Diff line number Diff line change @@ -156,13 +156,20 @@ std::string escape_non_alnum(const std::string &to_escape)
156156 std::ostringstream escaped;
157157 for (auto &ch : to_escape)
158158 {
159+ // `ch` may have a negative value in the case of utf-8 encodings of
160+ // characters above unicode code point 127. The following line maps these
161+ // negative values to positive values in the 128-255 range, using a
162+ // `static_cast`. This is neccessary in order to avoid undefined behaviour
163+ // in `isalnum`. The positive values are then stored in an integer using a
164+ // widening initialisation so that the stream insertion operator prints them
165+ // as numbers rather than characters.
166+ const int uch{static_cast <unsigned char >(ch)};
159167 if (ch == ' _' )
160168 escaped << " __" ;
161- else if (isalnum (ch ))
169+ else if (isalnum (uch ))
162170 escaped << ch;
163171 else
164- escaped << ' _' << std::hex << std::setfill (' 0' ) << std::setw (2 )
165- << (unsigned int )ch;
172+ escaped << ' _' << std::hex << std::setfill (' 0' ) << std::setw (2 ) << uch;
166173 }
167174 return escaped.str ();
168175}
Original file line number Diff line number Diff line change @@ -119,6 +119,7 @@ SRC += analyses/ai/ai.cpp \
119119 util/string2int.cpp \
120120 util/structured_data.cpp \
121121 util/string_utils/capitalize.cpp \
122+ util/string_utils/escape_non_alnum.cpp \
122123 util/string_utils/join_string.cpp \
123124 util/string_utils/split_string.cpp \
124125 util/string_utils/strip_string.cpp \
You can’t perform that action at this time.
0 commit comments