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