Index: runtime/platform/json.cc |
=================================================================== |
--- runtime/platform/json.cc (revision 26654) |
+++ runtime/platform/json.cc (working copy) |
@@ -398,7 +398,7 @@ |
} |
-void JSONReader::GetValueChars(char* buf, intptr_t buflen) const { |
+void JSONReader::GetRawValueChars(char* buf, intptr_t buflen) const { |
if (Type() == kNone) { |
return; |
} |
@@ -415,6 +415,72 @@ |
} |
+void JSONReader::GetDecodedValueChars(char* buf, intptr_t buflen) const { |
+ if (Type() == kNone) { |
+ return; |
+ } |
+ intptr_t max = buflen - 1; |
regis
2013/08/26 20:12:24
const
regis
2013/08/26 20:12:24
How about last_idx instead of max?
hausner
2013/08/26 20:57:40
Done.
hausner
2013/08/26 20:57:40
Done.
|
+ intptr_t value_len = ValueLen(); |
regis
2013/08/26 20:12:24
const
hausner
2013/08/26 20:57:40
Done.
|
+ const char* val = ValueChars(); |
+ intptr_t buf_idx = 0; |
+ intptr_t val_idx = 0; |
+ while ((buf_idx < max) && (val_idx < value_len)) { |
+ char ch = val[val_idx]; |
+ val_idx++; |
+ buf[buf_idx] = ch; |
regis
2013/08/26 20:12:24
You could move this buf[buf_idx] = ch to an else p
hausner
2013/08/26 20:57:40
True. OTOH I find it a bit more readable like this
|
+ if ((ch == '\\') && (val_idx < value_len)) { |
+ switch (val[val_idx]) { |
+ case '"': |
+ case '\\': |
+ case '/': |
+ buf[buf_idx] = val[val_idx]; |
+ val_idx++; |
+ break; |
+ case 'b': |
+ buf[buf_idx] = '\b'; |
+ val_idx++; |
+ break; |
+ case 'f': |
+ buf[buf_idx] = '\f'; |
+ val_idx++; |
+ break; |
+ case 'n': |
+ buf[buf_idx] = '\n'; |
+ val_idx++; |
+ break; |
+ case 'r': |
+ buf[buf_idx] = '\r'; |
+ val_idx++; |
+ break; |
+ case 't': |
+ buf[buf_idx] = '\t'; |
+ val_idx++; |
+ break; |
+ case 'u': |
+ // \u00XX |
+ // If the value is malformed or > 255, ignore and copy the |
+ // encoded characters. |
+ if ((val_idx < value_len - 4) && |
+ (val[val_idx + 1] == '0') && (val[val_idx + 2] == '0') && |
+ Utils::IsHexDigit(val[val_idx + 3]) && |
+ Utils::IsHexDigit(val[val_idx + 4])) { |
+ buf[buf_idx] = 16 * Utils::HexDigitToInt(val[val_idx + 3]) + |
+ Utils::HexDigitToInt(val[val_idx + 4]); |
+ val_idx += 5; |
+ } |
+ break; |
+ default: |
+ // Nothing. Copy the character after the backslash |
+ // in the next loop iteration. |
+ break; |
+ } |
+ } |
+ buf_idx++; |
+ } |
+ buf[buf_idx] = '\0'; |
+} |
+ |
+ |
TextBuffer::TextBuffer(intptr_t buf_size) { |
ASSERT(buf_size > 0); |
buf_ = reinterpret_cast<char*>(malloc(buf_size)); |