| 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 #include "src/base/platform/platform.h" | |
| 7 #include "src/inspector/protocol-platform.h" | |
| 8 | 6 |
| 9 #include <algorithm> | 7 #include <algorithm> |
| 10 #include <cctype> | 8 #include <cctype> |
| 11 #include <cstdlib> | 9 #include <cstdlib> |
| 12 #include <cstring> | 10 #include <cstring> |
| 13 #include <limits> | 11 #include <limits> |
| 14 #include <locale> | 12 #include <locale> |
| 15 #include <string> | 13 #include <string> |
| 16 | 14 |
| 15 #include "src/base/platform/platform.h" |
| 16 #include "src/inspector/protocol-platform.h" |
| 17 |
| 17 namespace v8_inspector { | 18 namespace v8_inspector { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 bool isASCII(UChar c) { return !(c & ~0x7F); } | 22 bool isASCII(UChar c) { return !(c & ~0x7F); } |
| 22 | 23 |
| 23 bool isSpaceOrNewLine(UChar c) { | 24 bool isSpaceOrNewLine(UChar c) { |
| 24 return isASCII(c) && c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); | 25 return isASCII(c) && c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); |
| 25 } | 26 } |
| 26 | 27 |
| 27 int charactersToInteger(const UChar* characters, size_t length, | 28 int charactersToInteger(const UChar* characters, size_t length, |
| 28 bool* ok = nullptr) { | 29 bool* ok = nullptr) { |
| 29 std::vector<char> buffer; | 30 std::vector<char> buffer; |
| 30 buffer.reserve(length + 1); | 31 buffer.reserve(length + 1); |
| 31 for (size_t i = 0; i < length; ++i) { | 32 for (size_t i = 0; i < length; ++i) { |
| 32 if (!isASCII(characters[i])) { | 33 if (!isASCII(characters[i])) { |
| 33 if (ok) *ok = false; | 34 if (ok) *ok = false; |
| 34 return 0; | 35 return 0; |
| 35 } | 36 } |
| 36 buffer.push_back(static_cast<char>(characters[i])); | 37 buffer.push_back(static_cast<char>(characters[i])); |
| 37 } | 38 } |
| 38 buffer.push_back('\0'); | 39 buffer.push_back('\0'); |
| 39 | 40 |
| 40 char* endptr; | 41 char* endptr; |
| 41 long result = std::strtol(buffer.data(), &endptr, 10); | 42 int64_t result = |
| 43 static_cast<int64_t>(std::strtol(buffer.data(), &endptr, 10)); |
| 42 if (ok) { | 44 if (ok) { |
| 43 *ok = !(*endptr) && result <= std::numeric_limits<int>::max() && | 45 *ok = !(*endptr) && result <= std::numeric_limits<int>::max() && |
| 44 result >= std::numeric_limits<int>::min(); | 46 result >= std::numeric_limits<int>::min(); |
| 45 } | 47 } |
| 46 return static_cast<int>(result); | 48 return static_cast<int>(result); |
| 47 } | 49 } |
| 48 | 50 |
| 49 const UChar replacementCharacter = 0xFFFD; | 51 const UChar replacementCharacter = 0xFFFD; |
| 50 using UChar32 = uint32_t; | 52 using UChar32 = uint32_t; |
| 51 | 53 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 77 } ConversionResult; | 79 } ConversionResult; |
| 78 | 80 |
| 79 ConversionResult convertUTF16ToUTF8(const UChar** sourceStart, | 81 ConversionResult convertUTF16ToUTF8(const UChar** sourceStart, |
| 80 const UChar* sourceEnd, char** targetStart, | 82 const UChar* sourceEnd, char** targetStart, |
| 81 char* targetEnd, bool strict) { | 83 char* targetEnd, bool strict) { |
| 82 ConversionResult result = conversionOK; | 84 ConversionResult result = conversionOK; |
| 83 const UChar* source = *sourceStart; | 85 const UChar* source = *sourceStart; |
| 84 char* target = *targetStart; | 86 char* target = *targetStart; |
| 85 while (source < sourceEnd) { | 87 while (source < sourceEnd) { |
| 86 UChar32 ch; | 88 UChar32 ch; |
| 87 unsigned short bytesToWrite = 0; | 89 uint32_t bytesToWrite = 0; |
| 88 const UChar32 byteMask = 0xBF; | 90 const UChar32 byteMask = 0xBF; |
| 89 const UChar32 byteMark = 0x80; | 91 const UChar32 byteMark = 0x80; |
| 90 const UChar* oldSource = | 92 const UChar* oldSource = |
| 91 source; // In case we have to back up because of target overflow. | 93 source; // In case we have to back up because of target overflow. |
| 92 ch = static_cast<unsigned short>(*source++); | 94 ch = static_cast<uint16_t>(*source++); |
| 93 // If we have a surrogate pair, convert to UChar32 first. | 95 // If we have a surrogate pair, convert to UChar32 first. |
| 94 if (ch >= 0xD800 && ch <= 0xDBFF) { | 96 if (ch >= 0xD800 && ch <= 0xDBFF) { |
| 95 // If the 16 bits following the high surrogate are in the source buffer... | 97 // If the 16 bits following the high surrogate are in the source buffer... |
| 96 if (source < sourceEnd) { | 98 if (source < sourceEnd) { |
| 97 UChar32 ch2 = static_cast<unsigned short>(*source); | 99 UChar32 ch2 = static_cast<uint16_t>(*source); |
| 98 // If it's a low surrogate, convert to UChar32. | 100 // If it's a low surrogate, convert to UChar32. |
| 99 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 101 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 100 ch = ((ch - 0xD800) << 10) + (ch2 - 0xDC00) + 0x0010000; | 102 ch = ((ch - 0xD800) << 10) + (ch2 - 0xDC00) + 0x0010000; |
| 101 ++source; | 103 ++source; |
| 102 } else if (strict) { // it's an unpaired high surrogate | 104 } else if (strict) { // it's an unpaired high surrogate |
| 103 --source; // return to the illegal value itself | 105 --source; // return to the illegal value itself |
| 104 result = sourceIllegal; | 106 result = sourceIllegal; |
| 105 break; | 107 break; |
| 106 } | 108 } |
| 107 } else { // We don't have the 16 bits following the high surrogate. | 109 } else { // We don't have the 16 bits following the high surrogate. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 133 | 135 |
| 134 target += bytesToWrite; | 136 target += bytesToWrite; |
| 135 if (target > targetEnd) { | 137 if (target > targetEnd) { |
| 136 source = oldSource; // Back up source pointer! | 138 source = oldSource; // Back up source pointer! |
| 137 target -= bytesToWrite; | 139 target -= bytesToWrite; |
| 138 result = targetExhausted; | 140 result = targetExhausted; |
| 139 break; | 141 break; |
| 140 } | 142 } |
| 141 switch (bytesToWrite) { // note: everything falls through. | 143 switch (bytesToWrite) { // note: everything falls through. |
| 142 case 4: | 144 case 4: |
| 143 *--target = (char)((ch | byteMark) & byteMask); | 145 *--target = static_cast<char>((ch | byteMark) & byteMask); |
| 144 ch >>= 6; | 146 ch >>= 6; |
| 145 case 3: | 147 case 3: |
| 146 *--target = (char)((ch | byteMark) & byteMask); | 148 *--target = static_cast<char>((ch | byteMark) & byteMask); |
| 147 ch >>= 6; | 149 ch >>= 6; |
| 148 case 2: | 150 case 2: |
| 149 *--target = (char)((ch | byteMark) & byteMask); | 151 *--target = static_cast<char>((ch | byteMark) & byteMask); |
| 150 ch >>= 6; | 152 ch >>= 6; |
| 151 case 1: | 153 case 1: |
| 152 *--target = (char)(ch | firstByteMark[bytesToWrite]); | 154 *--target = static_cast<char>(ch | firstByteMark[bytesToWrite]); |
| 153 } | 155 } |
| 154 target += bytesToWrite; | 156 target += bytesToWrite; |
| 155 } | 157 } |
| 156 *sourceStart = source; | 158 *sourceStart = source; |
| 157 *targetStart = target; | 159 *targetStart = target; |
| 158 return result; | 160 return result; |
| 159 } | 161 } |
| 160 | 162 |
| 161 /** | 163 /** |
| 162 * Is this code point a BMP code point (U+0000..U+ffff)? | 164 * Is this code point a BMP code point (U+0000..U+ffff)? |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 // There should be room left, since one UChar hasn't been | 514 // There should be room left, since one UChar hasn't been |
| 513 // converted. | 515 // converted. |
| 514 DCHECK((buffer + 3) <= (buffer + bufferVector.size())); | 516 DCHECK((buffer + 3) <= (buffer + bufferVector.size())); |
| 515 putUTF8Triple(buffer, *characters); | 517 putUTF8Triple(buffer, *characters); |
| 516 } | 518 } |
| 517 | 519 |
| 518 return std::string(bufferVector.data(), buffer - bufferVector.data()); | 520 return std::string(bufferVector.data(), buffer - bufferVector.data()); |
| 519 } | 521 } |
| 520 | 522 |
| 521 } // namespace v8_inspector | 523 } // namespace v8_inspector |
| OLD | NEW |