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