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/utils.h" | 8 #include "platform/utils.h" |
9 #include "vm/os.h" | 9 #include "vm/os.h" |
10 | 10 |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 case JSONScanner::TokenTrue: | 305 case JSONScanner::TokenTrue: |
306 case JSONScanner::TokenFalse: | 306 case JSONScanner::TokenFalse: |
307 case JSONScanner::TokenNull: | 307 case JSONScanner::TokenNull: |
308 return kLiteral; | 308 return kLiteral; |
309 default: | 309 default: |
310 return kNone; | 310 return kNone; |
311 } | 311 } |
312 } | 312 } |
313 | 313 |
314 | 314 |
| 315 void JSONReader::GetValueChars(char* buf, intptr_t buflen) const { |
| 316 if (Type() == kNone) { |
| 317 return; |
| 318 } |
| 319 intptr_t max = buflen - 1; |
| 320 if (ValueLen() < max) { |
| 321 max = ValueLen(); |
| 322 } |
| 323 const char* val = ValueChars(); |
| 324 intptr_t i = 0; |
| 325 for (; i < max; i++) { |
| 326 buf[i] = val[i]; |
| 327 } |
| 328 buf[i] = '\0'; |
| 329 } |
| 330 |
| 331 |
315 TextBuffer::TextBuffer(intptr_t buf_size) { | 332 TextBuffer::TextBuffer(intptr_t buf_size) { |
316 ASSERT(buf_size > 0); | 333 ASSERT(buf_size > 0); |
317 buf_ = reinterpret_cast<char*>(malloc(buf_size)); | 334 buf_ = reinterpret_cast<char*>(malloc(buf_size)); |
318 buf_size_ = buf_size; | 335 buf_size_ = buf_size; |
319 Clear(); | 336 Clear(); |
320 } | 337 } |
321 | 338 |
322 | 339 |
323 TextBuffer::~TextBuffer() { | 340 TextBuffer::~TextBuffer() { |
324 free(buf_); | 341 free(buf_); |
325 buf_ = NULL; | 342 buf_ = NULL; |
326 } | 343 } |
327 | 344 |
328 | 345 |
329 void TextBuffer::Clear() { | 346 void TextBuffer::Clear() { |
330 msg_len_ = 0; | 347 msg_len_ = 0; |
331 buf_[0] = '\0'; | 348 buf_[0] = '\0'; |
332 } | 349 } |
333 | 350 |
334 | 351 |
335 intptr_t TextBuffer::Printf(const char* format, ...) { | 352 intptr_t TextBuffer::Printf(const char* format, va_list args) { |
336 va_list args; | 353 va_list args1; |
337 va_start(args, format); | 354 va_copy(args1, args); |
338 intptr_t remaining = buf_size_ - msg_len_; | 355 intptr_t remaining = buf_size_ - msg_len_; |
339 ASSERT(remaining >= 0); | 356 ASSERT(remaining >= 0); |
340 intptr_t len = OS::VSNPrint(buf_ + msg_len_, remaining, format, args); | 357 intptr_t len = OS::VSNPrint(buf_ + msg_len_, remaining, format, args1); |
341 va_end(args); | 358 va_end(args1); |
342 if (len >= remaining) { | 359 if (len >= remaining) { |
343 const int kBufferSpareCapacity = 64; // Somewhat arbitrary. | 360 const int kBufferSpareCapacity = 64; // Somewhat arbitrary. |
344 GrowBuffer(len + kBufferSpareCapacity); | 361 GrowBuffer(len + kBufferSpareCapacity); |
345 remaining = buf_size_ - msg_len_; | 362 remaining = buf_size_ - msg_len_; |
346 ASSERT(remaining > len); | 363 ASSERT(remaining > len); |
347 va_list args2; | 364 va_list args2; |
348 va_start(args2, format); | 365 va_copy(args2, args); |
349 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); | 366 intptr_t len2 = OS::VSNPrint(buf_ + msg_len_, remaining, format, args2); |
350 va_end(args2); | 367 va_end(args2); |
351 ASSERT(len == len2); | 368 ASSERT(len == len2); |
352 } | 369 } |
353 msg_len_ += len; | 370 msg_len_ += len; |
354 buf_[msg_len_] = '\0'; | 371 buf_[msg_len_] = '\0'; |
355 return len; | 372 return len; |
356 } | 373 } |
357 | 374 |
358 | 375 |
| 376 intptr_t TextBuffer::Printf(const char* format, ...) { |
| 377 va_list args; |
| 378 va_start(args, format); |
| 379 intptr_t len = this->Printf(format, args); |
| 380 va_end(args); |
| 381 return len; |
| 382 } |
| 383 |
| 384 |
359 void TextBuffer::GrowBuffer(intptr_t len) { | 385 void TextBuffer::GrowBuffer(intptr_t len) { |
360 intptr_t new_size = buf_size_ + len; | 386 intptr_t new_size = buf_size_ + len; |
361 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); | 387 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); |
362 ASSERT(new_buf != NULL); | 388 ASSERT(new_buf != NULL); |
363 buf_ = new_buf; | 389 buf_ = new_buf; |
364 buf_size_ = new_size; | 390 buf_size_ = new_size; |
365 } | 391 } |
366 | 392 |
367 } // namespace dart | 393 } // namespace dart |
OLD | NEW |