Chromium Code Reviews| 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 if (*current_pos_ == '\0') { | 90 if (*current_pos_ == '\0') { |
| 91 token_length_ = 0; | 91 token_length_ = 0; |
| 92 token_ = TokenIllegal; | 92 token_ = TokenIllegal; |
| 93 return; | 93 return; |
| 94 } else if (*current_pos_ == '\\') { | 94 } else if (*current_pos_ == '\\') { |
| 95 ++current_pos_; | 95 ++current_pos_; |
| 96 if (*current_pos_ == '"') { | 96 if (*current_pos_ == '"') { |
| 97 // Consume escaped double quote. | 97 // Consume escaped double quote. |
| 98 ++current_pos_; | 98 ++current_pos_; |
| 99 } | 99 } |
| 100 } else if (*current_pos_ < 0) { | |
| 101 // UTF-8 not supported. | |
| 102 token_length_ = 0; | |
| 103 token_ = TokenIllegal; | |
| 104 return; | |
| 105 } else { | 100 } else { |
| 106 ++current_pos_; | 101 ++current_pos_; |
| 107 } | 102 } |
| 108 } | 103 } |
| 109 token_ = TokenString; | 104 token_ = TokenString; |
| 110 token_length_ = current_pos_ - token_start_; | 105 token_length_ = current_pos_ - token_start_; |
| 111 ++current_pos_; | 106 ++current_pos_; |
| 112 } | 107 } |
| 113 | 108 |
| 114 | 109 |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 buf_ = NULL; | 340 buf_ = NULL; |
| 346 } | 341 } |
| 347 | 342 |
| 348 | 343 |
| 349 void TextBuffer::Clear() { | 344 void TextBuffer::Clear() { |
| 350 msg_len_ = 0; | 345 msg_len_ = 0; |
| 351 buf_[0] = '\0'; | 346 buf_[0] = '\0'; |
| 352 } | 347 } |
| 353 | 348 |
| 354 | 349 |
| 350 void TextBuffer::AddChar(char ch) { | |
| 351 EnsureCapacity(sizeof(ch)); | |
| 352 buf_[msg_len_] = ch; | |
| 353 msg_len_++; | |
| 354 buf_[msg_len_] = '\0'; | |
| 355 } | |
| 356 | |
| 357 | |
| 358 void TextBuffer::AddUTF8(uint32_t ch) { | |
| 359 static const uint32_t kMaxOneByteChar = 0x7F; | |
| 360 static const uint32_t kMaxTwoByteChar = 0x7FF; | |
| 361 static const uint32_t kMaxThreeByteChar = 0xFFFF; | |
| 362 static const uint32_t kMaxFourByteChar = 0x10FFFF; | |
| 363 static const uint32_t kMask = ~(1 << 6); | |
| 364 | |
| 365 EnsureCapacity(sizeof(ch)); | |
|
siva
2012/07/18 23:52:24
why do you have two calls to EnsureCapacity, one h
hausner
2012/07/19 00:11:03
Oops, good catch. This is a left-over from a previ
| |
| 366 if (ch <= kMaxOneByteChar) { | |
| 367 EnsureCapacity(1); | |
| 368 buf_[msg_len_++] = ch; | |
| 369 buf_[msg_len_] = '\0'; | |
| 370 return; | |
| 371 } | |
| 372 if (ch <= kMaxTwoByteChar) { | |
| 373 EnsureCapacity(2); | |
| 374 buf_[msg_len_++] = 0xC0 | (ch >> 6); | |
| 375 buf_[msg_len_++] = 0x80 | (ch & kMask); | |
| 376 buf_[msg_len_] = '\0'; | |
| 377 return; | |
| 378 } | |
| 379 if (ch <= kMaxThreeByteChar) { | |
| 380 EnsureCapacity(3); | |
| 381 buf_[msg_len_++] = 0xE0 | (ch >> 12); | |
| 382 buf_[msg_len_++] = 0x80 | ((ch >> 6) & kMask); | |
| 383 buf_[msg_len_++] = 0x80 | (ch & kMask); | |
| 384 buf_[msg_len_] = '\0'; | |
| 385 return; | |
| 386 } | |
| 387 ASSERT(ch <= kMaxFourByteChar); | |
| 388 EnsureCapacity(4); | |
| 389 buf_[msg_len_++] = 0xF0 | (ch >> 18); | |
| 390 buf_[msg_len_++] = 0x80 | ((ch >> 12) & kMask); | |
| 391 buf_[msg_len_++] = 0x80 | ((ch >> 6) & kMask); | |
| 392 buf_[msg_len_++] = 0x80 | (ch & kMask); | |
| 393 buf_[msg_len_] = '\0'; | |
| 394 } | |
| 395 | |
| 396 | |
| 355 intptr_t TextBuffer::Printf(const char* format, ...) { | 397 intptr_t TextBuffer::Printf(const char* format, ...) { |
| 356 va_list args; | 398 va_list args; |
| 357 va_start(args, format); | 399 va_start(args, format); |
| 358 intptr_t remaining = buf_size_ - msg_len_; | 400 intptr_t remaining = buf_size_ - msg_len_; |
| 359 ASSERT(remaining >= 0); | 401 ASSERT(remaining >= 0); |
| 360 intptr_t len = OS::VSNPrint(buf_ + msg_len_, remaining, format, args); | 402 intptr_t len = OS::VSNPrint(buf_ + msg_len_, remaining, format, args); |
| 361 va_end(args); | 403 va_end(args); |
| 362 if (len >= remaining) { | 404 if (len >= remaining) { |
| 363 const int kBufferSpareCapacity = 64; // Somewhat arbitrary. | 405 EnsureCapacity(len); |
| 364 GrowBuffer(len + kBufferSpareCapacity); | |
| 365 remaining = buf_size_ - msg_len_; | 406 remaining = buf_size_ - msg_len_; |
| 366 ASSERT(remaining > len); | 407 ASSERT(remaining > len); |
| 367 va_list args2; | 408 va_list args2; |
| 368 va_start(args2, format); | 409 va_start(args2, format); |
| 369 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); | 410 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); |
| 370 va_end(args2); | 411 va_end(args2); |
| 371 ASSERT(len == len2); | 412 ASSERT(len == len2); |
| 372 } | 413 } |
| 373 msg_len_ += len; | 414 msg_len_ += len; |
| 374 buf_[msg_len_] = '\0'; | 415 buf_[msg_len_] = '\0'; |
| 375 return len; | 416 return len; |
| 376 } | 417 } |
| 377 | 418 |
| 378 | 419 |
| 379 void TextBuffer::PrintJsonString8(const uint8_t* codepoints, intptr_t length) { | 420 void TextBuffer::AddEscapedChar(uint32_t cp) { |
| 380 for (intptr_t i = 0; i < length; i++) { | 421 switch (cp) { |
| 381 uint8_t cp = codepoints[i]; | 422 case '"': |
| 382 switch (cp) { | 423 Printf("%s", "\\\""); |
| 383 case '"': | 424 break; |
| 384 Printf("%s", "\\\""); | 425 case '\\': |
| 385 break; | 426 Printf("%s", "\\\\"); |
| 386 case '\\': | 427 break; |
| 387 Printf("%s", "\\\\"); | 428 case '/': |
| 388 break; | 429 Printf("%s", "\\/"); |
| 389 case '/': | 430 break; |
| 390 Printf("%s", "\\/"); | 431 case '\b': |
| 391 break; | 432 Printf("%s", "\\b"); |
| 392 case '\b': | 433 break; |
| 393 Printf("%s", "\\b"); | 434 case '\f': |
| 394 break; | 435 Printf("%s", "\\f"); |
| 395 case '\f': | 436 break; |
| 396 Printf("%s", "\\f"); | 437 case '\n': |
| 397 break; | 438 Printf("%s", "\\n"); |
| 398 case '\n': | 439 break; |
| 399 Printf("%s", "\\n"); | 440 case '\r': |
| 400 break; | 441 Printf("%s", "\\r"); |
| 401 case '\r': | 442 break; |
| 402 Printf("%s", "\\r"); | 443 case '\t': |
| 403 break; | 444 Printf("%s", "\\t"); |
| 404 case '\t': | 445 break; |
| 405 Printf("%s", "\\t"); | 446 default: |
| 406 break; | 447 if (cp < 0x20) { |
| 407 default: | 448 // Encode character as \u00HH. |
| 408 if ((0x20 <= cp) && (cp <= 0x7e)) { | 449 uint32_t digit2 = (cp >> 4) & 0xf; |
| 409 Printf("%c", cp); | 450 uint32_t digit3 = (cp & 0xf); |
| 410 } else { | 451 Printf("\\u00%c%c", |
| 411 // Encode character as \u00HH. | 452 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, |
| 412 uint8_t digit2 = (cp & 0xf0) >> 4; | 453 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3); |
| 413 uint8_t digit3 = cp & 0xf; | 454 } else { |
| 414 Printf("\\u00%c%c", | 455 AddUTF8(cp); |
| 415 digit2 > 9 ? 'A' + (digit2 - 10) : '0' + digit2, | 456 } |
| 416 digit3 > 9 ? 'A' + (digit3 - 10) : '0' + digit3); | |
| 417 } | |
| 418 } | |
| 419 } | 457 } |
| 420 } | 458 } |
| 421 | 459 |
| 422 | 460 |
| 423 void TextBuffer::GrowBuffer(intptr_t len) { | 461 void TextBuffer::EnsureCapacity(intptr_t len) { |
| 424 intptr_t new_size = buf_size_ + len; | 462 intptr_t remaining = buf_size_ - msg_len_; |
| 425 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); | 463 if (remaining <= len) { |
| 426 ASSERT(new_buf != NULL); | 464 const int kBufferSpareCapacity = 64; // Somewhat arbitrary. |
| 427 buf_ = new_buf; | 465 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; |
|
siva
2012/07/18 23:52:24
you may want to check with Todd on this, he has be
hausner
2012/07/19 00:11:03
Filing a bug and adding a todo.
| |
| 428 buf_size_ = new_size; | 466 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); |
| 467 ASSERT(new_buf != NULL); | |
| 468 buf_ = new_buf; | |
| 469 buf_size_ = new_size; | |
| 470 } | |
| 429 } | 471 } |
| 430 | 472 |
| 431 } // namespace dart | 473 } // namespace dart |
| OLD | NEW |