Chromium Code Reviews| 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 String16STL_h | 5 #ifndef String16STL_h |
| 6 #define String16STL_h | 6 #define String16STL_h |
| 7 | 7 |
| 8 #include <cctype> | 8 #include <cctype> |
| 9 #include <cstdlib> | 9 #include <cstdlib> |
| 10 #include <cstring> | 10 #include <cstring> |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 using UChar = uint16_t; | 15 using UChar = uint16_t; |
| 16 using UChar32 = uint32_t; | 16 using UChar32 = uint32_t; |
| 17 using LChar = unsigned char; | 17 using LChar = unsigned char; |
| 18 // presubmit: allow wstring | 18 // presubmit: allow wstring |
| 19 using wstring = std::basic_string<UChar>; | 19 using wstring = std::basic_string<UChar>; |
| 20 const size_t kNotFound = static_cast<size_t>(-1); | 20 const size_t kNotFound = static_cast<size_t>(-1); |
| 21 | 21 |
| 22 namespace blink { | 22 namespace blink { |
| 23 namespace protocol { | 23 namespace protocol { |
| 24 | 24 |
| 25 class String16 { | 25 class String16 { |
| 26 public: | 26 public: |
| 27 String16() { } | 27 String16() { } |
| 28 String16(const String16& other) : m_impl(other.m_impl) { } | 28 String16(const String16& other) : m_impl(other.m_impl) { } |
| 29 // presubmit: allow wstring | 29 String16(const UChar* characters, size_t size) : m_impl(characters, size) { } |
| 30 String16(const wstring& impl) : m_impl(impl) { } | |
| 31 String16(const UChar* characters) : m_impl(characters) { } | 30 String16(const UChar* characters) : m_impl(characters) { } |
| 32 String16(const char* characters) : String16(characters, std::strlen(characte rs)) { } | 31 String16(const char* characters) : String16(characters, std::strlen(characte rs)) { } |
| 33 String16(const char* characters, size_t size) | 32 String16(const char* characters, size_t size) |
| 34 { | 33 { |
| 35 m_impl.resize(size); | 34 m_impl.resize(size); |
| 36 for (size_t i = 0; i < size; ++i) | 35 for (size_t i = 0; i < size; ++i) |
| 37 m_impl[i] = characters[i]; | 36 m_impl[i] = characters[i]; |
| 38 } | 37 } |
| 39 String16(const UChar* characters, size_t size) : m_impl(characters, size) { } | 38 |
| 40 String16 isolatedCopy() const { return String16(m_impl); } | 39 String16 isolatedCopy() const { return String16(m_impl); } |
| 41 | |
| 42 unsigned sizeInBytes() const { return m_impl.size() * sizeof(UChar); } | |
| 43 const UChar* characters16() const { return m_impl.c_str(); } | 40 const UChar* characters16() const { return m_impl.c_str(); } |
| 44 std::string utf8() const; | |
| 45 static String16 fromUTF8(const char* stringStart, size_t length); | |
| 46 static String16 fromInteger(int i) { return String16(String16::intToString(i ).c_str()); } | |
| 47 static String16 fromDouble(double d) { return String16(String16::doubleToStr ing(d).c_str()); } | |
| 48 static String16 fromDoubleFixedPrecision(double d, int len) { return String1 6(String16::doubleToString(d).c_str()); } | |
| 49 | |
| 50 static double charactersToDouble(const UChar* characters, size_t length, boo l* ok = 0) | |
| 51 { | |
| 52 std::string str; | |
| 53 str.resize(length); | |
| 54 for (size_t i = 0; i < length; ++i) | |
| 55 str[i] = static_cast<char>(characters[i]); | |
| 56 | |
| 57 const char* buffer = str.c_str(); | |
| 58 char* endptr; | |
| 59 double result = strtod(buffer, &endptr); | |
| 60 if (ok) | |
| 61 *ok = buffer + length == endptr; | |
| 62 return result; | |
| 63 } | |
| 64 | |
| 65 String16 substring(unsigned pos, unsigned len = 0xFFFFFFFF) const | |
| 66 { | |
| 67 return String16(m_impl.substr(pos, len)); | |
| 68 } | |
| 69 | |
| 70 String16 stripWhiteSpace() const; | |
| 71 | |
| 72 int toInt(bool* ok = 0) const | |
| 73 { | |
| 74 size_t length = m_impl.length(); | |
| 75 std::string str; | |
| 76 str.resize(length); | |
| 77 for (size_t i = 0; i < length; ++i) | |
| 78 str[i] = static_cast<char>(m_impl[i]); | |
| 79 | |
| 80 const char* buffer = str.c_str(); | |
| 81 char* endptr; | |
| 82 int result = strtol(buffer, &endptr, 10); | |
| 83 if (ok) | |
| 84 *ok = buffer + length == endptr; | |
| 85 return result; | |
| 86 } | |
| 87 | |
| 88 size_t length() const { return m_impl.length(); } | 41 size_t length() const { return m_impl.length(); } |
| 89 bool isEmpty() const { return !m_impl.length(); } | 42 bool isEmpty() const { return !m_impl.length(); } |
| 90 UChar operator[](unsigned index) const { return m_impl[index]; } | 43 UChar operator[](unsigned index) const { return m_impl[index]; } |
| 44 String16 substring(unsigned pos, unsigned len = 0xFFFFFFFF) const { return S tring16(m_impl.substr(pos, len)); } | |
| 45 size_t find(const String16& str, unsigned start = 0) const { return m_impl.f ind(str.m_impl, start); } | |
| 46 size_t reverseFind(const String16& str, unsigned start = 0xFFFFFFFF) const { return m_impl.rfind(str.m_impl, start); } | |
| 91 | 47 |
| 92 size_t find(UChar c, unsigned start = 0) const | 48 // Convenience methods. |
| 93 { | 49 std::string utf8() const; |
| 94 return m_impl.find(c, start); | 50 static String16 fromUTF8(const char* stringStart, size_t length); |
| 95 } | |
| 96 | |
| 97 size_t find(const String16& str, unsigned start = 0) const | |
| 98 { | |
| 99 return m_impl.find(str.m_impl, start); | |
| 100 } | |
| 101 | |
| 102 size_t reverseFind(const String16& str, unsigned start = 0xFFFFFFFF) const | |
| 103 { | |
| 104 return m_impl.rfind(str.m_impl, start); | |
| 105 } | |
| 106 | |
| 107 bool startWith(const String16& s) const | |
| 108 { | |
| 109 if (m_impl.length() < s.m_impl.length()) | |
| 110 return false; | |
| 111 return m_impl.substr(0, s.m_impl.length()) == s.m_impl; | |
| 112 } | |
| 113 | |
| 114 bool endsWith(UChar character) const | |
| 115 { | |
| 116 return m_impl.length() && m_impl[m_impl.length() - 1] == character; | |
| 117 } | |
| 118 | 51 |
| 119 // presubmit: allow wstring | 52 // presubmit: allow wstring |
| 120 const wstring& impl() const { return m_impl; } | 53 const wstring& impl() const { return m_impl; } |
| 54 // presubmit: allow wstring | |
| 55 String16(const wstring& impl) : m_impl(impl) { } | |
| 121 | 56 |
| 122 std::size_t hash() const | 57 std::size_t hash() const |
|
pfeldman
2016/08/09 17:20:38
Could this also move out?
| |
| 123 { | 58 { |
| 124 if (!has_hash) { | 59 if (!has_hash) { |
| 125 size_t hash = 0; | 60 size_t hash = 0; |
| 126 for (size_t i = 0; i < length(); ++i) | 61 for (size_t i = 0; i < length(); ++i) |
| 127 hash = 31 * hash + m_impl[i]; | 62 hash = 31 * hash + m_impl[i]; |
| 128 hash_code = hash; | 63 hash_code = hash; |
| 129 has_hash = true; | 64 has_hash = true; |
| 130 } | 65 } |
| 131 return hash_code; | 66 return hash_code; |
| 132 } | 67 } |
| 133 | 68 |
| 134 private: | 69 private: |
| 135 static std::string intToString(int); | |
| 136 static std::string doubleToString(double); | |
| 137 // presubmit: allow wstring | 70 // presubmit: allow wstring |
| 138 wstring m_impl; | 71 wstring m_impl; |
| 139 mutable bool has_hash = false; | 72 mutable bool has_hash = false; |
| 140 mutable std::size_t hash_code = 0; | 73 mutable std::size_t hash_code = 0; |
| 141 }; | 74 }; |
| 142 | 75 |
| 143 static inline bool isSpaceOrNewline(UChar c) | |
| 144 { | |
| 145 return std::isspace(c); // NOLINT | |
| 146 } | |
| 147 | |
| 148 class String16Builder { | |
| 149 public: | |
| 150 String16Builder() { } | |
| 151 | |
| 152 void append(const String16& str) | |
| 153 { | |
| 154 m_impl += str.impl(); | |
| 155 } | |
| 156 | |
| 157 void append(UChar c) | |
| 158 { | |
| 159 m_impl += c; | |
| 160 } | |
| 161 | |
| 162 void append(LChar c) | |
| 163 { | |
| 164 m_impl += c; | |
| 165 } | |
| 166 | |
| 167 void append(char c) | |
| 168 { | |
| 169 m_impl += c; | |
| 170 } | |
| 171 | |
| 172 void appendNumber(int i) | |
| 173 { | |
| 174 m_impl = m_impl + String16::fromInteger(i).impl(); | |
| 175 } | |
| 176 | |
| 177 void appendNumber(double d) | |
| 178 { | |
| 179 m_impl = m_impl + String16::fromDoubleFixedPrecision(d, 6).impl(); | |
| 180 } | |
| 181 | |
| 182 void append(const UChar* c, size_t length) | |
| 183 { | |
| 184 // presubmit: allow wstring | |
| 185 m_impl += wstring(c, length); | |
| 186 } | |
| 187 | |
| 188 void append(const char* c, size_t length) | |
| 189 { | |
| 190 m_impl += String16(c, length).impl(); | |
| 191 } | |
| 192 | |
| 193 String16 toString() | |
| 194 { | |
| 195 return String16(m_impl); | |
| 196 } | |
| 197 | |
| 198 void reserveCapacity(unsigned newCapacity) | |
| 199 { | |
| 200 } | |
| 201 | |
| 202 private: | |
| 203 // presubmit: allow wstring | |
| 204 wstring m_impl; | |
| 205 }; | |
| 206 | |
| 207 inline bool operator==(const String16& a, const String16& b) { return a.impl() = = b.impl(); } | 76 inline bool operator==(const String16& a, const String16& b) { return a.impl() = = b.impl(); } |
|
pfeldman
2016/08/09 17:20:38
Same if you make base a template with pre-defined
| |
| 208 inline bool operator!=(const String16& a, const String16& b) { return a.impl() ! = b.impl(); } | 77 inline bool operator!=(const String16& a, const String16& b) { return a.impl() ! = b.impl(); } |
| 209 inline bool operator==(const String16& a, const char* b) { return a.impl() == St ring16(b).impl(); } | 78 inline bool operator==(const String16& a, const char* b) { return a.impl() == St ring16(b).impl(); } |
| 210 inline bool operator<(const String16& a, const String16& b) { return a.impl() < b.impl(); } | 79 inline bool operator<(const String16& a, const String16& b) { return a.impl() < b.impl(); } |
| 211 | 80 |
| 212 inline String16 operator+(const String16& a, const char* b) | 81 inline String16 operator+(const String16& a, const char* b) |
| 213 { | 82 { |
| 214 return String16(a.impl() + String16(b).impl()); | 83 return String16(a.impl() + String16(b).impl()); |
| 215 } | 84 } |
| 216 | 85 |
| 217 inline String16 operator+(const char* a, const String16& b) | 86 inline String16 operator+(const char* a, const String16& b) |
| 218 { | 87 { |
| 219 return String16(String16(a).impl() + b.impl()); | 88 return String16(String16(a).impl() + b.impl()); |
| 220 } | 89 } |
| 221 | 90 |
| 222 inline String16 operator+(const String16& a, const String16& b) | 91 inline String16 operator+(const String16& a, const String16& b) |
| 223 { | 92 { |
| 224 return String16(a.impl() + b.impl()); | 93 return String16(a.impl() + b.impl()); |
| 225 } | 94 } |
| 226 | 95 |
| 227 } // namespace protocol | 96 } // namespace protocol |
| 228 } // namespace blink | 97 } // namespace blink |
| 229 | 98 |
| 230 using String16 = blink::protocol::String16; | |
| 231 using String16Builder = blink::protocol::String16Builder; | |
| 232 | |
| 233 | |
| 234 namespace WTF { | |
| 235 // Interim solution for those headers that reference WTF::String for overrides. | |
| 236 // It does nothing. If the code actually relies on WTF:String, it will not | |
| 237 // compile! | |
| 238 // TODO(eostroukhov): Eradicate | |
| 239 class String { | |
| 240 public: | |
| 241 String() {}; | |
| 242 String(const String16& other) {}; | |
| 243 operator String16() const { return String16(); }; | |
| 244 }; | |
| 245 } // namespace WTF | |
| 246 | |
| 247 #if !defined(__APPLE__) || defined(_LIBCPP_VERSION) | 99 #if !defined(__APPLE__) || defined(_LIBCPP_VERSION) |
| 248 | 100 |
| 249 namespace std { | 101 namespace std { |
| 250 template<> struct hash<String16> { | 102 template<> struct hash<blink::protocol::String16> { |
| 251 std::size_t operator()(const String16& string) const | 103 std::size_t operator()(const blink::protocol::String16& string) const |
| 252 { | 104 { |
| 253 return string.hash(); | 105 return string.hash(); |
| 254 } | 106 } |
| 255 }; | 107 }; |
| 256 | 108 |
| 257 } // namespace std | 109 } // namespace std |
| 258 | 110 |
| 259 #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION) | 111 #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION) |
| 260 | 112 |
| 261 using String = WTF::String; | 113 class InspectorProtocolConvenienceStringType { |
| 114 public: | |
| 115 // This class should not be ever instantiated, so we don't implement constru ctors. | |
|
kozy
2016/08/09 16:40:37
Can we make constructor private?
| |
| 116 InspectorProtocolConvenienceStringType(); | |
| 117 InspectorProtocolConvenienceStringType(const blink::protocol::String16& othe r); | |
| 118 operator blink::protocol::String16() const { return blink::protocol::String1 6(); }; | |
| 119 }; | |
| 262 | 120 |
| 263 #endif // !defined(String16STL_h) | 121 #endif // !defined(String16STL_h) |
| OLD | NEW |