OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium 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 #ifndef String16_h | 5 #ifndef String16_h |
6 #define String16_h | 6 #define String16_h |
7 | 7 |
| 8 #include "platform/inspector_protocol/Platform.h" |
| 9 |
| 10 #include <vector> |
| 11 |
| 12 namespace blink { |
| 13 namespace protocol { |
| 14 |
| 15 namespace internal { |
| 16 PLATFORM_EXPORT void intToStr(int, char*); |
| 17 PLATFORM_EXPORT void doubleToStr(double, char*); |
| 18 PLATFORM_EXPORT void doubleToStr3(double, char*); |
| 19 PLATFORM_EXPORT void doubleToStr6(double, char*); |
| 20 PLATFORM_EXPORT double strToDouble(const char*, bool*); |
| 21 PLATFORM_EXPORT int strToInt(const char*, bool*); |
| 22 } // namespace internal |
| 23 |
| 24 template <typename T, typename C> |
| 25 class PLATFORM_EXPORT String16Base { |
| 26 public: |
| 27 static bool isASCII(C c) |
| 28 { |
| 29 return !(c & ~0x7F); |
| 30 } |
| 31 |
| 32 static bool isSpaceOrNewLine(C c) |
| 33 { |
| 34 return isASCII(c) ? c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)) : f
alse; |
| 35 } |
| 36 |
| 37 static T fromInteger(int number) |
| 38 { |
| 39 static char buffer[50]; |
| 40 internal::intToStr(number, buffer); |
| 41 return T(buffer); |
| 42 } |
| 43 |
| 44 static T fromDouble(double number) |
| 45 { |
| 46 static char buffer[100]; |
| 47 internal::doubleToStr(number, buffer); |
| 48 return T(buffer); |
| 49 } |
| 50 |
| 51 static T fromDoublePrecision3(double number) |
| 52 { |
| 53 static char buffer[100]; |
| 54 internal::doubleToStr3(number, buffer); |
| 55 return T(buffer); |
| 56 } |
| 57 |
| 58 static T fromDoublePrecision6(double number) |
| 59 { |
| 60 static char buffer[100]; |
| 61 internal::doubleToStr6(number, buffer); |
| 62 return T(buffer); |
| 63 } |
| 64 |
| 65 static double charactersToDouble(const C* characters, size_t length, bool* o
k = nullptr) |
| 66 { |
| 67 char buffer[length + 1]; |
| 68 for (size_t i = 0; i < length; ++i) { |
| 69 if (!isASCII(characters[i])) { |
| 70 if (ok) |
| 71 *ok = false; |
| 72 return 0; |
| 73 } |
| 74 buffer[i] = static_cast<char>(characters[i]); |
| 75 } |
| 76 buffer[length] = '\0'; |
| 77 return internal::strToDouble(buffer, ok); |
| 78 } |
| 79 |
| 80 static int charactersToInteger(const C* characters, size_t length, bool* ok
= nullptr) |
| 81 { |
| 82 char buffer[length + 1]; |
| 83 for (size_t i = 0; i < length; ++i) { |
| 84 if (!isASCII(characters[i])) { |
| 85 if (ok) |
| 86 *ok = false; |
| 87 return 0; |
| 88 } |
| 89 buffer[i] = static_cast<char>(characters[i]); |
| 90 } |
| 91 buffer[length] = '\0'; |
| 92 return internal::strToInt(buffer, ok); |
| 93 } |
| 94 |
| 95 double toDouble(bool* ok = nullptr) const |
| 96 { |
| 97 const C* characters = static_cast<const T&>(*this).characters16(); |
| 98 size_t length = static_cast<const T&>(*this).length(); |
| 99 return charactersToDouble(characters, length, ok); |
| 100 } |
| 101 |
| 102 int toInteger(bool* ok = nullptr) const |
| 103 { |
| 104 const C* characters = static_cast<const T&>(*this).characters16(); |
| 105 size_t length = static_cast<const T&>(*this).length(); |
| 106 return charactersToInteger(characters, length, ok); |
| 107 } |
| 108 |
| 109 T stripWhiteSpace() const |
| 110 { |
| 111 size_t length = static_cast<const T&>(*this).length(); |
| 112 if (!length) |
| 113 return T(); |
| 114 |
| 115 unsigned start = 0; |
| 116 unsigned end = length - 1; |
| 117 const C* characters = static_cast<const T&>(*this).characters16(); |
| 118 |
| 119 // skip white space from start |
| 120 while (start <= end && isSpaceOrNewLine(characters[start])) |
| 121 ++start; |
| 122 |
| 123 // only white space |
| 124 if (start > end) |
| 125 return T(); |
| 126 |
| 127 // skip white space from end |
| 128 while (end && isSpaceOrNewLine(characters[end])) |
| 129 --end; |
| 130 |
| 131 if (!start && end == length - 1) |
| 132 return T(static_cast<const T&>(*this)); |
| 133 return T(characters + start, end + 1 - start); |
| 134 } |
| 135 |
| 136 bool startsWith(const char* prefix) const |
| 137 { |
| 138 const C* characters = static_cast<const T&>(*this).characters16(); |
| 139 size_t length = static_cast<const T&>(*this).length(); |
| 140 for (size_t i = 0, j = 0; prefix[j] && i < length; ++i, ++j) { |
| 141 if (characters[i] != prefix[j]) |
| 142 return false; |
| 143 } |
| 144 return true; |
| 145 } |
| 146 }; |
| 147 |
| 148 } // namespace protocol |
| 149 } // namespace blink |
| 150 |
8 #if V8_INSPECTOR_USE_STL | 151 #if V8_INSPECTOR_USE_STL |
9 #include "platform/inspector_protocol/String16STL.h" | 152 #include "platform/inspector_protocol/String16STL.h" |
10 #else | 153 #else |
11 #include "platform/inspector_protocol/String16WTF.h" | 154 #include "platform/inspector_protocol/String16WTF.h" |
12 #endif // V8_INSPECTOR_USE_STL | 155 #endif // V8_INSPECTOR_USE_STL |
13 | 156 |
| 157 namespace blink { |
| 158 namespace protocol { |
| 159 |
| 160 class PLATFORM_EXPORT String16Builder { |
| 161 public: |
| 162 String16Builder(); |
| 163 void append(const String16&); |
| 164 void append(UChar); |
| 165 void append(char); |
| 166 void append(const UChar*, size_t); |
| 167 void append(const char*, size_t); |
| 168 String16 toString(); |
| 169 void reserveCapacity(size_t); |
| 170 |
| 171 private: |
| 172 std::vector<UChar> m_buffer; |
| 173 }; |
| 174 |
| 175 } // namespace protocol |
| 176 } // namespace blink |
| 177 |
| 178 using String16 = blink::protocol::String16; |
| 179 using String16Builder = blink::protocol::String16Builder; |
| 180 |
14 #endif // !defined(String16_h) | 181 #endif // !defined(String16_h) |
OLD | NEW |