string revision 6309a85f3be27b49451e37d0b31446e0cf727f23
18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/* -*- c++ -*- */ 28e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/* 38e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Copyright (C) 2009 The Android Open Source Project 48e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * All rights reserved. 58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without 78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions 88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met: 98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * * Redistributions of source code must retain the above copyright 108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * notice, this list of conditions and the following disclaimer. 11635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project * * Redistributions in binary form must reproduce the above copyright 128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * notice, this list of conditions and the following disclaimer in 138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * the documentation and/or other materials provided with the 148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * distribution. 158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * SUCH DAMAGE. 288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */ 298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#ifndef ANDROID_ASTL_STRING__ 318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#define ANDROID_ASTL_STRING__ 328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include <cstddef> 348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include <iterator> 358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include <char_traits.h> 368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectnamespace std { 388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectclass ostream; 408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// Simple string implementation. Its purpose is to be able to compile code that 428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// uses the STL and requires std::string. 438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// 448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// IMPORTANT: 458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// . This class it is not fully STL compliant. Some constructors/methods maybe 468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// missing, they will be added on demand. 478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// . We don't provide a std::basic_string template that std::string extends 488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// because we use only char (wchar is not supported on Android). 498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// . The memory allocation scheme uses the heap. Since Android has the concept 508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// of SharedBuffer, we may, in the future, templatize this class and add an 518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// allocation parameter. 528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// . The implementation is not optimized in any way (no copy on write support), 538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// temporary instance may be expensive. 548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// . Currently there is limited support for iterators. 558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project// 568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectclass string 588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{ 598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project public: 608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef char_traits<char> traits_type; 618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef traits_type::char_type value_type; 628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef size_t size_type; 638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef ptrdiff_t difference_type; 648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef value_type& reference; 658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef const value_type& const_reference; 668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef value_type* pointer; 678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef const value_type* const_pointer; 688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef __wrapper_iterator<pointer,string> iterator; 698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project typedef __wrapper_iterator<const_pointer,string> const_iterator; 708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project static const size_type npos = static_cast<size_type>(-1); 728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Constructors 748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string(); 758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string(const string& str); 778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Construct a string from a source's substring. 798e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param str The source string. 808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param pos The index of the character to start the copy at. 818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param n The number of characters to copy. Use string::npos for the 828e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // remainder. 838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string(const string& str, size_t pos, size_type n); 848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Same as above but implicitly copy from pos to the end of the str. 868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string(const string& str, size_type pos); 87635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project 888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Construct a string from a C string. 898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param str The source string, must be '\0' terminated. 908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string(const value_type *str); 918e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Construct a string from a char array. 938e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param str The source C string. '\0' are ignored. 948e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param n The number of characters to copy. 958e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string(const value_type *str, size_type n); 968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Construct a string from a repetition of a character. 988e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param n The number of characters. 998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param c The character to use. 1008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string(size_t n, char c); 1018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Construct a string from a char array. 1038e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param begin The start of the source C string. '\0' are ignored. 1048e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param end The end of source C string. Points just pass the last 1058e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // character. 1068e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string(const value_type *begin, const value_type *end); 1078e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1088e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project ~string(); 1098e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return The number of characters in the string, not including any 1118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // null-termination. 1128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project size_type length() const { return mLength; } 1138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project size_type size() const { return mLength; } 1148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return A pointer to null-terminated contents. 1168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project const value_type *c_str() const { return mData; } 1178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project const value_type *data() const { return mData; } 1188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Empty the string on return. Release the internal buffer. Length 1208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // and capacity are both 0 on return. If you want to keep the 1218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // internal buffer around for reuse, call 'erase' instead. 1228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project void clear(); 1238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return true if the string is empty. 1258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project bool empty() const { return this->size() == 0; } 1268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Remove 'len' characters from the string starting at 'pos'. The 1288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // string length is reduced by 'len'. If len is greater or equal 1298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // to the number of characters in the string, it is truncated at 1308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // 'pos'. If 'pos' is beyond the end of the string, 'erase' does 1318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // nothing. Note, regular STL implementations throw a out_of_range 1328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // exception in this case. 1338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Internally, the capacity of the buffer remains unchanged. If 1348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // you wanted to recover the deleted chars' memory you should call 1358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // 'reserve' explicitly (see also 'clear'). 1368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param pos Index of the first character to remove (default to 0) 1378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param n Number of characters to delete. (default to remainder) 1388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return a reference to this string. 1398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& erase(size_type pos = 0, size_type n = npos); 1408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param str The string to be append. 1428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return A reference to this string. 1438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& operator+=(const string& str) { return this->append(str); } 1448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param str The C string to be append. 1468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return A reference to this string. 1478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& operator+=(const value_type *str) { return this->append(str); } 1488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param c A character to be append. 1508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return A reference to this string. 1518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& operator+=(const char c) { this->push_back(c); return *this; } 1528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param c A character to be append. 1548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project void push_back(const char c); 1558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // no-op if str is NULL. 1578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& append(const value_type *str); 1588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // no-op if str is NULL. n must be >= 0. 1598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& append(const value_type *str, size_type n); 1608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // no-op if str is NULL. pos and n must be >= 0. 1618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& append(const value_type *str, size_type pos, size_type n); 1628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& append(const string& str); 1638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Comparison. 1658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return 0 if this==other, < 0 if this < other and > 0 if this > other. 1668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Don't assume the values are -1, 0, 1 1678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project int compare(const string& other) const; 1688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project int compare(const value_type *other) const; 1698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend bool operator==(const string& left, const string& right); 1718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend bool operator==(const string& left, const value_type *right); 1728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend bool operator==(const value_type *left, const string& right) { return right == left; } 1738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend bool operator!=(const string& left, const string& right) { return !(left == right); } 1748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend bool operator!=(const string& left, const char* right) { return !(left == right); } 1758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend bool operator!=(const value_type *left, const string& right) { return !(left == right); } 1768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return Number of elements for which memory has been allocated. capacity >= size(). 1788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project size_type capacity() const { return mCapacity; } 1798e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Change the capacity to new_size. No effect if new_size < size(). 1818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // 0 means Shrink to fit. 1828e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param new_size number of character to be allocated. 1838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project void reserve(size_type new_size = 0); 1848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Exchange the content of this with the content of other. 1868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param other Instance to swap this one with. 1878e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project void swap(string& other); 1888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Accessors. 1908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param pos of the char. No boundary, signed checks are done. 1918e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return a const reference to the char. 1928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project const char& operator[](const size_type pos) const; 1938e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1948e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param pos of the char. No boundary, signed checks are done. 1958e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return a reference to the char. 1968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project char& operator[](const size_type pos); 1978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 1988e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // 'at' is similar to operator[] except that it does check bounds. 1998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project const char& at(const size_type pos) const; 2008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project char& at(const size_type pos); 2018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Assignments. 2038e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& operator=(const string& str) { return assign(str); } 2048e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& operator=(const char* str) { return assign(str); } 2058e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& operator=(char c); 2068e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2078e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& assign(const string& str); 2088e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Assign a substring of the original. 2098e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param str Original string. 2108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param pos Index of the start of the copy. 2118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param n Number of character to be copied. 2128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& assign(const string& str, size_type pos, size_type n); 2138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& assign(const value_type *str); 2148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Assign a non-nul terminated array of chars. 2168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param array Of chars non-nul terminated. 2178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param len Length of the array. 2188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project string& assign(const value_type *array, size_type len); 2198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Concat. Prefer using += or append. 2218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Uses unnamed object for return value optimization. 2228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend string operator+(const string& left, const string& right) { 2238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project return string(left).append(right); 2248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project } 2258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend string operator+(const string& left, const value_type *right) { 2268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project return string(left).append(right); 2278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project } 2288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend string operator+(const value_type *left, const string& right) { 2298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project return string(left).append(right); 2308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project } 2318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend string operator+(const string& left, char right) { 2328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project return string(left).operator+=(right); 2338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project } 2348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project friend string operator+(char left, const string& right) { 2358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project return string(&left, 1).append(right); 2368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project } 2378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Find the position of a sub-string. The empty string is always 2398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // found at the requested position except when it's beyond the 2408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // string's end. 2418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param str String to locate. 2428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param pos Index of the character to search from. Default to 0. 2438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return Index of start of the first occurrence of the 2448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // string. string::npos if no occurrence of str was found from the 2458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // starting position. 2468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project size_type find(const string& str, size_type pos = 0) const { 247231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block return find(str.mData, pos); 2488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project } 2498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Find the position of a C sub-string. The empty string is always 2518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // found at the requested position except when it's beyond the 2528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // string's end. 2538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param str C string to locate. 2548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @param pos Index of the character to search from. Default to 0. 2558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return Index of start of the first occurrence of the 2568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // string. string::npos if no occurrence of str was found from the 2578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // starting position. 2588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project size_type find(const value_type *str, size_type pos = 0) const; 2598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Find the lowest position xpos, if possible, such that: 2618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // pos <= xpos && xpos < size() 2628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // at(xpos) == c 2638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return xpos if it exists, npos otherwise. 2648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project size_type find(const value_type c, size_type pos = 0) const { 2658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project return find_first_of(c, pos); 2668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project } 2678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // Find the highest position xpos, if possible, such that: 2698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // xpos <= pos && xpos < size() 2708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // at(xpos) == c 2718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project // @return xpos if it exists, npos otherwise. 272d0825bca7fe65beaee391d30da42e937db621564Steve Block size_type rfind(const value_type c, size_type pos = npos) const { 2738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project return find_last_of(c, pos); 274 } 275 276 // Iterators 277 iterator begin() {return iterator(mData);} 278 const_iterator begin() const {return const_iterator(mData);} 279 iterator end() {return iterator(mData + mLength);} 280 const_iterator end() const {return const_iterator(mData + mLength);} 281 282 // @return the substring [pos, pos + n]. 283 // Requires pos <= size(). If n > size() - pos, size() - pos is used. 284 string substr(size_type pos = 0, size_type n = npos) const; 285 286 // Find char methods. Return the position or npos if the char was not found. 287 size_type find_first_of(value_type c, size_type pos = 0) const; 288 size_type find_last_of(value_type c, size_type pos = npos) const; 289 size_type find_first_not_of(value_type c, size_type pos = 0) const; 290 size_type find_last_not_of(value_type c, size_type pos = npos) const; 291 292 private: 293 bool SafeMalloc(size_type n); 294 void SafeRealloc(size_type n); 295 void SafeFree(value_type *str); 296 void ResetTo(value_type *str); 297 void ConstructEmptyString(); 298 void Constructor(const value_type *str, size_type n); 299 void Constructor(const value_type *str, size_type pos, size_type n); 300 void Constructor(size_type num, char c); 301 void DeleteSafe(); 302 void Append(const value_type *str, size_type len); 303 304 value_type *mData; // pointer to the buffer 305 size_type mCapacity; // size of the buffer. 306 size_type mLength; // len of the string excl. null-terminator. 307}; 308 309// Comparaison: 310bool operator<(const string& lhs, const string& rhs); 311bool operator<=(const string& lhs, const string& rhs); 312bool operator>(const string& lhs, const string& rhs); 313bool operator>=(const string& lhs, const string& rhs); 314 315// Swap 316void swap(string& lhs, string& rhs); 317 318// I/O 319ostream& operator<<(ostream& os, const string& str); 320 321} // namespace std 322 323#endif // ANDROID_ASTL_STRING__ 324