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)
156
156
std::ostringstream escaped;
157
157
for (auto &ch : to_escape)
158
158
{
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)};
159
167
if (ch == ' _' )
160
168
escaped << " __" ;
161
- else if (isalnum (ch ))
169
+ else if (isalnum (uch ))
162
170
escaped << ch;
163
171
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;
166
173
}
167
174
return escaped.str ();
168
175
}
Original file line number Diff line number Diff line change @@ -119,6 +119,7 @@ SRC += analyses/ai/ai.cpp \
119
119
util/string2int.cpp \
120
120
util/structured_data.cpp \
121
121
util/string_utils/capitalize.cpp \
122
+ util/string_utils/escape_non_alnum.cpp \
122
123
util/string_utils/join_string.cpp \
123
124
util/string_utils/split_string.cpp \
124
125
util/string_utils/strip_string.cpp \
You can’t perform that action at this time.
0 commit comments