Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: third_party/WebKit/Source/platform/inspector_protocol/String16STL.h

Issue 2044343002: DevTools: update V8Inspector to work with the new v8_inspector API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comment addressed Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <cstdlib>
8 #include <cstring> 9 #include <cstring>
10 #include <stdint.h>
9 #include <string> 11 #include <string>
10 #include <vector> 12 #include <vector>
11 13
12 using UChar = uint16_t; 14 using UChar = uint16_t;
13 using UChar32 = uint32_t; 15 using UChar32 = uint32_t;
14 using LChar = unsigned char; 16 using LChar = unsigned char;
15 // presubmit: allow wstring 17 // presubmit: allow wstring
16 using wstring = std::basic_string<UChar>; 18 using wstring = std::basic_string<UChar>;
17 const size_t kNotFound = static_cast<size_t>(-1); 19 const size_t kNotFound = static_cast<size_t>(-1);
18 20
19 namespace blink { 21 namespace blink {
20 namespace protocol { 22 namespace protocol {
21 23
22 class String16 { 24 class String16 {
23 public: 25 public:
24 String16() { } 26 String16() { }
25 String16(const String16& other) : m_impl(other.m_impl) { } 27 String16(const String16& other) : m_impl(other.m_impl) { }
26 // presubmit: allow wstring 28 // presubmit: allow wstring
27 String16(const wstring& impl) : m_impl(impl) { } 29 String16(const wstring& impl) : m_impl(impl) { }
28 String16(const UChar* characters) : m_impl(characters) { } 30 String16(const UChar* characters) : m_impl(characters) { }
29 String16(const char* characters) : String16(characters, std::strlen(characte rs)) { } 31 String16(const char* characters) : String16(characters, std::strlen(characte rs)) { }
30 String16(const char* characters, size_t size) 32 String16(const char* characters, size_t size)
31 { 33 {
32 m_impl.resize(size); 34 m_impl.resize(size);
33 for (size_t i = 0; i < size; ++i) 35 for (size_t i = 0; i < size; ++i)
34 m_impl[i] = characters[i]; 36 m_impl[i] = characters[i];
35 } 37 }
36 String16(const UChar* characters, size_t size) : m_impl(characters, size) { } 38 String16(const UChar* characters, size_t size) : m_impl(characters, size) { }
37 String16 isolatedCopy() const { return String16(m_impl); } 39 String16 isolatedCopy() const { return String16(m_impl); }
38 ~String16() { }
39 40
40 unsigned sizeInBytes() const { return m_impl.size() * sizeof(UChar); } 41 unsigned sizeInBytes() const { return m_impl.size() * sizeof(UChar); }
41 const UChar* characters16() const { return m_impl.c_str(); } 42 const UChar* characters16() const { return m_impl.c_str(); }
42 std::string utf8() const; 43 std::string utf8() const;
43 static String16 number(int i) { return String16(std::to_string(i).c_str()); } 44 static String16 fromUTF8(const char* stringStart, size_t length);
44 static String16 fromDouble(double d) { return String16(std::to_string(d).c_s tr()); } 45 static String16 number(int i) { return String16(String16::intToString(i).c_s tr()); }
45 static String16 fromDoubleFixedPrecision(double d, int len) { return String1 6(std::to_string(d).c_str()); } 46 static String16 fromDouble(double d) { return String16(String16::doubleToStr ing(d).c_str()); }
47 static String16 fromDoubleFixedPrecision(double d, int len) { return String1 6(String16::doubleToString(d).c_str()); }
46 48
47 static double charactersToDouble(const UChar* characters, size_t length, boo l* ok = 0) 49 static double charactersToDouble(const UChar* characters, size_t length, boo l* ok = 0)
48 { 50 {
49 size_t idx;
50 std::string str; 51 std::string str;
51 str.resize(length); 52 str.resize(length);
52 for (size_t i = 0; i < length; ++i) 53 for (size_t i = 0; i < length; ++i)
53 str[i] = static_cast<char>(characters[i]); 54 str[i] = static_cast<char>(characters[i]);
54 double result = stod(str, &idx); 55
56 const char* buffer = str.c_str();
57 char* endptr;
58 double result = strtod(buffer, &endptr);
55 if (ok) 59 if (ok)
56 *ok = idx == length; 60 *ok = buffer + length == endptr;
57 return result; 61 return result;
58 } 62 }
59 63
60 String16 substring(unsigned pos, unsigned len = 0xFFFFFFFF) const 64 String16 substring(unsigned pos, unsigned len = 0xFFFFFFFF) const
61 { 65 {
62 return String16(m_impl.substr(pos, len)); 66 return String16(m_impl.substr(pos, len));
63 } 67 }
64 68
65 String16 stripWhiteSpace() const; 69 String16 stripWhiteSpace() const;
66 70
67 int toInt(bool* ok = 0) const 71 int toInt(bool* ok = 0) const
68 { 72 {
69 size_t length = m_impl.length(); 73 size_t length = m_impl.length();
70 size_t idx;
71 std::string str; 74 std::string str;
72 str.resize(length); 75 str.resize(length);
73 for (size_t i = 0; i < length; ++i) 76 for (size_t i = 0; i < length; ++i)
74 str[i] = static_cast<char>(m_impl[i]); 77 str[i] = static_cast<char>(m_impl[i]);
75 int result = stoi(str, &idx); 78
79 const char* buffer = str.c_str();
80 char* endptr;
81 int result = strtol(buffer, &endptr, 10);
76 if (ok) 82 if (ok)
77 *ok = idx == length; 83 *ok = buffer + length == endptr;
78 return result; 84 return result;
79 } 85 }
80 86
81 size_t length() const { return m_impl.length(); } 87 size_t length() const { return m_impl.length(); }
82 bool isEmpty() const { return !m_impl.length(); } 88 bool isEmpty() const { return !m_impl.length(); }
83 UChar operator[](unsigned index) const { return m_impl[index]; } 89 UChar operator[](unsigned index) const { return m_impl[index]; }
84 90
85 size_t find(UChar c, unsigned start = 0) const 91 size_t find(UChar c, unsigned start = 0) const
86 { 92 {
87 return m_impl.find(c, start); 93 return m_impl.find(c, start);
88 } 94 }
89 95
90 size_t find(const String16& str, unsigned start = 0) const 96 size_t find(const String16& str, unsigned start = 0) const
91 { 97 {
92 return m_impl.find(str.m_impl, start); 98 return m_impl.find(str.m_impl, start);
93 } 99 }
94 100
95 size_t reverseFind(const String16& str, unsigned start = 0xFFFFFFFF) const 101 size_t reverseFind(const String16& str, unsigned start = 0xFFFFFFFF) const
96 { 102 {
97 return m_impl.rfind(str.m_impl, start); 103 return m_impl.rfind(str.m_impl, start);
98 } 104 }
99 105
106 bool startWith(const String16& s) const
107 {
108 if (m_impl.length() < s.m_impl.length())
109 return false;
110 return m_impl.substr(0, s.m_impl.length()) == s.m_impl;
111 }
112
100 bool endsWith(UChar character) const 113 bool endsWith(UChar character) const
101 { 114 {
102 return m_impl.length() && m_impl[m_impl.length() - 1] == character; 115 return m_impl.length() && m_impl[m_impl.length() - 1] == character;
103 } 116 }
104 117
105 // presubmit: allow wstring 118 // presubmit: allow wstring
106 const wstring& impl() const { return m_impl; } 119 const wstring& impl() const { return m_impl; }
107 120
108 std::size_t hash() const 121 std::size_t hash() const
109 { 122 {
110 if (!has_hash) { 123 if (!has_hash) {
111 size_t hash = 0; 124 size_t hash = 0;
112 for (size_t i = 0; i < length(); ++i) 125 for (size_t i = 0; i < length(); ++i)
113 hash = 31 * hash + m_impl[i]; 126 hash = 31 * hash + m_impl[i];
114 hash_code = hash; 127 hash_code = hash;
115 has_hash = true; 128 has_hash = true;
116 } 129 }
117 return hash_code; 130 return hash_code;
118 } 131 }
119 132
120 private: 133 private:
134 static std::string intToString(int);
135 static std::string doubleToString(double);
121 // presubmit: allow wstring 136 // presubmit: allow wstring
122 wstring m_impl; 137 wstring m_impl;
123 mutable bool has_hash = false; 138 mutable bool has_hash = false;
124 mutable std::size_t hash_code; 139 mutable std::size_t hash_code;
125 }; 140 };
126 141
127 static inline bool isSpaceOrNewline(UChar c) 142 static inline bool isSpaceOrNewline(UChar c)
128 { 143 {
129 return false; 144 return false;
130 } 145 }
(...skipping 17 matching lines...) Expand all
148 m_impl += c; 163 m_impl += c;
149 } 164 }
150 165
151 void append(char c) 166 void append(char c)
152 { 167 {
153 m_impl += c; 168 m_impl += c;
154 } 169 }
155 170
156 void appendNumber(int i) 171 void appendNumber(int i)
157 { 172 {
158 m_impl = m_impl + String16(std::to_string(i).c_str()).impl(); 173 m_impl = m_impl + String16::number(i).impl();
159 } 174 }
160 175
161 void append(const UChar* c, size_t length) 176 void append(const UChar* c, size_t length)
162 { 177 {
163 // presubmit: allow wstring 178 // presubmit: allow wstring
164 m_impl += wstring(c, length); 179 m_impl += wstring(c, length);
165 } 180 }
166 181
167 void append(const char* c, size_t length) 182 void append(const char* c, size_t length)
168 { 183 {
(...skipping 10 matching lines...) Expand all
179 } 194 }
180 195
181 private: 196 private:
182 // presubmit: allow wstring 197 // presubmit: allow wstring
183 wstring m_impl; 198 wstring m_impl;
184 }; 199 };
185 200
186 inline bool operator==(const String16& a, const String16& b) { return a.impl() = = b.impl(); } 201 inline bool operator==(const String16& a, const String16& b) { return a.impl() = = b.impl(); }
187 inline bool operator!=(const String16& a, const String16& b) { return a.impl() ! = b.impl(); } 202 inline bool operator!=(const String16& a, const String16& b) { return a.impl() ! = b.impl(); }
188 inline bool operator==(const String16& a, const char* b) { return a.impl() == St ring16(b).impl(); } 203 inline bool operator==(const String16& a, const char* b) { return a.impl() == St ring16(b).impl(); }
204 inline bool operator<(const String16& a, const String16& b) { return a.impl() < b.impl(); }
189 205
190 inline String16 operator+(const String16& a, const char* b) 206 inline String16 operator+(const String16& a, const char* b)
191 { 207 {
192 return String16(a.impl() + String16(b).impl()); 208 return String16(a.impl() + String16(b).impl());
193 } 209 }
194 210
195 inline String16 operator+(const char* a, const String16& b) 211 inline String16 operator+(const char* a, const String16& b)
196 { 212 {
197 return String16(String16(a).impl() + b.impl()); 213 return String16(String16(a).impl() + b.impl());
198 } 214 }
(...skipping 18 matching lines...) Expand all
217 class String { 233 class String {
218 public: 234 public:
219 String() {}; 235 String() {};
220 String(const String16& other) {}; 236 String(const String16& other) {};
221 operator String16() const { return String16(); }; 237 operator String16() const { return String16(); };
222 }; 238 };
223 } // namespace WTF 239 } // namespace WTF
224 240
225 using String = WTF::String; 241 using String = WTF::String;
226 242
227 namespace std {
228 template<>
229 struct hash<String16> {
230 std::size_t operator()(const String16& k) const
231 {
232 return k.hash();
233 }
234 };
235 }
236
237 #endif // !defined(String16STL_h) 243 #endif // !defined(String16STL_h)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698