OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 272 |
273 | 273 |
274 template <typename T> | 274 template <typename T> |
275 class Vector { | 275 class Vector { |
276 public: | 276 public: |
277 Vector() : start_(NULL), length_(0) {} | 277 Vector() : start_(NULL), length_(0) {} |
278 Vector(T* data, int length) : start_(data), length_(length) { | 278 Vector(T* data, int length) : start_(data), length_(length) { |
279 ASSERT(length == 0 || (length > 0 && data != NULL)); | 279 ASSERT(length == 0 || (length > 0 && data != NULL)); |
280 } | 280 } |
281 | 281 |
| 282 static Vector<T> New(int length) { |
| 283 return Vector<T>(NewArray<T>(length), length); |
| 284 } |
| 285 |
282 // Returns the length of the vector. | 286 // Returns the length of the vector. |
283 int length() const { return length_; } | 287 int length() const { return length_; } |
284 | 288 |
285 // Returns whether or not the vector is empty. | 289 // Returns whether or not the vector is empty. |
286 bool is_empty() const { return length_ == 0; } | 290 bool is_empty() const { return length_ == 0; } |
287 | 291 |
288 // Returns the pointer to the start of the data in the vector. | 292 // Returns the pointer to the start of the data in the vector. |
289 T* start() const { return start_; } | 293 T* start() const { return start_; } |
290 | 294 |
291 // Access individual vector elements - checks bounds in debug mode. | 295 // Access individual vector elements - checks bounds in debug mode. |
(...skipping 11 matching lines...) Expand all Loading... |
303 | 307 |
304 // Releases the array underlying this vector. Once disposed the | 308 // Releases the array underlying this vector. Once disposed the |
305 // vector is empty. | 309 // vector is empty. |
306 void Dispose() { | 310 void Dispose() { |
307 if (is_empty()) return; | 311 if (is_empty()) return; |
308 DeleteArray(start_); | 312 DeleteArray(start_); |
309 start_ = NULL; | 313 start_ = NULL; |
310 length_ = 0; | 314 length_ = 0; |
311 } | 315 } |
312 | 316 |
| 317 inline Vector<T> operator+(int offset) { |
| 318 ASSERT(offset < length_); |
| 319 return Vector<T>(start_ + offset, length_ - offset); |
| 320 } |
| 321 |
313 // Factory method for creating empty vectors. | 322 // Factory method for creating empty vectors. |
314 static Vector<T> empty() { return Vector<T>(NULL, 0); } | 323 static Vector<T> empty() { return Vector<T>(NULL, 0); } |
315 | 324 |
316 private: | 325 private: |
317 T* start_; | 326 T* start_; |
318 int length_; | 327 int length_; |
319 }; | 328 }; |
320 | 329 |
321 | 330 |
| 331 template <typename T, int size> |
| 332 class EmbeddedVector : public Vector<T> { |
| 333 public: |
| 334 EmbeddedVector() : Vector<T>(buffer_, size) { } |
| 335 private: |
| 336 T buffer_[size]; |
| 337 }; |
| 338 |
| 339 |
322 inline Vector<const char> CStrVector(const char* data) { | 340 inline Vector<const char> CStrVector(const char* data) { |
323 return Vector<const char>(data, strlen(data)); | 341 return Vector<const char>(data, strlen(data)); |
324 } | 342 } |
325 | 343 |
326 inline Vector<char> MutableCStrVector(char* data) { | 344 inline Vector<char> MutableCStrVector(char* data) { |
327 return Vector<char>(data, strlen(data)); | 345 return Vector<char>(data, strlen(data)); |
328 } | 346 } |
329 | 347 |
| 348 inline Vector<char> MutableCStrVector(char* data, int max) { |
| 349 int length = strlen(data); |
| 350 return Vector<char>(data, (length < max) ? length : max); |
| 351 } |
| 352 |
330 template <typename T> | 353 template <typename T> |
331 inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms, | 354 inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms, |
332 int length) { | 355 int length) { |
333 return Vector< Handle<Object> >( | 356 return Vector< Handle<Object> >( |
334 reinterpret_cast<v8::internal::Handle<Object>*>(elms), length); | 357 reinterpret_cast<v8::internal::Handle<Object>*>(elms), length); |
335 } | 358 } |
336 | 359 |
337 | 360 |
338 // Simple support to read a file into a 0-terminated C-string. | 361 // Simple support to read a file into a 0-terminated C-string. |
339 // The returned buffer must be freed by the caller. | 362 // The returned buffer must be freed by the caller. |
(...skipping 22 matching lines...) Expand all Loading... |
362 // purpose of the class is to use safe operations that checks the | 385 // purpose of the class is to use safe operations that checks the |
363 // buffer bounds on all operations in debug mode. | 386 // buffer bounds on all operations in debug mode. |
364 class StringBuilder { | 387 class StringBuilder { |
365 public: | 388 public: |
366 // Create a string builder with a buffer of the given size. The | 389 // Create a string builder with a buffer of the given size. The |
367 // buffer is allocated through NewArray<char> and must be | 390 // buffer is allocated through NewArray<char> and must be |
368 // deallocated by the caller of Finalize(). | 391 // deallocated by the caller of Finalize(). |
369 explicit StringBuilder(int size); | 392 explicit StringBuilder(int size); |
370 | 393 |
371 StringBuilder(char* buffer, int size) | 394 StringBuilder(char* buffer, int size) |
372 : buffer_(buffer), size_(size), position_(0) { } | 395 : buffer_(buffer, size), position_(0) { } |
373 | 396 |
374 ~StringBuilder() { if (!is_finalized()) Finalize(); } | 397 ~StringBuilder() { if (!is_finalized()) Finalize(); } |
375 | 398 |
376 int size() const { return size_; } | 399 int size() const { return buffer_.length(); } |
377 | 400 |
378 // Get the current position in the builder. | 401 // Get the current position in the builder. |
379 int position() const { | 402 int position() const { |
380 ASSERT(!is_finalized()); | 403 ASSERT(!is_finalized()); |
381 return position_; | 404 return position_; |
382 } | 405 } |
383 | 406 |
384 // Reset the position. | 407 // Reset the position. |
385 void Reset() { position_ = 0; } | 408 void Reset() { position_ = 0; } |
386 | 409 |
387 // Add a single character to the builder. It is not allowed to add | 410 // Add a single character to the builder. It is not allowed to add |
388 // 0-characters; use the Finalize() method to terminate the string | 411 // 0-characters; use the Finalize() method to terminate the string |
389 // instead. | 412 // instead. |
390 void AddCharacter(char c) { | 413 void AddCharacter(char c) { |
391 ASSERT(c != '\0'); | 414 ASSERT(c != '\0'); |
392 ASSERT(!is_finalized() && position_ < size_); | 415 ASSERT(!is_finalized() && position_ < buffer_.length()); |
393 buffer_[position_++] = c; | 416 buffer_[position_++] = c; |
394 } | 417 } |
395 | 418 |
396 // Add an entire string to the builder. Uses strlen() internally to | 419 // Add an entire string to the builder. Uses strlen() internally to |
397 // compute the length of the input string. | 420 // compute the length of the input string. |
398 void AddString(const char* s); | 421 void AddString(const char* s); |
399 | 422 |
400 // Add the first 'n' characters of the given string 's' to the | 423 // Add the first 'n' characters of the given string 's' to the |
401 // builder. The input string must have enough characters. | 424 // builder. The input string must have enough characters. |
402 void AddSubstring(const char* s, int n); | 425 void AddSubstring(const char* s, int n); |
403 | 426 |
404 // Add formatted contents to the builder just like printf(). | 427 // Add formatted contents to the builder just like printf(). |
405 void AddFormatted(const char* format, ...); | 428 void AddFormatted(const char* format, ...); |
406 | 429 |
407 // Add character padding to the builder. If count is non-positive, | 430 // Add character padding to the builder. If count is non-positive, |
408 // nothing is added to the builder. | 431 // nothing is added to the builder. |
409 void AddPadding(char c, int count); | 432 void AddPadding(char c, int count); |
410 | 433 |
411 // Finalize the string by 0-terminating it and returning the buffer. | 434 // Finalize the string by 0-terminating it and returning the buffer. |
412 char* Finalize(); | 435 char* Finalize(); |
413 | 436 |
414 private: | 437 private: |
415 char* buffer_; | 438 Vector<char> buffer_; |
416 int size_; | |
417 int position_; | 439 int position_; |
418 | 440 |
419 bool is_finalized() const { return position_ < 0; } | 441 bool is_finalized() const { return position_ < 0; } |
420 | 442 |
421 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); | 443 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); |
422 }; | 444 }; |
423 | 445 |
424 } } // namespace v8::internal | 446 } } // namespace v8::internal |
425 | 447 |
426 #endif // V8_UTILS_H_ | 448 #endif // V8_UTILS_H_ |
OLD | NEW |