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