Chromium Code Reviews| 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/String16.h" | 5 #include "src/inspector/String16.h" | 
| 6 | 6 | 
| 7 #include "src/inspector/ProtocolPlatform.h" | 7 #include "src/inspector/ProtocolPlatform.h" | 
| 8 | 8 | 
| 9 #include <algorithm> | 9 #include <algorithm> | 
| 10 #include <cctype> | 10 #include <cctype> | 
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 for (size_t i = 0; i < length; ++i) { | 31 for (size_t i = 0; i < length; ++i) { | 
| 32 if (!isASCII(characters[i])) { | 32 if (!isASCII(characters[i])) { | 
| 33 if (ok) *ok = false; | 33 if (ok) *ok = false; | 
| 34 return 0; | 34 return 0; | 
| 35 } | 35 } | 
| 36 buffer.push_back(static_cast<char>(characters[i])); | 36 buffer.push_back(static_cast<char>(characters[i])); | 
| 37 } | 37 } | 
| 38 buffer.push_back('\0'); | 38 buffer.push_back('\0'); | 
| 39 | 39 | 
| 40 char* endptr; | 40 char* endptr; | 
| 41 int result = std::strtol(buffer.data(), &endptr, 10); | 41 long result = std::strtol(buffer.data(), &endptr, 10); | 
| 42 if (ok) *ok = !(*endptr); | 42 if (ok) *ok = !(*endptr) && result <= std::numeric_limits<int>::max(); | 
| 
 
dgozman
2016/09/12 22:44:25
Also check for >= ::min().
 
kozy
2016/09/12 23:02:59
Done.
 
 | |
| 43 return result; | 43 return static_cast<int>(result); | 
| 44 } | 44 } | 
| 45 | 45 | 
| 46 const UChar replacementCharacter = 0xFFFD; | 46 const UChar replacementCharacter = 0xFFFD; | 
| 47 using UChar32 = uint32_t; | 47 using UChar32 = uint32_t; | 
| 48 | 48 | 
| 49 inline int inlineUTF8SequenceLengthNonASCII(char b0) { | 49 inline int inlineUTF8SequenceLengthNonASCII(char b0) { | 
| 50 if ((b0 & 0xC0) != 0xC0) return 0; | 50 if ((b0 & 0xC0) != 0xC0) return 0; | 
| 51 if ((b0 & 0xE0) == 0xC0) return 2; | 51 if ((b0 & 0xE0) == 0xC0) return 2; | 
| 52 if ((b0 & 0xF0) == 0xE0) return 3; | 52 if ((b0 & 0xF0) == 0xE0) return 3; | 
| 53 if ((b0 & 0xF8) == 0xF0) return 4; | 53 if ((b0 & 0xF8) == 0xF0) return 4; | 
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 // Magic values subtracted from a buffer value during UTF8 conversion. | 242 // Magic values subtracted from a buffer value during UTF8 conversion. | 
| 243 // This table contains as many values as there might be trailing bytes | 243 // This table contains as many values as there might be trailing bytes | 
| 244 // in a UTF-8 sequence. | 244 // in a UTF-8 sequence. | 
| 245 static const UChar32 offsetsFromUTF8[6] = {0x00000000UL, | 245 static const UChar32 offsetsFromUTF8[6] = {0x00000000UL, | 
| 246 0x00003080UL, | 246 0x00003080UL, | 
| 247 0x000E2080UL, | 247 0x000E2080UL, | 
| 248 0x03C82080UL, | 248 0x03C82080UL, | 
| 249 static_cast<UChar32>(0xFA082080UL), | 249 static_cast<UChar32>(0xFA082080UL), | 
| 250 static_cast<UChar32>(0x82082080UL)}; | 250 static_cast<UChar32>(0x82082080UL)}; | 
| 251 | 251 | 
| 252 static inline UChar32 readUTF8Sequence(const char*& sequence, unsigned length) { | 252 static inline UChar32 readUTF8Sequence(const char*& sequence, size_t length) { | 
| 253 UChar32 character = 0; | 253 UChar32 character = 0; | 
| 254 | 254 | 
| 255 // The cases all fall through. | 255 // The cases all fall through. | 
| 256 switch (length) { | 256 switch (length) { | 
| 257 case 6: | 257 case 6: | 
| 258 character += static_cast<unsigned char>(*sequence++); | 258 character += static_cast<unsigned char>(*sequence++); | 
| 259 character <<= 6; | 259 character <<= 6; | 
| 260 case 5: | 260 case 5: | 
| 261 character += static_cast<unsigned char>(*sequence++); | 261 character += static_cast<unsigned char>(*sequence++); | 
| 262 character <<= 6; | 262 character <<= 6; | 
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 | 360 | 
| 361 // static | 361 // static | 
| 362 String16 String16::fromInteger(int number) { | 362 String16 String16::fromInteger(int number) { | 
| 363 const size_t kBufferSize = 50; | 363 const size_t kBufferSize = 50; | 
| 364 char buffer[kBufferSize]; | 364 char buffer[kBufferSize]; | 
| 365 std::snprintf(buffer, kBufferSize, "%d", number); | 365 std::snprintf(buffer, kBufferSize, "%d", number); | 
| 366 return String16(buffer); | 366 return String16(buffer); | 
| 367 } | 367 } | 
| 368 | 368 | 
| 369 // static | 369 // static | 
| 370 String16 String16::fromInteger(size_t number) { | |
| 371 const size_t kBufferSize = 50; | |
| 372 char buffer[kBufferSize]; | |
| 373 std::snprintf(buffer, kBufferSize, "%zu", number); | |
| 374 return String16(buffer); | |
| 375 } | |
| 376 | |
| 377 // static | |
| 370 String16 String16::fromDouble(double number) { | 378 String16 String16::fromDouble(double number) { | 
| 371 const size_t kBufferSize = 100; | 379 const size_t kBufferSize = 100; | 
| 372 char buffer[kBufferSize]; | 380 char buffer[kBufferSize]; | 
| 373 std::snprintf(buffer, kBufferSize, "%f", number); | 381 std::snprintf(buffer, kBufferSize, "%f", number); | 
| 374 return String16(buffer); | 382 return String16(buffer); | 
| 375 } | 383 } | 
| 376 | 384 | 
| 377 // static | 385 // static | 
| 378 String16 String16::fromDoublePrecision3(double number) { | 386 String16 String16::fromDoublePrecision3(double number) { | 
| 379 const size_t kBufferSize = 100; | 387 const size_t kBufferSize = 100; | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 390 return String16(buffer); | 398 return String16(buffer); | 
| 391 } | 399 } | 
| 392 | 400 | 
| 393 int String16::toInteger(bool* ok) const { | 401 int String16::toInteger(bool* ok) const { | 
| 394 return charactersToInteger(characters16(), length(), ok); | 402 return charactersToInteger(characters16(), length(), ok); | 
| 395 } | 403 } | 
| 396 | 404 | 
| 397 String16 String16::stripWhiteSpace() const { | 405 String16 String16::stripWhiteSpace() const { | 
| 398 if (!length()) return String16(); | 406 if (!length()) return String16(); | 
| 399 | 407 | 
| 400 unsigned start = 0; | 408 size_t start = 0; | 
| 401 unsigned end = length() - 1; | 409 size_t end = length() - 1; | 
| 402 | 410 | 
| 403 // skip white space from start | 411 // skip white space from start | 
| 404 while (start <= end && isSpaceOrNewLine(characters16()[start])) ++start; | 412 while (start <= end && isSpaceOrNewLine(characters16()[start])) ++start; | 
| 405 | 413 | 
| 406 // only white space | 414 // only white space | 
| 407 if (start > end) return String16(); | 415 if (start > end) return String16(); | 
| 408 | 416 | 
| 409 // skip white space from end | 417 // skip white space from end | 
| 410 while (end && isSpaceOrNewLine(characters16()[end])) --end; | 418 while (end && isSpaceOrNewLine(characters16()[end])) --end; | 
| 411 | 419 | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 449 std::vector<UChar> buffer(length); | 457 std::vector<UChar> buffer(length); | 
| 450 UChar* bufferStart = buffer.data(); | 458 UChar* bufferStart = buffer.data(); | 
| 451 | 459 | 
| 452 UChar* bufferCurrent = bufferStart; | 460 UChar* bufferCurrent = bufferStart; | 
| 453 const char* stringCurrent = stringStart; | 461 const char* stringCurrent = stringStart; | 
| 454 if (convertUTF8ToUTF16(&stringCurrent, stringStart + length, &bufferCurrent, | 462 if (convertUTF8ToUTF16(&stringCurrent, stringStart + length, &bufferCurrent, | 
| 455 bufferCurrent + buffer.size(), 0, | 463 bufferCurrent + buffer.size(), 0, | 
| 456 true) != conversionOK) | 464 true) != conversionOK) | 
| 457 return String16(); | 465 return String16(); | 
| 458 | 466 | 
| 459 unsigned utf16Length = bufferCurrent - bufferStart; | 467 size_t utf16Length = bufferCurrent - bufferStart; | 
| 460 return String16(bufferStart, utf16Length); | 468 return String16(bufferStart, utf16Length); | 
| 461 } | 469 } | 
| 462 | 470 | 
| 463 std::string String16::utf8() const { | 471 std::string String16::utf8() const { | 
| 464 unsigned length = this->length(); | 472 size_t length = this->length(); | 
| 465 | 473 | 
| 466 if (!length) return std::string(""); | 474 if (!length) return std::string(""); | 
| 467 | 475 | 
| 468 // Allocate a buffer big enough to hold all the characters | 476 // Allocate a buffer big enough to hold all the characters | 
| 469 // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes). | 477 // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes). | 
| 470 // Optimization ideas, if we find this function is hot: | 478 // Optimization ideas, if we find this function is hot: | 
| 471 // * We could speculatively create a CStringBuffer to contain 'length' | 479 // * We could speculatively create a CStringBuffer to contain 'length' | 
| 472 // characters, and resize if necessary (i.e. if the buffer contains | 480 // characters, and resize if necessary (i.e. if the buffer contains | 
| 473 // non-ascii characters). (Alternatively, scan the buffer first for | 481 // non-ascii characters). (Alternatively, scan the buffer first for | 
| 474 // ascii characters, so we know this will be sufficient). | 482 // ascii characters, so we know this will be sufficient). | 
| (...skipping 26 matching lines...) Expand all Loading... | |
| 501 // There should be room left, since one UChar hasn't been | 509 // There should be room left, since one UChar hasn't been | 
| 502 // converted. | 510 // converted. | 
| 503 DCHECK((buffer + 3) <= (buffer + bufferVector.size())); | 511 DCHECK((buffer + 3) <= (buffer + bufferVector.size())); | 
| 504 putUTF8Triple(buffer, *characters); | 512 putUTF8Triple(buffer, *characters); | 
| 505 } | 513 } | 
| 506 | 514 | 
| 507 return std::string(bufferVector.data(), buffer - bufferVector.data()); | 515 return std::string(bufferVector.data(), buffer - bufferVector.data()); | 
| 508 } | 516 } | 
| 509 | 517 | 
| 510 } // namespace v8_inspector | 518 } // namespace v8_inspector | 
| OLD | NEW |