Changeset 286062 in webkit
- Timestamp:
- Nov 19, 2021, 7:57:39 AM (5 years ago)
- Location:
- trunk/Source/WTF
- Files:
-
- 2 edited
-
ChangeLog (modified) (1 diff)
-
wtf/JSONValues.cpp (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r286048 r286062 1 2021-11-19 Alex Christensen <achristensen@webkit.org> 2 3 Remove allocation in JSON::Value::parseJSON 4 https://bugs.webkit.org/show_bug.cgi?id=233346 5 6 Reviewed by Yusuke Suzuki. 7 8 Parse the characters as Latin1 characters if we have an 8 bit string rather than converting them to UTF-16 just to be parsed. 9 10 * wtf/JSONValues.cpp: 11 (WTF::JSONImpl::Value::parseJSON): 12 1 13 2021-11-18 Antoine Quint <graouts@webkit.org> 2 14 -
trunk/Source/WTF/wtf/JSONValues.cpp
r285443 r286062 62 62 const char* const falseToken = "false"; 63 63 64 bool parseConstToken(const UChar* start, const UChar* end, const UChar** tokenEnd, const char* token) 64 template<typename CodeUnit> 65 bool parseConstToken(const CodeUnit* start, const CodeUnit* end, const CodeUnit** tokenEnd, const char* token) 65 66 { 66 67 while (start < end && *token != '\0' && *start++ == *token++) { } … … 73 74 } 74 75 75 bool readInt(const UChar* start, const UChar* end, const UChar** tokenEnd, bool canHaveLeadingZeros) 76 template<typename CodeUnit> 77 bool readInt(const CodeUnit* start, const CodeUnit* end, const CodeUnit** tokenEnd, bool canHaveLeadingZeros) 76 78 { 77 79 if (start == end) … … 95 97 } 96 98 97 bool parseNumberToken(const UChar* start, const UChar* end, const UChar** tokenEnd) 99 template<typename CodeUnit> 100 bool parseNumberToken(const CodeUnit* start, const CodeUnit* end, const CodeUnit** tokenEnd) 98 101 { 99 102 // We just grab the number here. We validate the size in DecodeNumber. … … 102 105 return false; 103 106 104 UCharc = *start;107 CodeUnit c = *start; 105 108 if ('-' == c) 106 109 ++start; … … 146 149 } 147 150 148 bool readHexDigits(const UChar* start, const UChar* end, const UChar** tokenEnd, int digits) 151 template<typename CodeUnit> 152 bool readHexDigits(const CodeUnit* start, const CodeUnit* end, const CodeUnit** tokenEnd, int digits) 149 153 { 150 154 if (end - start < digits) … … 160 164 } 161 165 162 bool parseStringToken(const UChar* start, const UChar* end, const UChar** tokenEnd) 166 template<typename CodeUnit> 167 bool parseStringToken(const CodeUnit* start, const CodeUnit* end, const CodeUnit** tokenEnd) 163 168 { 164 169 while (start < end) { 165 UCharc = *start++;170 CodeUnit c = *start++; 166 171 if ('\\' == c && start < end) { 167 172 c = *start++; … … 198 203 } 199 204 200 Token parseToken(const UChar* start, const UChar* end, const UChar** tokenStart, const UChar** tokenEnd) 205 template<typename CodeUnit> 206 Token parseToken(const CodeUnit* start, const CodeUnit* end, const CodeUnit** tokenStart, const CodeUnit** tokenEnd) 201 207 { 202 208 while (start < end && isSpaceOrNewline(*start)) … … 262 268 } 263 269 264 bool decodeString(const UChar* start, const UChar* end, StringBuilder& output) 270 template<typename CodeUnit> 271 bool decodeString(const CodeUnit* start, const CodeUnit* end, StringBuilder& output) 265 272 { 266 273 while (start < end) { … … 317 324 } 318 325 319 bool decodeString(const UChar* start, const UChar* end, String& output) 326 template<typename CodeUnit> 327 bool decodeString(const CodeUnit* start, const CodeUnit* end, String& output) 320 328 { 321 329 if (start == end) { … … 336 344 } 337 345 338 RefPtr<JSON::Value> buildValue(const UChar* start, const UChar* end, const UChar** valueTokenEnd, int depth) 346 template<typename CodeUnit> 347 RefPtr<JSON::Value> buildValue(const CodeUnit* start, const CodeUnit* end, const CodeUnit** valueTokenEnd, int depth) 339 348 { 340 349 if (depth > stackLimit) … … 342 351 343 352 RefPtr<JSON::Value> result; 344 const UChar* tokenStart;345 const UChar* tokenEnd;353 const CodeUnit* tokenStart; 354 const CodeUnit* tokenEnd; 346 355 Token token = parseToken(start, end, &tokenStart, &tokenEnd); 347 356 switch (token) { … … 507 516 RefPtr<Value> Value::parseJSON(const String& json) 508 517 { 509 // FIXME: This whole file should just use StringView instead of UChar/length and avoid upconverting. 510 auto characters = StringView(json).upconvertedCharacters(); 511 const UChar* start = characters; 512 const UChar* end = start + json.length(); 513 const UChar* tokenEnd; 514 auto result = buildValue(start, end, &tokenEnd, 0); 515 if (!result) 516 return nullptr; 517 518 for (const UChar* valueEnd = tokenEnd; valueEnd < end; ++valueEnd) { 519 if (!isSpaceOrNewline(*valueEnd)) 518 auto containsNonSpace = [] (const auto* begin, const auto* end) { 519 if (!begin) 520 return false; 521 for (const auto* it = begin; it < end; it++) { 522 if (!isSpaceOrNewline(*it)) 523 return true; 524 } 525 return false; 526 }; 527 528 RefPtr<Value> result; 529 if (json.is8Bit()) { 530 const LChar* start = json.characters8(); 531 const LChar* end = start + json.length(); 532 const LChar* tokenEnd { nullptr }; 533 result = buildValue(start, end, &tokenEnd, 0); 534 if (containsNonSpace(tokenEnd, end)) 520 535 return nullptr; 521 } 522 536 } else { 537 const UChar* start = json.characters16(); 538 const UChar* end = start + json.length(); 539 const UChar* tokenEnd { nullptr }; 540 result = buildValue(start, end, &tokenEnd, 0); 541 if (containsNonSpace(tokenEnd, end)) 542 return nullptr; 543 } 523 544 return result; 524 545 }
Note:
See TracChangeset
for help on using the changeset viewer.