OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/inspector/string-16.h" | 5 #include "src/inspector/string-16.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cctype> | 8 #include <cctype> |
9 #include <cstdlib> | 9 #include <cstdlib> |
10 #include <cstring> | 10 #include <cstring> |
11 #include <iomanip> | |
12 #include <limits> | 11 #include <limits> |
13 #include <locale> | |
14 #include <sstream> | |
15 #include <string> | 12 #include <string> |
16 | 13 |
17 #include "src/base/platform/platform.h" | 14 #include "src/base/platform/platform.h" |
| 15 #include "src/conversions.h" |
18 | 16 |
19 namespace v8_inspector { | 17 namespace v8_inspector { |
20 | 18 |
21 namespace { | 19 namespace { |
22 | 20 |
23 bool isASCII(UChar c) { return !(c & ~0x7F); } | 21 bool isASCII(UChar c) { return !(c & ~0x7F); } |
24 | 22 |
25 bool isSpaceOrNewLine(UChar c) { | 23 bool isSpaceOrNewLine(UChar c) { |
26 return isASCII(c) && c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); | 24 return isASCII(c) && c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); |
27 } | 25 } |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 static inline void putUTF8Triple(char*& buffer, UChar ch) { | 357 static inline void putUTF8Triple(char*& buffer, UChar ch) { |
360 *buffer++ = static_cast<char>(((ch >> 12) & 0x0F) | 0xE0); | 358 *buffer++ = static_cast<char>(((ch >> 12) & 0x0F) | 0xE0); |
361 *buffer++ = static_cast<char>(((ch >> 6) & 0x3F) | 0x80); | 359 *buffer++ = static_cast<char>(((ch >> 6) & 0x3F) | 0x80); |
362 *buffer++ = static_cast<char>((ch & 0x3F) | 0x80); | 360 *buffer++ = static_cast<char>((ch & 0x3F) | 0x80); |
363 } | 361 } |
364 | 362 |
365 } // namespace | 363 } // namespace |
366 | 364 |
367 // static | 365 // static |
368 String16 String16::fromInteger(int number) { | 366 String16 String16::fromInteger(int number) { |
369 const size_t kBufferSize = 50; | 367 char arr[50]; |
370 char buffer[kBufferSize]; | 368 v8::internal::Vector<char> buffer(arr, arraysize(arr)); |
371 v8::base::OS::SNPrintF(buffer, kBufferSize, "%d", number); | 369 return String16(IntToCString(number, buffer)); |
372 return String16(buffer); | |
373 } | 370 } |
374 | 371 |
375 // static | 372 // static |
376 String16 String16::fromInteger(size_t number) { | 373 String16 String16::fromInteger(size_t number) { |
377 const size_t kBufferSize = 50; | 374 const size_t kBufferSize = 50; |
378 char buffer[kBufferSize]; | 375 char buffer[kBufferSize]; |
379 #if !defined(_WIN32) && !defined(_WIN64) | 376 #if !defined(_WIN32) && !defined(_WIN64) |
380 v8::base::OS::SNPrintF(buffer, kBufferSize, "%zu", number); | 377 v8::base::OS::SNPrintF(buffer, kBufferSize, "%zu", number); |
381 #else | 378 #else |
382 v8::base::OS::SNPrintF(buffer, kBufferSize, "%Iu", number); | 379 v8::base::OS::SNPrintF(buffer, kBufferSize, "%Iu", number); |
383 #endif | 380 #endif |
384 return String16(buffer); | 381 return String16(buffer); |
385 } | 382 } |
386 | 383 |
387 // static | 384 // static |
388 String16 String16::fromDouble(double number) { | 385 String16 String16::fromDouble(double number) { |
389 std::ostringstream s; | 386 char arr[50]; |
390 s.imbue(std::locale("C")); | 387 v8::internal::Vector<char> buffer(arr, arraysize(arr)); |
391 s << std::fixed << std::setprecision(std::numeric_limits<double>::digits10) | 388 return String16(DoubleToCString(number, buffer)); |
392 << number; | |
393 return String16(s.str().c_str()); | |
394 } | 389 } |
395 | 390 |
396 // static | 391 // static |
397 String16 String16::fromDouble(double number, int precision) { | 392 String16 String16::fromDouble(double number, int precision) { |
398 std::ostringstream s; | 393 std::unique_ptr<char[]> str( |
399 s.imbue(std::locale("C")); | 394 v8::internal::DoubleToPrecisionCString(number, precision)); |
400 s << std::fixed << std::setprecision(precision) << number; | 395 return String16(str.get()); |
401 return String16(s.str().c_str()); | |
402 } | 396 } |
403 | 397 |
404 int String16::toInteger(bool* ok) const { | 398 int String16::toInteger(bool* ok) const { |
405 return charactersToInteger(characters16(), length(), ok); | 399 return charactersToInteger(characters16(), length(), ok); |
406 } | 400 } |
407 | 401 |
408 String16 String16::stripWhiteSpace() const { | 402 String16 String16::stripWhiteSpace() const { |
409 if (!length()) return String16(); | 403 if (!length()) return String16(); |
410 | 404 |
411 size_t start = 0; | 405 size_t start = 0; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 // There should be room left, since one UChar hasn't been | 526 // There should be room left, since one UChar hasn't been |
533 // converted. | 527 // converted. |
534 DCHECK((buffer + 3) <= (buffer + bufferVector.size())); | 528 DCHECK((buffer + 3) <= (buffer + bufferVector.size())); |
535 putUTF8Triple(buffer, *characters); | 529 putUTF8Triple(buffer, *characters); |
536 } | 530 } |
537 | 531 |
538 return std::string(bufferVector.data(), buffer - bufferVector.data()); | 532 return std::string(bufferVector.data(), buffer - bufferVector.data()); |
539 } | 533 } |
540 | 534 |
541 } // namespace v8_inspector | 535 } // namespace v8_inspector |
OLD | NEW |