1/* -*- c++ -*- */ 2/* 3 * Copyright (C) 2009 The Android Open Source Project 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#ifndef ANDROID_ASTL_STRING__ 31#define ANDROID_ASTL_STRING__ 32 33#include <cstddef> 34 35namespace std { 36 37// Simple string implementation. Its purpose is to be able to compile code that 38// uses the STL and requires std::string. 39// 40// IMPORTANT: 41// . This class it is not fully STL compliant. Some constructors/methods maybe 42// missing, they will be added on demand. 43// . We don't provide a std::basic_string template that std::string extends 44// because we use only char (wchar is not supported on Android). 45// . The memory allocation scheme uses the heap. Since Android has the concept 46// of SharedBuffer, we may, in the future, templatize this class and add an 47// allocation parameter. 48// . The implementation is not optimized in any way (no copy on write support), 49// temporary instance may be expensive. 50// . Currently there is no support for iterators. 51// 52 53class string 54{ 55 public: 56 typedef size_t size_type; 57 typedef char value_type; 58 59 static const size_type npos = static_cast<size_type>(-1); 60 61 // Constructors 62 string(); 63 64 string(const string& str); 65 66 // Construct a string from a source's substring. 67 // @param str The source string. 68 // @param pos The index of the character to start the copy at. 69 // @param n The number of characters to copy. Use string::npos for the 70 // remainder. 71 string(const string& str, size_t pos, size_type n); 72 73 // Same as above but implicitly copy from pos to the end of the str. 74 string(const string& str, size_type pos); 75 76 // Construct a string from a C string. 77 // @param str The source string, must be '\0' terminated. 78 string(const value_type *str); 79 80 // Construct a string from a char array. 81 // @param str The source C string. '\0' are ignored. 82 // @param n The number of characters to copy. 83 string(const value_type *str, size_type n); 84 85 // Construct a string from a repetition of a character. 86 // @param n The number of characters. 87 // @param c The character to use. 88 string(size_t n, char c); 89 90 // Construct a string from a char array. 91 // @param begin The start of the source C string. '\0' are ignored. 92 // @param end The end of source C string. Points just pass the last 93 // character. 94 string(const value_type *begin, const value_type *end); 95 96 ~string(); 97 98 // @return The number of characters in the string, not including any 99 // null-termination. 100 size_type length() const { return mLength; } 101 size_type size() const { return mLength; } 102 103 // @return A pointer to null-terminated contents. 104 const value_type *c_str() const { return mData; } 105 const value_type *data() const { return mData; } 106 107 // Empty the string on return. Release the internal buffer. Length 108 // and capacity are both 0 on return. If you want to keep the 109 // internal buffer around for reuse, call 'erase' instead. 110 void clear(); 111 112 // @return true if the string is empty. 113 bool empty() const { return this->size() == 0; } 114 115 // Remove 'len' characters from the string starting at 'pos'. The 116 // string length is reduced by 'len'. If len is greater or equal 117 // to the number of characters in the string, it is truncated at 118 // 'pos'. If 'pos' is beyond the end of the string, 'erase' does 119 // nothing. Note, regular STL implementations throw a out_of_range 120 // exception in this case. 121 // Internally, the capacity of the buffer remains unchanged. If 122 // you wanted to recover the deleted chars' memory you should call 123 // 'reserve' explicitly (see also 'clear'). 124 // @param pos Index of the first character to remove (default to 0) 125 // @param n Number of characters to delete. (default to remainder) 126 // @return a reference to this string. 127 string& erase(size_type pos = 0, size_type n = npos); 128 129 // @param str The string to be append. 130 // @return A reference to this string. 131 string& operator+=(const string& str) { return this->append(str); } 132 133 // @param str The C string to be append. 134 // @return A reference to this string. 135 string& operator+=(const value_type *str) { return this->append(str); } 136 137 // @param c A character to be append. 138 // @return A reference to this string. 139 string& operator+=(const char c) { this->push_back(c); return *this; } 140 141 // @param c A character to be append. 142 void push_back(const char c); 143 144 // no-op if str is NULL. 145 string& append(const value_type *str); 146 // no-op if str is NULL. n must be >= 0. 147 string& append(const value_type *str, size_type n); 148 // no-op if str is NULL. pos and n must be >= 0. 149 string& append(const value_type *str, size_type pos, size_type n); 150 string& append(const string& str); 151 152 // Comparison. 153 // @return 0 if this==other, < 0 if this < other and > 0 if this > other. 154 // Don't assume the values are -1, 0, 1 155 int compare(const string& other) const; 156 int compare(const value_type *other) const; 157 158 friend bool operator==(const string& left, const string& right); 159 friend bool operator==(const string& left, const value_type *right); 160 friend bool operator==(const value_type *left, const string& right) { return right == left; } 161 friend bool operator!=(const string& left, const string& right) { return !(left == right); } 162 friend bool operator!=(const string& left, const char* right) { return !(left == right); } 163 friend bool operator!=(const value_type *left, const string& right) { return !(left == right); } 164 165 // @return Number of elements for which memory has been allocated. capacity >= size(). 166 size_type capacity() const { return mCapacity; } 167 168 // Change the capacity to new_size. No effect if new_size < size(). 169 // 0 means Shrink to fit. 170 // @param new_size number of character to be allocated. 171 void reserve(size_type new_size = 0); 172 173 // Exchange the content of this with the content of other. 174 // @param other Instance to swap this one with. 175 void swap(string& other); 176 177 // Accessors. 178 // @param pos of the char. No boundary, signed checks are done. 179 // @return a const reference to the char. 180 const char& operator[](const size_type pos) const; 181 182 // @param pos of the char. No boundary, signed checks are done. 183 // @return a reference to the char. 184 char& operator[](const size_type pos); 185 186 // Assignments. 187 string& operator=(const string& str) { return assign(str); } 188 string& operator=(const char* str) { return assign(str); } 189 string& operator=(char c); 190 191 string& assign(const string& str); 192 // Assign a substring of the original. 193 // @param str Original string. 194 // @param pos Index of the start of the copy. 195 // @param n Number of character to be copied. 196 string& assign(const string& str, size_type pos, size_type n); 197 string& assign(const value_type *str); 198 199 // Assign a non-nul terminated array of chars. 200 // @param array Of chars non-nul terminated. 201 // @param len Length of the array. 202 string& assign(const value_type *array, size_type len); 203 204 // Concat. Prefer using += or append. 205 // Uses unnamed object for return value optimization. 206 friend string operator+(const string& left, const string& right) { 207 return string(left).append(right); 208 } 209 friend string operator+(const string& left, const value_type *right) { 210 return string(left).append(right); 211 } 212 friend string operator+(const value_type *left, const string& right) { 213 return string(left).append(right); 214 } 215 friend string operator+(const string& left, char right) { 216 return string(left).operator+=(right); 217 } 218 friend string operator+(char left, const string& right) { 219 return string(&left, 1).append(right); 220 } 221 222 // Find the position of a sub-string. The empty string is always 223 // found at the requested position except when it's beyond the 224 // string's end. 225 // @param str String to locate. 226 // @param pos Index of the character to search from. Default to 0. 227 // @return Index of start of the first occurrence of the 228 // string. string::npos if no occurrence of str was found from the 229 // starting position. 230 size_type find(const string& str, size_type pos = 0) const { 231 return find(str.mData, pos); 232 } 233 234 // Find the position of a C sub-string. The empty string is always 235 // found at the requested position except when it's beyond the 236 // string's end. 237 // @param str C string to locate. 238 // @param pos Index of the character to search from. Default to 0. 239 // @return Index of start of the first occurrence of the 240 // string. string::npos if no occurrence of str was found from the 241 // starting position. 242 size_type find(const value_type *str, size_type pos = 0) const; 243 244 private: 245 bool SafeMalloc(size_type n); 246 void SafeRealloc(size_type n); 247 void SafeFree(value_type *str); 248 void ResetTo(value_type *str); 249 void ConstructEmptyString(); 250 void Constructor(const value_type *str, size_type n); 251 void Constructor(const value_type *str, size_type pos, size_type n); 252 void Constructor(size_type num, char c); 253 void DeleteSafe(); 254 void Append(const value_type *str, size_type len); 255 256 value_type *mData; // pointer to the buffer 257 size_type mCapacity; // size of the buffer. 258 size_type mLength; // len of the string excl. null-terminator. 259}; 260 261} // namespace std 262 263#endif // ANDROID_ASTL_STRING__ 264