| 203 | } |
| 204 | |
| 205 | bool JSONReader::decodeString(std::string_view str, std::string& decoded) { |
| 206 | decoded.reserve(str.size()); |
| 207 | const auto* current = str.data(); |
| 208 | const auto* end = str.data() + str.size(); |
| 209 | while (current != end) { |
| 210 | auto c = *current++; |
| 211 | if (c == '"') |
| 212 | break; |
| 213 | if (c == '\\') { |
| 214 | if (current == end) { |
| 215 | setErrorAtCurrentPosition("Empty escape sequence in string"); |
| 216 | return false; |
| 217 | } |
| 218 | auto escape = *current++; |
| 219 | switch (escape) { |
| 220 | case '"': |
| 221 | decoded += '"'; |
| 222 | break; |
| 223 | case '/': |
| 224 | decoded += '/'; |
| 225 | break; |
| 226 | case '\\': |
| 227 | decoded += '\\'; |
| 228 | break; |
| 229 | case 'b': |
| 230 | decoded += '\b'; |
| 231 | break; |
| 232 | case 'f': |
| 233 | decoded += '\f'; |
| 234 | break; |
| 235 | case 'n': |
| 236 | decoded += '\n'; |
| 237 | break; |
| 238 | case 'r': |
| 239 | decoded += '\r'; |
| 240 | break; |
| 241 | case 't': |
| 242 | decoded += '\t'; |
| 243 | break; |
| 244 | case 'u': { |
| 245 | unsigned int unicode; |
| 246 | if (!decodeUnicodeCodePoint(current, end, unicode)) |
| 247 | return false; |
| 248 | decoded += codePointToUTF8(unicode); |
| 249 | } break; |
| 250 | default: |
| 251 | setErrorAtCurrentPosition("Bad escape sequence in string"); |
| 252 | return false; |
| 253 | } |
| 254 | } else { |
| 255 | decoded += c; |
| 256 | } |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | // Taken from the jsoncpp library |
| 262 | bool JSONReader::decodeUnicodeCodePoint(const char*& current, const char* end, unsigned int& unicode) { |
nothing calls this directly
no test coverage detected