OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/json.h" | 5 #include "platform/json.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
9 #include "platform/utils.h" | 9 #include "platform/utils.h" |
10 #include "vm/os.h" | 10 #include "vm/os.h" |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); | 369 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); |
370 va_end(args2); | 370 va_end(args2); |
371 ASSERT(len == len2); | 371 ASSERT(len == len2); |
372 } | 372 } |
373 msg_len_ += len; | 373 msg_len_ += len; |
374 buf_[msg_len_] = '\0'; | 374 buf_[msg_len_] = '\0'; |
375 return len; | 375 return len; |
376 } | 376 } |
377 | 377 |
378 | 378 |
379 void TextBuffer::PrintEscapedChar(uint32_t cp) { | |
380 switch (cp) { | |
381 case '"': | |
382 Printf("%s", "\\\""); | |
383 break; | |
384 case '\\': | |
385 Printf("%s", "\\\\"); | |
386 break; | |
387 case '/': | |
388 Printf("%s", "\\/"); | |
389 break; | |
390 case '\b': | |
391 Printf("%s", "\\b"); | |
392 break; | |
393 case '\f': | |
394 Printf("%s", "\\f"); | |
395 break; | |
396 case '\n': | |
397 Printf("%s", "\\n"); | |
398 break; | |
399 case '\r': | |
400 Printf("%s", "\\r"); | |
401 break; | |
402 case '\t': | |
403 Printf("%s", "\\t"); | |
404 break; | |
405 default: | |
406 if ((0x20 <= cp) && (cp <= 0x7e)) { | |
407 Printf("%c", cp); | |
408 } else if (cp <= 0xffff) { | |
409 // Encode character as \uHHHH. | |
410 uint8_t digit0 = (cp & 0xf000) >> 12; | |
411 uint8_t digit1 = (cp & 0x0f00) >> 8; | |
412 uint8_t digit2 = (cp & 0x00f0) >> 4; | |
413 uint8_t digit3 = (cp & 0x000f); | |
414 Printf("\\u%c%c%c%c", | |
415 digit0 > 9 ? 'A' + (digit0 - 10) : '0' + digit0, | |
siva
2012/07/18 17:11:31
We have a Utils::IntToHexDigit(..) function for th
hausner
2012/07/18 22:20:36
Ok, but that function contains an assert that the
| |
416 digit1 > 9 ? 'A' + (digit1 - 10) : '0' + digit1, | |
417 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, | |
418 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3); | |
419 } else { | |
420 // Non-standard 32 bit character escape code of format \UHHHHHHHH. | |
421 uint8_t digit0 = (cp & 0xf0000000) >> 28; | |
422 uint8_t digit1 = (cp & 0x0f000000) >> 24; | |
423 uint8_t digit2 = (cp & 0x00f00000) >> 20; | |
424 uint8_t digit3 = (cp & 0x000f0000) >> 16; | |
425 uint8_t digit4 = (cp & 0x0000f000) >> 12; | |
426 uint8_t digit5 = (cp & 0x00000f00) >> 8; | |
427 uint8_t digit6 = (cp & 0x000000f0) >> 4; | |
428 uint8_t digit7 = (cp & 0x0000000f); | |
429 Printf("\\u%c%c%c%c%c%c%c%c", | |
430 digit0 > 9 ? 'A' + (digit0 - 10) : '0' + digit0, | |
431 digit1 > 9 ? 'A' + (digit1 - 10) : '0' + digit1, | |
432 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, | |
433 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3, | |
434 digit4 > 9 ? 'A' + (digit4 - 10) : '0' + digit4, | |
435 digit5 > 9 ? 'A' + (digit5 - 10) : '0' + digit5, | |
436 digit6 > 9 ? 'A' + (digit6 - 10) : '0' + digit6, | |
437 digit7 > 9 ? 'A' + (digit7 - 10) : '0' + digit7); | |
438 } | |
439 } | |
440 } | |
441 | |
442 | |
443 void TextBuffer::PrintJsonString32(const uint32_t* codepoints, | |
444 intptr_t length) { | |
445 for (intptr_t i = 0; i < length; i++) { | |
446 uint32_t cp = codepoints[i]; | |
447 PrintEscapedChar(cp); | |
448 } | |
449 } | |
450 | |
451 | |
379 void TextBuffer::PrintJsonString8(const uint8_t* codepoints, intptr_t length) { | 452 void TextBuffer::PrintJsonString8(const uint8_t* codepoints, intptr_t length) { |
380 for (intptr_t i = 0; i < length; i++) { | 453 for (intptr_t i = 0; i < length; i++) { |
381 uint8_t cp = codepoints[i]; | 454 uint8_t cp = codepoints[i]; |
382 switch (cp) { | 455 PrintEscapedChar(cp); |
383 case '"': | |
384 Printf("%s", "\\\""); | |
385 break; | |
386 case '\\': | |
387 Printf("%s", "\\\\"); | |
388 break; | |
389 case '/': | |
390 Printf("%s", "\\/"); | |
391 break; | |
392 case '\b': | |
393 Printf("%s", "\\b"); | |
394 break; | |
395 case '\f': | |
396 Printf("%s", "\\f"); | |
397 break; | |
398 case '\n': | |
399 Printf("%s", "\\n"); | |
400 break; | |
401 case '\r': | |
402 Printf("%s", "\\r"); | |
403 break; | |
404 case '\t': | |
405 Printf("%s", "\\t"); | |
406 break; | |
407 default: | |
408 if ((0x20 <= cp) && (cp <= 0x7e)) { | |
409 Printf("%c", cp); | |
410 } else { | |
411 // Encode character as \u00HH. | |
412 uint8_t digit2 = (cp & 0xf0) >> 4; | |
413 uint8_t digit3 = cp & 0xf; | |
414 Printf("\\u00%c%c", | |
415 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, | |
416 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3); | |
417 } | |
418 } | |
419 } | 456 } |
420 } | 457 } |
421 | 458 |
422 | 459 |
423 void TextBuffer::GrowBuffer(intptr_t len) { | 460 void TextBuffer::GrowBuffer(intptr_t len) { |
424 intptr_t new_size = buf_size_ + len; | 461 intptr_t new_size = buf_size_ + len; |
425 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); | 462 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); |
426 ASSERT(new_buf != NULL); | 463 ASSERT(new_buf != NULL); |
427 buf_ = new_buf; | 464 buf_ = new_buf; |
428 buf_size_ = new_size; | 465 buf_size_ = new_size; |
429 } | 466 } |
430 | 467 |
431 } // namespace dart | 468 } // namespace dart |
OLD | NEW |