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 "Collections.h" | 8 //#include "Collections.h" |
9 //#include "Platform.h" | 9 //#include "Platform.h" |
10 #include "{{config.class_export.header}}" | 10 #include "{{config.class_export.header}}" |
11 | 11 |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 namespace blink { | 14 namespace blink { |
15 namespace protocol { | 15 namespace protocol { |
16 | 16 |
17 namespace internal { | 17 namespace internal { |
18 {{config.class_export.macro}} void intToStr(int, char*, size_t); | 18 {{config.class_export.macro}} void intToStr(int, char*, size_t); |
19 {{config.class_export.macro}} void doubleToStr(double, char*, size_t); | 19 {{config.class_export.macro}} void doubleToStr(double, char*, size_t); |
20 {{config.class_export.macro}} void doubleToStr3(double, char*, size_t); | 20 {{config.class_export.macro}} void doubleToStr3(double, char*, size_t); |
21 {{config.class_export.macro}} void doubleToStr6(double, char*, size_t); | 21 {{config.class_export.macro}} void doubleToStr6(double, char*, size_t); |
22 {{config.class_export.macro}} double strToDouble(const char*, bool*); | |
23 {{config.class_export.macro}} int strToInt(const char*, bool*); | 22 {{config.class_export.macro}} int strToInt(const char*, bool*); |
24 } // namespace internal | 23 } // namespace internal |
25 | 24 |
26 template <typename T, typename C> | 25 template <typename T, typename C> |
27 class {{config.class_export.macro}} String16Base { | 26 class {{config.class_export.macro}} String16Base { |
28 public: | 27 public: |
29 static bool isASCII(C c) | 28 static bool isASCII(C c) |
30 { | 29 { |
31 return !(c & ~0x7F); | 30 return !(c & ~0x7F); |
32 } | 31 } |
(...skipping 24 matching lines...) Expand all Loading... |
57 return T(buffer); | 56 return T(buffer); |
58 } | 57 } |
59 | 58 |
60 static T fromDoublePrecision6(double number) | 59 static T fromDoublePrecision6(double number) |
61 { | 60 { |
62 char buffer[100]; | 61 char buffer[100]; |
63 internal::doubleToStr6(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); | 62 internal::doubleToStr6(number, buffer, PROTOCOL_ARRAY_LENGTH(buffer)); |
64 return T(buffer); | 63 return T(buffer); |
65 } | 64 } |
66 | 65 |
67 static double charactersToDouble(const C* characters, size_t length, bool* o
k = nullptr) | |
68 { | |
69 std::vector<char> buffer; | |
70 buffer.reserve(length + 1); | |
71 for (size_t i = 0; i < length; ++i) { | |
72 if (!isASCII(characters[i])) { | |
73 if (ok) | |
74 *ok = false; | |
75 return 0; | |
76 } | |
77 buffer.push_back(static_cast<char>(characters[i])); | |
78 } | |
79 buffer.push_back('\0'); | |
80 return internal::strToDouble(buffer.data(), ok); | |
81 } | |
82 | |
83 static int charactersToInteger(const C* characters, size_t length, bool* ok
= nullptr) | 66 static int charactersToInteger(const C* characters, size_t length, bool* ok
= nullptr) |
84 { | 67 { |
85 std::vector<char> buffer; | 68 std::vector<char> buffer; |
86 buffer.reserve(length + 1); | 69 buffer.reserve(length + 1); |
87 for (size_t i = 0; i < length; ++i) { | 70 for (size_t i = 0; i < length; ++i) { |
88 if (!isASCII(characters[i])) { | 71 if (!isASCII(characters[i])) { |
89 if (ok) | 72 if (ok) |
90 *ok = false; | 73 *ok = false; |
91 return 0; | 74 return 0; |
92 } | 75 } |
93 buffer.push_back(static_cast<char>(characters[i])); | 76 buffer.push_back(static_cast<char>(characters[i])); |
94 } | 77 } |
95 buffer.push_back('\0'); | 78 buffer.push_back('\0'); |
96 return internal::strToInt(buffer.data(), ok); | 79 return internal::strToInt(buffer.data(), ok); |
97 } | 80 } |
98 | 81 |
99 double toDouble(bool* ok = nullptr) const | |
100 { | |
101 const C* characters = static_cast<const T&>(*this).characters16(); | |
102 size_t length = static_cast<const T&>(*this).length(); | |
103 return charactersToDouble(characters, length, ok); | |
104 } | |
105 | |
106 int toInteger(bool* ok = nullptr) const | 82 int toInteger(bool* ok = nullptr) const |
107 { | 83 { |
108 const C* characters = static_cast<const T&>(*this).characters16(); | 84 const C* characters = static_cast<const T&>(*this).characters16(); |
109 size_t length = static_cast<const T&>(*this).length(); | 85 size_t length = static_cast<const T&>(*this).length(); |
110 return charactersToInteger(characters, length, ok); | 86 return charactersToInteger(characters, length, ok); |
111 } | 87 } |
112 | 88 |
113 T stripWhiteSpace() const | 89 T stripWhiteSpace() const |
114 { | 90 { |
115 size_t length = static_cast<const T&>(*this).length(); | 91 size_t length = static_cast<const T&>(*this).length(); |
(...skipping 13 matching lines...) Expand all Loading... |
129 return T(); | 105 return T(); |
130 | 106 |
131 // skip white space from end | 107 // skip white space from end |
132 while (end && isSpaceOrNewLine(characters[end])) | 108 while (end && isSpaceOrNewLine(characters[end])) |
133 --end; | 109 --end; |
134 | 110 |
135 if (!start && end == length - 1) | 111 if (!start && end == length - 1) |
136 return T(static_cast<const T&>(*this)); | 112 return T(static_cast<const T&>(*this)); |
137 return T(characters + start, end + 1 - start); | 113 return T(characters + start, end + 1 - start); |
138 } | 114 } |
139 | |
140 bool startsWith(const char* prefix) const | |
141 { | |
142 const C* characters = static_cast<const T&>(*this).characters16(); | |
143 size_t length = static_cast<const T&>(*this).length(); | |
144 for (size_t i = 0, j = 0; prefix[j] && i < length; ++i, ++j) { | |
145 if (characters[i] != prefix[j]) | |
146 return false; | |
147 } | |
148 return true; | |
149 } | |
150 }; | 115 }; |
151 | 116 |
152 } // namespace protocol | 117 } // namespace protocol |
153 } // namespace blink | 118 } // namespace blink |
154 | 119 |
155 #include "{{config.lib.string16_header}}" | 120 #include "{{config.lib.string16_header}}" |
156 | 121 |
157 namespace blink { | 122 namespace blink { |
158 namespace protocol { | 123 namespace protocol { |
159 | 124 |
(...skipping 12 matching lines...) Expand all Loading... |
172 std::vector<UChar> m_buffer; | 137 std::vector<UChar> m_buffer; |
173 }; | 138 }; |
174 | 139 |
175 } // namespace protocol | 140 } // namespace protocol |
176 } // namespace blink | 141 } // namespace blink |
177 | 142 |
178 using String16 = blink::protocol::String16; | 143 using String16 = blink::protocol::String16; |
179 using String16Builder = blink::protocol::String16Builder; | 144 using String16Builder = blink::protocol::String16Builder; |
180 | 145 |
181 #endif // !defined(String16_h) | 146 #endif // !defined(String16_h) |
OLD | NEW |