| OLD | NEW |
| 1 // Copyright 2010 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 183 |
| 184 | 184 |
| 185 // Simple support to read a file into a 0-terminated C-string. | 185 // Simple support to read a file into a 0-terminated C-string. |
| 186 // The returned buffer must be freed by the caller. | 186 // The returned buffer must be freed by the caller. |
| 187 // On return, *exits tells whether the file existed. | 187 // On return, *exits tells whether the file existed. |
| 188 Vector<const char> ReadFile(const char* filename, | 188 Vector<const char> ReadFile(const char* filename, |
| 189 bool* exists, | 189 bool* exists, |
| 190 bool verbose = true); | 190 bool verbose = true); |
| 191 | 191 |
| 192 | 192 |
| 193 // Helper class for building result strings in a character buffer. The | |
| 194 // purpose of the class is to use safe operations that checks the | |
| 195 // buffer bounds on all operations in debug mode. | |
| 196 class StringBuilder { | |
| 197 public: | |
| 198 // Create a string builder with a buffer of the given size. The | |
| 199 // buffer is allocated through NewArray<char> and must be | |
| 200 // deallocated by the caller of Finalize(). | |
| 201 explicit StringBuilder(int size); | |
| 202 | |
| 203 StringBuilder(char* buffer, int size) | |
| 204 : buffer_(buffer, size), position_(0) { } | |
| 205 | |
| 206 ~StringBuilder() { if (!is_finalized()) Finalize(); } | |
| 207 | |
| 208 int size() const { return buffer_.length(); } | |
| 209 | |
| 210 // Get the current position in the builder. | |
| 211 int position() const { | |
| 212 ASSERT(!is_finalized()); | |
| 213 return position_; | |
| 214 } | |
| 215 | |
| 216 // Reset the position. | |
| 217 void Reset() { position_ = 0; } | |
| 218 | |
| 219 // Add a single character to the builder. It is not allowed to add | |
| 220 // 0-characters; use the Finalize() method to terminate the string | |
| 221 // instead. | |
| 222 void AddCharacter(char c) { | |
| 223 ASSERT(c != '\0'); | |
| 224 ASSERT(!is_finalized() && position_ < buffer_.length()); | |
| 225 buffer_[position_++] = c; | |
| 226 } | |
| 227 | |
| 228 // Add an entire string to the builder. Uses strlen() internally to | |
| 229 // compute the length of the input string. | |
| 230 void AddString(const char* s); | |
| 231 | |
| 232 // Add the first 'n' characters of the given string 's' to the | |
| 233 // builder. The input string must have enough characters. | |
| 234 void AddSubstring(const char* s, int n); | |
| 235 | |
| 236 // Add formatted contents to the builder just like printf(). | |
| 237 void AddFormatted(const char* format, ...); | |
| 238 | |
| 239 // Add formatted contents like printf based on a va_list. | |
| 240 void AddFormattedList(const char* format, va_list list); | |
| 241 | |
| 242 // Add character padding to the builder. If count is non-positive, | |
| 243 // nothing is added to the builder. | |
| 244 void AddPadding(char c, int count); | |
| 245 | |
| 246 // Finalize the string by 0-terminating it and returning the buffer. | |
| 247 char* Finalize(); | |
| 248 | |
| 249 private: | |
| 250 Vector<char> buffer_; | |
| 251 int position_; | |
| 252 | |
| 253 bool is_finalized() const { return position_ < 0; } | |
| 254 | |
| 255 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); | |
| 256 }; | |
| 257 | |
| 258 | 193 |
| 259 // Copy from ASCII/16bit chars to ASCII/16bit chars. | 194 // Copy from ASCII/16bit chars to ASCII/16bit chars. |
| 260 template <typename sourcechar, typename sinkchar> | 195 template <typename sourcechar, typename sinkchar> |
| 261 static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { | 196 static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { |
| 262 sinkchar* limit = dest + chars; | 197 sinkchar* limit = dest + chars; |
| 263 #ifdef V8_HOST_CAN_READ_UNALIGNED | 198 #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 264 if (sizeof(*dest) == sizeof(*src)) { | 199 if (sizeof(*dest) == sizeof(*src)) { |
| 265 if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) { | 200 if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) { |
| 266 OS::MemCopy(dest, src, chars * sizeof(*dest)); | 201 OS::MemCopy(dest, src, chars * sizeof(*dest)); |
| 267 return; | 202 return; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 void Init(const char* filename); | 241 void Init(const char* filename); |
| 307 | 242 |
| 308 char* filename_; | 243 char* filename_; |
| 309 OS::MemoryMappedFile* file_; | 244 OS::MemoryMappedFile* file_; |
| 310 | 245 |
| 311 const char* data_; | 246 const char* data_; |
| 312 size_t length_; | 247 size_t length_; |
| 313 bool remove_file_on_cleanup_; | 248 bool remove_file_on_cleanup_; |
| 314 }; | 249 }; |
| 315 | 250 |
| 251 class StringBuilder : public SimpleStringBuilder { |
| 252 public: |
| 253 explicit StringBuilder(int size) : SimpleStringBuilder(size) { } |
| 254 StringBuilder(char* buffer, int size) : SimpleStringBuilder(buffer, size) { } |
| 255 |
| 256 // Add formatted contents to the builder just like printf(). |
| 257 void AddFormatted(const char* format, ...); |
| 258 |
| 259 // Add formatted contents like printf based on a va_list. |
| 260 void AddFormattedList(const char* format, va_list list); |
| 261 private: |
| 262 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); |
| 263 }; |
| 316 | 264 |
| 317 } } // namespace v8::internal | 265 } } // namespace v8::internal |
| 318 | 266 |
| 319 #endif // V8_V8UTILS_H_ | 267 #endif // V8_V8UTILS_H_ |
| OLD | NEW |