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 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
391 case JSONScanner::TokenTrue: | 391 case JSONScanner::TokenTrue: |
392 case JSONScanner::TokenFalse: | 392 case JSONScanner::TokenFalse: |
393 case JSONScanner::TokenNull: | 393 case JSONScanner::TokenNull: |
394 return kLiteral; | 394 return kLiteral; |
395 default: | 395 default: |
396 return kNone; | 396 return kNone; |
397 } | 397 } |
398 } | 398 } |
399 | 399 |
400 | 400 |
401 void JSONReader::GetValueChars(char* buf, intptr_t buflen) const { | 401 void JSONReader::GetRawValueChars(char* buf, intptr_t buflen) const { |
402 if (Type() == kNone) { | 402 if (Type() == kNone) { |
403 return; | 403 return; |
404 } | 404 } |
405 intptr_t max = buflen - 1; | 405 intptr_t max = buflen - 1; |
regis
2013/08/26 20:12:24
const
hausner
2013/08/26 20:57:40
Not in this case, unless I initialize it as min(bu
| |
406 if (ValueLen() < max) { | 406 if (ValueLen() < max) { |
407 max = ValueLen(); | 407 max = ValueLen(); |
408 } | 408 } |
409 const char* val = ValueChars(); | 409 const char* val = ValueChars(); |
410 intptr_t i = 0; | 410 intptr_t i = 0; |
411 for (; i < max; i++) { | 411 for (; i < max; i++) { |
412 buf[i] = val[i]; | 412 buf[i] = val[i]; |
413 } | 413 } |
414 buf[i] = '\0'; | 414 buf[i] = '\0'; |
415 } | 415 } |
416 | 416 |
417 | 417 |
418 void JSONReader::GetDecodedValueChars(char* buf, intptr_t buflen) const { | |
419 if (Type() == kNone) { | |
420 return; | |
421 } | |
422 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.
| |
423 intptr_t value_len = ValueLen(); | |
regis
2013/08/26 20:12:24
const
hausner
2013/08/26 20:57:40
Done.
| |
424 const char* val = ValueChars(); | |
425 intptr_t buf_idx = 0; | |
426 intptr_t val_idx = 0; | |
427 while ((buf_idx < max) && (val_idx < value_len)) { | |
428 char ch = val[val_idx]; | |
429 val_idx++; | |
430 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
| |
431 if ((ch == '\\') && (val_idx < value_len)) { | |
432 switch (val[val_idx]) { | |
433 case '"': | |
434 case '\\': | |
435 case '/': | |
436 buf[buf_idx] = val[val_idx]; | |
437 val_idx++; | |
438 break; | |
439 case 'b': | |
440 buf[buf_idx] = '\b'; | |
441 val_idx++; | |
442 break; | |
443 case 'f': | |
444 buf[buf_idx] = '\f'; | |
445 val_idx++; | |
446 break; | |
447 case 'n': | |
448 buf[buf_idx] = '\n'; | |
449 val_idx++; | |
450 break; | |
451 case 'r': | |
452 buf[buf_idx] = '\r'; | |
453 val_idx++; | |
454 break; | |
455 case 't': | |
456 buf[buf_idx] = '\t'; | |
457 val_idx++; | |
458 break; | |
459 case 'u': | |
460 // \u00XX | |
461 // If the value is malformed or > 255, ignore and copy the | |
462 // encoded characters. | |
463 if ((val_idx < value_len - 4) && | |
464 (val[val_idx + 1] == '0') && (val[val_idx + 2] == '0') && | |
465 Utils::IsHexDigit(val[val_idx + 3]) && | |
466 Utils::IsHexDigit(val[val_idx + 4])) { | |
467 buf[buf_idx] = 16 * Utils::HexDigitToInt(val[val_idx + 3]) + | |
468 Utils::HexDigitToInt(val[val_idx + 4]); | |
469 val_idx += 5; | |
470 } | |
471 break; | |
472 default: | |
473 // Nothing. Copy the character after the backslash | |
474 // in the next loop iteration. | |
475 break; | |
476 } | |
477 } | |
478 buf_idx++; | |
479 } | |
480 buf[buf_idx] = '\0'; | |
481 } | |
482 | |
483 | |
418 TextBuffer::TextBuffer(intptr_t buf_size) { | 484 TextBuffer::TextBuffer(intptr_t buf_size) { |
419 ASSERT(buf_size > 0); | 485 ASSERT(buf_size > 0); |
420 buf_ = reinterpret_cast<char*>(malloc(buf_size)); | 486 buf_ = reinterpret_cast<char*>(malloc(buf_size)); |
421 buf_size_ = buf_size; | 487 buf_size_ = buf_size; |
422 Clear(); | 488 Clear(); |
423 } | 489 } |
424 | 490 |
425 | 491 |
426 TextBuffer::~TextBuffer() { | 492 TextBuffer::~TextBuffer() { |
427 free(buf_); | 493 free(buf_); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 // the debugger front-end. | 634 // the debugger front-end. |
569 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; | 635 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; |
570 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); | 636 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); |
571 ASSERT(new_buf != NULL); | 637 ASSERT(new_buf != NULL); |
572 buf_ = new_buf; | 638 buf_ = new_buf; |
573 buf_size_ = new_size; | 639 buf_size_ = new_size; |
574 } | 640 } |
575 } | 641 } |
576 | 642 |
577 } // namespace dart | 643 } // namespace dart |
OLD | NEW |