OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 804 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 public: | 815 public: |
816 int length() { return 0; } | 816 int length() { return 0; } |
817 ElementType& operator[](int i) { | 817 ElementType& operator[](int i) { |
818 UNREACHABLE(); | 818 UNREACHABLE(); |
819 static ElementType t = 0; | 819 static ElementType t = 0; |
820 return t; | 820 return t; |
821 } | 821 } |
822 }; | 822 }; |
823 | 823 |
824 | 824 |
| 825 // Helper class for building result strings in a character buffer. The |
| 826 // purpose of the class is to use safe operations that checks the |
| 827 // buffer bounds on all operations in debug mode. |
| 828 // This simple base class does not allow formatted output. |
| 829 class SimpleStringBuilder { |
| 830 public: |
| 831 // Create a string builder with a buffer of the given size. The |
| 832 // buffer is allocated through NewArray<char> and must be |
| 833 // deallocated by the caller of Finalize(). |
| 834 explicit SimpleStringBuilder(int size); |
| 835 |
| 836 SimpleStringBuilder(char* buffer, int size) |
| 837 : buffer_(buffer, size), position_(0) { } |
| 838 |
| 839 ~SimpleStringBuilder() { if (!is_finalized()) Finalize(); } |
| 840 |
| 841 int size() const { return buffer_.length(); } |
| 842 |
| 843 // Get the current position in the builder. |
| 844 int position() const { |
| 845 ASSERT(!is_finalized()); |
| 846 return position_; |
| 847 } |
| 848 |
| 849 // Reset the position. |
| 850 void Reset() { position_ = 0; } |
| 851 |
| 852 // Add a single character to the builder. It is not allowed to add |
| 853 // 0-characters; use the Finalize() method to terminate the string |
| 854 // instead. |
| 855 void AddCharacter(char c) { |
| 856 ASSERT(c != '\0'); |
| 857 ASSERT(!is_finalized() && position_ < buffer_.length()); |
| 858 buffer_[position_++] = c; |
| 859 } |
| 860 |
| 861 // Add an entire string to the builder. Uses strlen() internally to |
| 862 // compute the length of the input string. |
| 863 void AddString(const char* s); |
| 864 |
| 865 // Add the first 'n' characters of the given string 's' to the |
| 866 // builder. The input string must have enough characters. |
| 867 void AddSubstring(const char* s, int n); |
| 868 |
| 869 // Add character padding to the builder. If count is non-positive, |
| 870 // nothing is added to the builder. |
| 871 void AddPadding(char c, int count); |
| 872 |
| 873 // Add the decimal representation of the value. |
| 874 void AddDecimalInteger(int value); |
| 875 |
| 876 // Finalize the string by 0-terminating it and returning the buffer. |
| 877 char* Finalize(); |
| 878 |
| 879 protected: |
| 880 Vector<char> buffer_; |
| 881 int position_; |
| 882 |
| 883 bool is_finalized() const { return position_ < 0; } |
| 884 private: |
| 885 DISALLOW_IMPLICIT_CONSTRUCTORS(SimpleStringBuilder); |
| 886 }; |
| 887 |
825 } } // namespace v8::internal | 888 } } // namespace v8::internal |
826 | 889 |
827 #endif // V8_UTILS_H_ | 890 #endif // V8_UTILS_H_ |
OLD | NEW |