OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 13 matching lines...) Expand all Loading... |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef DOUBLE_CONVERSION_UTILS_H_ | 28 #ifndef DOUBLE_CONVERSION_UTILS_H_ |
29 #define DOUBLE_CONVERSION_UTILS_H_ | 29 #define DOUBLE_CONVERSION_UTILS_H_ |
30 | 30 |
31 #include "wtf/Assertions.h" | 31 #include "wtf/Assertions.h" |
32 #include <string.h> | 32 #include <string.h> |
33 | 33 |
34 #define UNIMPLEMENTED ASSERT_NOT_REACHED | 34 #define UNIMPLEMENTED NOTREACHED |
35 #define UNREACHABLE ASSERT_NOT_REACHED | 35 #define UNREACHABLE NOTREACHED |
36 | 36 |
37 // Double operations detection based on target architecture. | 37 // Double operations detection based on target architecture. |
38 // Linux uses a 80bit wide floating point stack on x86. This induces double | 38 // Linux uses a 80bit wide floating point stack on x86. This induces double |
39 // rounding, which in turn leads to wrong results. | 39 // rounding, which in turn leads to wrong results. |
40 // An easy way to test if the floating-point operations are correct is to | 40 // An easy way to test if the floating-point operations are correct is to |
41 // evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then | 41 // evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then |
42 // the result is equal to 89255e-22. | 42 // the result is equal to 89255e-22. |
43 // The best way to test this, is to create a division-function and to compare | 43 // The best way to test this, is to create a division-function and to compare |
44 // the output of the division with the expected result. (Inlining must be | 44 // the output of the division with the expected result. (Inlining must be |
45 // disabled.) | 45 // disabled.) |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 } | 120 } |
121 | 121 |
122 // Returns the minimum of the two parameters. | 122 // Returns the minimum of the two parameters. |
123 template <typename T> | 123 template <typename T> |
124 static T Min(T a, T b) { | 124 static T Min(T a, T b) { |
125 return a < b ? a : b; | 125 return a < b ? a : b; |
126 } | 126 } |
127 | 127 |
128 inline int StrLength(const char* string) { | 128 inline int StrLength(const char* string) { |
129 size_t length = strlen(string); | 129 size_t length = strlen(string); |
130 ASSERT(length == static_cast<size_t>(static_cast<int>(length))); | 130 DCHECK_EQ(length, static_cast<size_t>(static_cast<int>(length))); |
131 return static_cast<int>(length); | 131 return static_cast<int>(length); |
132 } | 132 } |
133 | 133 |
134 // This is a simplified version of V8's Vector class. | 134 // This is a simplified version of V8's Vector class. |
135 template <typename T> | 135 template <typename T> |
136 class Vector { | 136 class Vector { |
137 public: | 137 public: |
138 Vector() : start_(NULL), length_(0) {} | 138 Vector() : start_(NULL), length_(0) {} |
139 Vector(T* data, int length) : start_(data), length_(length) { | 139 Vector(T* data, int length) : start_(data), length_(length) { |
140 ASSERT(length == 0 || (length > 0 && data != NULL)); | 140 DCHECK(length == 0 || (length > 0 && data != NULL)); |
141 } | 141 } |
142 | 142 |
143 // Returns a vector using the same backing storage as this one, | 143 // Returns a vector using the same backing storage as this one, |
144 // spanning from and including 'from', to but not including 'to'. | 144 // spanning from and including 'from', to but not including 'to'. |
145 Vector<T> SubVector(int from, int to) { | 145 Vector<T> SubVector(int from, int to) { |
146 ASSERT(to <= length_); | 146 DCHECK_LE(to, length_); |
147 ASSERT(from < to); | 147 DCHECK_LT(from, to); |
148 ASSERT(0 <= from); | 148 DCHECK_LE(0, from); |
149 return Vector<T>(start() + from, to - from); | 149 return Vector<T>(start() + from, to - from); |
150 } | 150 } |
151 | 151 |
152 // Returns the length of the vector. | 152 // Returns the length of the vector. |
153 int length() const { return length_; } | 153 int length() const { return length_; } |
154 | 154 |
155 // Returns whether or not the vector is empty. | 155 // Returns whether or not the vector is empty. |
156 bool is_empty() const { return length_ == 0; } | 156 bool is_empty() const { return length_ == 0; } |
157 | 157 |
158 // Returns the pointer to the start of the data in the vector. | 158 // Returns the pointer to the start of the data in the vector. |
(...skipping 23 matching lines...) Expand all Loading... |
182 | 182 |
183 ~StringBuilder() { | 183 ~StringBuilder() { |
184 if (!is_finalized()) | 184 if (!is_finalized()) |
185 Finalize(); | 185 Finalize(); |
186 } | 186 } |
187 | 187 |
188 int size() const { return buffer_.length(); } | 188 int size() const { return buffer_.length(); } |
189 | 189 |
190 // Get the current position in the builder. | 190 // Get the current position in the builder. |
191 int position() const { | 191 int position() const { |
192 ASSERT(!is_finalized()); | 192 DCHECK(!is_finalized()); |
193 return position_; | 193 return position_; |
194 } | 194 } |
195 | 195 |
196 // Set the current position in the builder. | 196 // Set the current position in the builder. |
197 void SetPosition(int position) { | 197 void SetPosition(int position) { |
198 ASSERT(!is_finalized()); | 198 DCHECK(!is_finalized()); |
199 SECURITY_DCHECK(position < size()); | 199 SECURITY_DCHECK(position < size()); |
200 position_ = position; | 200 position_ = position; |
201 } | 201 } |
202 | 202 |
203 // Reset the position. | 203 // Reset the position. |
204 void Reset() { position_ = 0; } | 204 void Reset() { position_ = 0; } |
205 | 205 |
206 // Add a single character to the builder. It is not allowed to add | 206 // Add a single character to the builder. It is not allowed to add |
207 // 0-characters; use the Finalize() method to terminate the string | 207 // 0-characters; use the Finalize() method to terminate the string |
208 // instead. | 208 // instead. |
209 void AddCharacter(char c) { | 209 void AddCharacter(char c) { |
210 ASSERT(c != '\0'); | 210 DCHECK_NE(c, '\0'); |
211 ASSERT(!is_finalized() && position_ < buffer_.length()); | 211 DCHECK(!is_finalized()); |
| 212 DCHECK_LT(position_, buffer_.length()); |
212 buffer_[position_++] = c; | 213 buffer_[position_++] = c; |
213 } | 214 } |
214 | 215 |
215 // Add an entire string to the builder. Uses strlen() internally to | 216 // Add an entire string to the builder. Uses strlen() internally to |
216 // compute the length of the input string. | 217 // compute the length of the input string. |
217 void AddString(const char* s) { AddSubstring(s, StrLength(s)); } | 218 void AddString(const char* s) { AddSubstring(s, StrLength(s)); } |
218 | 219 |
219 // Add the first 'n' characters of the given string 's' to the | 220 // Add the first 'n' characters of the given string 's' to the |
220 // builder. The input string must have enough characters. | 221 // builder. The input string must have enough characters. |
221 void AddSubstring(const char* s, int n) { | 222 void AddSubstring(const char* s, int n) { |
222 ASSERT(!is_finalized() && position_ + n < buffer_.length()); | 223 DCHECK(!is_finalized()); |
| 224 DCHECK_LT(position_ + n, buffer_.length()); |
223 SECURITY_DCHECK(static_cast<size_t>(n) <= strlen(s)); | 225 SECURITY_DCHECK(static_cast<size_t>(n) <= strlen(s)); |
224 memcpy(&buffer_[position_], s, n * kCharSize); | 226 memcpy(&buffer_[position_], s, n * kCharSize); |
225 position_ += n; | 227 position_ += n; |
226 } | 228 } |
227 | 229 |
228 // Add character padding to the builder. If count is non-positive, | 230 // Add character padding to the builder. If count is non-positive, |
229 // nothing is added to the builder. | 231 // nothing is added to the builder. |
230 void AddPadding(char c, int count) { | 232 void AddPadding(char c, int count) { |
231 for (int i = 0; i < count; i++) { | 233 for (int i = 0; i < count; i++) { |
232 AddCharacter(c); | 234 AddCharacter(c); |
233 } | 235 } |
234 } | 236 } |
235 | 237 |
236 // Finalize the string by 0-terminating it and returning the buffer. | 238 // Finalize the string by 0-terminating it and returning the buffer. |
237 char* Finalize() { | 239 char* Finalize() { |
238 ASSERT(!is_finalized() && position_ < buffer_.length()); | 240 DCHECK(!is_finalized()); |
| 241 DCHECK_LT(position_, buffer_.length()); |
239 buffer_[position_] = '\0'; | 242 buffer_[position_] = '\0'; |
240 // Make sure nobody managed to add a 0-character to the | 243 // Make sure nobody managed to add a 0-character to the |
241 // buffer while building the string. | 244 // buffer while building the string. |
242 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); | 245 DCHECK_EQ(strlen(buffer_.start()), static_cast<size_t>(position_)); |
243 position_ = -1; | 246 position_ = -1; |
244 ASSERT(is_finalized()); | 247 DCHECK(is_finalized()); |
245 return buffer_.start(); | 248 return buffer_.start(); |
246 } | 249 } |
247 | 250 |
248 private: | 251 private: |
249 Vector<char> buffer_; | 252 Vector<char> buffer_; |
250 int position_; | 253 int position_; |
251 | 254 |
252 bool is_finalized() const { return position_ < 0; } | 255 bool is_finalized() const { return position_ < 0; } |
253 | 256 |
254 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); | 257 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 template <class Dest, class Source> | 295 template <class Dest, class Source> |
293 inline Dest BitCast(Source* source) { | 296 inline Dest BitCast(Source* source) { |
294 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source)); | 297 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source)); |
295 } | 298 } |
296 | 299 |
297 } // namespace double_conversion | 300 } // namespace double_conversion |
298 | 301 |
299 } // namespace WTF | 302 } // namespace WTF |
300 | 303 |
301 #endif // DOUBLE_CONVERSION_UTILS_H_ | 304 #endif // DOUBLE_CONVERSION_UTILS_H_ |
OLD | NEW |