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
330cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania#include <algorithm>
34fe47ae56a8808c836923466e44704db3a8371593Niko Catania#include <cstddef>
3591ea6c037471a1acd97b03c3097223777906f748Nicolas Catania#include <iterator>
36f31fdb2e57186a552855e27f55036369ec1c279aNicolas Catania#include <char_traits.h>
37fe47ae56a8808c836923466e44704db3a8371593Niko Catania
38fe47ae56a8808c836923466e44704db3a8371593Niko Catanianamespace std {
39fe47ae56a8808c836923466e44704db3a8371593Niko Catania
4044d38f3d1ae0fd02030e259c4f93a265f91e96fcNicolas Cataniaclass ostream;
4144d38f3d1ae0fd02030e259c4f93a265f91e96fcNicolas Catania
42fe47ae56a8808c836923466e44704db3a8371593Niko Catania// Simple string implementation. Its purpose is to be able to compile code that
43fe47ae56a8808c836923466e44704db3a8371593Niko Catania// uses the STL and requires std::string.
44fe47ae56a8808c836923466e44704db3a8371593Niko Catania//
45fe47ae56a8808c836923466e44704db3a8371593Niko Catania// IMPORTANT:
46fe47ae56a8808c836923466e44704db3a8371593Niko Catania// . This class it is not fully STL compliant. Some constructors/methods maybe
47fe47ae56a8808c836923466e44704db3a8371593Niko Catania// missing, they will be added on demand.
48fe47ae56a8808c836923466e44704db3a8371593Niko Catania// . We don't provide a std::basic_string template that std::string extends
49fe47ae56a8808c836923466e44704db3a8371593Niko Catania// because we use only char (wchar is not supported on Android).
50fe47ae56a8808c836923466e44704db3a8371593Niko Catania// . The memory allocation scheme uses the heap. Since Android has the concept
51fe47ae56a8808c836923466e44704db3a8371593Niko Catania// of SharedBuffer, we may, in the future, templatize this class and add an
52fe47ae56a8808c836923466e44704db3a8371593Niko Catania// allocation parameter.
53fe47ae56a8808c836923466e44704db3a8371593Niko Catania// . The implementation is not optimized in any way (no copy on write support),
54fe47ae56a8808c836923466e44704db3a8371593Niko Catania// temporary instance may be expensive.
5591ea6c037471a1acd97b03c3097223777906f748Nicolas Catania// . Currently there is limited support for iterators.
56d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania//
57fe47ae56a8808c836923466e44704db3a8371593Niko Catania
58fe47ae56a8808c836923466e44704db3a8371593Niko Cataniaclass string
59fe47ae56a8808c836923466e44704db3a8371593Niko Catania{
60fe47ae56a8808c836923466e44704db3a8371593Niko Catania  public:
61743c6a2694bf16bf29d516c906363199a7bccf86Nicolas Catania    typedef char_traits<char>      traits_type;
62f31fdb2e57186a552855e27f55036369ec1c279aNicolas Catania    typedef traits_type::char_type value_type;
63f31fdb2e57186a552855e27f55036369ec1c279aNicolas Catania    typedef size_t                 size_type;
64f31fdb2e57186a552855e27f55036369ec1c279aNicolas Catania    typedef ptrdiff_t              difference_type;
65f31fdb2e57186a552855e27f55036369ec1c279aNicolas Catania    typedef value_type&            reference;
66f31fdb2e57186a552855e27f55036369ec1c279aNicolas Catania    typedef const value_type&      const_reference;
67f31fdb2e57186a552855e27f55036369ec1c279aNicolas Catania    typedef value_type*            pointer;
68f31fdb2e57186a552855e27f55036369ec1c279aNicolas Catania    typedef const value_type*      const_pointer;
6991ea6c037471a1acd97b03c3097223777906f748Nicolas Catania    typedef __wrapper_iterator<pointer,string>  iterator;
7091ea6c037471a1acd97b03c3097223777906f748Nicolas Catania    typedef __wrapper_iterator<const_pointer,string> const_iterator;
71fe47ae56a8808c836923466e44704db3a8371593Niko Catania
727618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    static const size_type npos = static_cast<size_type>(-1);
73fe47ae56a8808c836923466e44704db3a8371593Niko Catania
74fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Constructors
75fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string();
76fe47ae56a8808c836923466e44704db3a8371593Niko Catania
77fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string(const string& str);
78fe47ae56a8808c836923466e44704db3a8371593Niko Catania
79fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Construct a string from a source's substring.
80fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param str The source string.
81d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param pos The index of the character to start the copy at.
82d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param n The number of characters to copy. Use string::npos for the
83fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // remainder.
84d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    string(const string& str, size_t pos, size_type n);
85fe47ae56a8808c836923466e44704db3a8371593Niko Catania
86d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // Same as above but implicitly copy from pos to the end of the str.
87d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    string(const string& str, size_type pos);
88fe47ae56a8808c836923466e44704db3a8371593Niko Catania
89fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Construct a string from a C string.
90fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param str The source string, must be '\0' terminated.
917618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    string(const value_type *str);
92fe47ae56a8808c836923466e44704db3a8371593Niko Catania
93fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Construct a string from a char array.
94fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param str The source C string. '\0' are ignored.
95d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param n The number of characters to copy.
96d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    string(const value_type *str, size_type n);
97fe47ae56a8808c836923466e44704db3a8371593Niko Catania
98fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Construct a string from a repetition of a character.
99d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param n The number of characters.
100fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param c The character to use.
101d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    string(size_t n, char c);
102fe47ae56a8808c836923466e44704db3a8371593Niko Catania
103fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Construct a string from a char array.
104fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param begin The start of the source C string. '\0' are ignored.
105fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param end The end of source C string. Points just pass the last
106fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // character.
1077618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    string(const value_type *begin, const value_type *end);
108fe47ae56a8808c836923466e44704db3a8371593Niko Catania
109fe47ae56a8808c836923466e44704db3a8371593Niko Catania    ~string();
110fe47ae56a8808c836923466e44704db3a8371593Niko Catania
111fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return The number of characters in the string, not including any
112fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // null-termination.
1137618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    size_type length() const { return mLength; }
1147618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    size_type size() const { return mLength; }
115fe47ae56a8808c836923466e44704db3a8371593Niko Catania
116fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return A pointer to null-terminated contents.
1177618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    const value_type *c_str() const { return mData; }
1187618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    const value_type *data() const { return mData; }
119fe47ae56a8808c836923466e44704db3a8371593Niko Catania
12060fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // Empty the string on return. Release the internal buffer. Length
12160fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // and capacity are both 0 on return. If you want to keep the
12260fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // internal buffer around for reuse, call 'erase' instead.
123fe47ae56a8808c836923466e44704db3a8371593Niko Catania    void clear();
124fe47ae56a8808c836923466e44704db3a8371593Niko Catania
125fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return true if the string is empty.
126fe47ae56a8808c836923466e44704db3a8371593Niko Catania    bool empty() const { return this->size() == 0; }
127fe47ae56a8808c836923466e44704db3a8371593Niko Catania
12860fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // Remove 'len' characters from the string starting at 'pos'. The
12960fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // string length is reduced by 'len'. If len is greater or equal
13060fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // to the number of characters in the string, it is truncated at
13160fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // 'pos'. If 'pos' is beyond the end of the string, 'erase' does
13260fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // nothing. Note, regular STL implementations throw a out_of_range
13360fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // exception in this case.
13460fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // Internally, the capacity of the buffer remains unchanged. If
13560fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // you wanted to recover the deleted chars' memory you should call
13660fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // 'reserve' explicitly (see also 'clear').
13760fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // @param pos Index of the first character to remove (default to 0)
138d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param n Number of characters to delete. (default to remainder)
13960fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // @return a reference to this string.
140d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    string& erase(size_type pos = 0, size_type n = npos);
14160fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania
142fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param str The string to be append.
143fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return A reference to this string.
144fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string& operator+=(const string& str) { return this->append(str); }
145fe47ae56a8808c836923466e44704db3a8371593Niko Catania
146fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param str The C string to be append.
147fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return A reference to this string.
1487618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    string& operator+=(const value_type *str) { return this->append(str); }
149fe47ae56a8808c836923466e44704db3a8371593Niko Catania
150fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param c A character to be append.
151fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return A reference to this string.
152fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string& operator+=(const char c) { this->push_back(c); return *this; }
153fe47ae56a8808c836923466e44704db3a8371593Niko Catania
154fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param c A character to be append.
155fe47ae56a8808c836923466e44704db3a8371593Niko Catania    void push_back(const char c);
156fe47ae56a8808c836923466e44704db3a8371593Niko Catania
157fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // no-op if str is NULL.
1587618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    string& append(const value_type *str);
159d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // no-op if str is NULL. n must be >= 0.
160d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    string& append(const value_type *str, size_type n);
161d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // no-op if str is NULL. pos and n must be >= 0.
162d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    string& append(const value_type *str, size_type pos, size_type n);
163fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string& append(const string& str);
164fe47ae56a8808c836923466e44704db3a8371593Niko Catania
1650cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    template<typename _InputIterator>
1660cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    string& append(_InputIterator first, _InputIterator last);
1670cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania
16860fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // Comparison.
169fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return 0 if this==other, < 0 if this < other and > 0 if this > other.
170fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Don't assume the values are -1, 0, 1
171fe47ae56a8808c836923466e44704db3a8371593Niko Catania    int compare(const string& other) const;
1727618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    int compare(const value_type *other) const;
173fe47ae56a8808c836923466e44704db3a8371593Niko Catania
174fe47ae56a8808c836923466e44704db3a8371593Niko Catania    friend bool operator==(const string& left, const string& right);
1757618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    friend bool operator==(const string& left, const value_type *right);
1767618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    friend bool operator==(const value_type *left, const string& right) { return right == left; }
177fe47ae56a8808c836923466e44704db3a8371593Niko Catania    friend bool operator!=(const string& left, const string& right) { return !(left == right); }
178fe47ae56a8808c836923466e44704db3a8371593Niko Catania    friend bool operator!=(const string& left, const char* right) { return !(left == right); }
1797618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    friend bool operator!=(const value_type *left, const string& right) { return !(left == right); }
180fe47ae56a8808c836923466e44704db3a8371593Niko Catania
181fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return Number of elements for which memory has been allocated. capacity >= size().
1827618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    size_type capacity() const { return mCapacity; }
183fe47ae56a8808c836923466e44704db3a8371593Niko Catania
184d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // Change the capacity to new_size. No effect if new_size < size().
185d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // 0 means Shrink to fit.
186fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param new_size number of character to be allocated.
1877618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    void reserve(size_type new_size = 0);
188fe47ae56a8808c836923466e44704db3a8371593Niko Catania
189fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Exchange the content of this with the content of other.
190fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param other Instance to swap this one with.
191fe47ae56a8808c836923466e44704db3a8371593Niko Catania    void swap(string& other);
192fe47ae56a8808c836923466e44704db3a8371593Niko Catania
193fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Accessors.
194d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param pos of the char. No boundary, signed checks are done.
195fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return a const reference to the char.
196d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    const char& operator[](const size_type pos) const;
197d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania
198d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param pos of the char. No boundary, signed checks are done.
199fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @return a reference to the char.
200d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    char& operator[](const size_type pos);
201fe47ae56a8808c836923466e44704db3a8371593Niko Catania
20274a6fdea77d52a17be4bc38831fe02a31cefbf34Nicolas Catania    // 'at' is similar to operator[] except that it does check bounds.
20374a6fdea77d52a17be4bc38831fe02a31cefbf34Nicolas Catania    const char& at(const size_type pos) const;
20474a6fdea77d52a17be4bc38831fe02a31cefbf34Nicolas Catania    char& at(const size_type pos);
20574a6fdea77d52a17be4bc38831fe02a31cefbf34Nicolas Catania
206fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Assignments.
207fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string& operator=(const string& str) { return assign(str); }
208fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string& operator=(const char* str) { return assign(str); }
209fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string& operator=(char c);
210fe47ae56a8808c836923466e44704db3a8371593Niko Catania
211fe47ae56a8808c836923466e44704db3a8371593Niko Catania    string& assign(const string& str);
212d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // Assign a substring of the original.
213fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param str Original string.
214fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param pos Index of the start of the copy.
215d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param n Number of character to be copied.
216d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    string& assign(const string& str, size_type pos, size_type n);
2177618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    string& assign(const value_type *str);
218d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania
219d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // Assign a non-nul terminated array of chars.
220d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    // @param array Of chars non-nul terminated.
221fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // @param len Length of the array.
2227618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    string& assign(const value_type *array, size_type len);
223fe47ae56a8808c836923466e44704db3a8371593Niko Catania
224fe47ae56a8808c836923466e44704db3a8371593Niko Catania    // Concat. Prefer using += or append.
22560fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // Uses unnamed object for return value optimization.
226fe47ae56a8808c836923466e44704db3a8371593Niko Catania    friend string operator+(const string& left, const string& right) {
227fe47ae56a8808c836923466e44704db3a8371593Niko Catania        return string(left).append(right);
228fe47ae56a8808c836923466e44704db3a8371593Niko Catania    }
2297618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    friend string operator+(const string& left, const value_type *right) {
230fe47ae56a8808c836923466e44704db3a8371593Niko Catania        return string(left).append(right);
231fe47ae56a8808c836923466e44704db3a8371593Niko Catania    }
2327618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    friend string operator+(const value_type *left, const string& right) {
233fe47ae56a8808c836923466e44704db3a8371593Niko Catania        return string(left).append(right);
234fe47ae56a8808c836923466e44704db3a8371593Niko Catania    }
235fe47ae56a8808c836923466e44704db3a8371593Niko Catania    friend string operator+(const string& left, char right) {
236fe47ae56a8808c836923466e44704db3a8371593Niko Catania        return string(left).operator+=(right);
237fe47ae56a8808c836923466e44704db3a8371593Niko Catania    }
238fe47ae56a8808c836923466e44704db3a8371593Niko Catania    friend string operator+(char left, const string& right) {
239fe47ae56a8808c836923466e44704db3a8371593Niko Catania        return string(&left, 1).append(right);
240fe47ae56a8808c836923466e44704db3a8371593Niko Catania    }
241fe47ae56a8808c836923466e44704db3a8371593Niko Catania
242464136e01a1facf09ce3befccbfc04f2d1da8d5bNicolas Catania    // Insert a copy of c before the character referred to by pos.
243464136e01a1facf09ce3befccbfc04f2d1da8d5bNicolas Catania    // @param pos A valid iterator on *this.
244464136e01a1facf09ce3befccbfc04f2d1da8d5bNicolas Catania    // @return An iterator which refers to the copy of the inserted
245464136e01a1facf09ce3befccbfc04f2d1da8d5bNicolas Catania    // character. Because internally some reallocation may occur, the
246464136e01a1facf09ce3befccbfc04f2d1da8d5bNicolas Catania    // returned iterator may be different from 'pos'.
247464136e01a1facf09ce3befccbfc04f2d1da8d5bNicolas Catania    iterator insert(iterator pos, char c);
248464136e01a1facf09ce3befccbfc04f2d1da8d5bNicolas Catania
249d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // Find the position of a sub-string. The empty string is always
250d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // found at the requested position except when it's beyond the
251d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // string's end.
252d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // @param str String to locate.
253d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // @param pos Index of the character to search from. Default to 0.
25460fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // @return Index of start of the first occurrence of the
25560fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // string. string::npos if no occurrence of str was found from the
256d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // starting position.
257d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    size_type find(const string& str, size_type pos = 0) const {
25860fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania        return find(str.mData, pos);
259d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    }
260d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania
261d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // Find the position of a C sub-string. The empty string is always
262d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // found at the requested position except when it's beyond the
263d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // string's end.
264d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // @param str C string to locate.
265d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // @param pos Index of the character to search from. Default to 0.
26660fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // @return Index of start of the first occurrence of the
26760fd0f758a63e74980d712a13c91bc24d98cedc6Nicolas Catania    // string. string::npos if no occurrence of str was found from the
268d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania    // starting position.
2697618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    size_type find(const value_type *str, size_type pos = 0) const;
270d738d268c8f915bde451bba52e0c3996113ba9f0Nicolas Catania
271cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    // Find the lowest position xpos, if possible, such that:
272cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    //   pos <= xpos && xpos < size()
273cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    //   at(xpos) == c
274cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    // @return xpos if it exists, npos otherwise.
275cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    size_type find(const value_type c, size_type pos = 0) const {
276cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania        return find_first_of(c, pos);
277cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    }
278cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania
279cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    // Find the highest position xpos, if possible, such that:
280cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    //   xpos <= pos && xpos < size()
281cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    //   at(xpos) == c
282cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    // @return xpos if it exists, npos otherwise.
283cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    size_type rfind(const value_type c, size_type pos = npos) const {
284cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania        return find_last_of(c, pos);
285cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    }
286cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania
28791ea6c037471a1acd97b03c3097223777906f748Nicolas Catania    // Iterators
28891ea6c037471a1acd97b03c3097223777906f748Nicolas Catania    iterator begin() {return iterator(mData);}
28991ea6c037471a1acd97b03c3097223777906f748Nicolas Catania    const_iterator begin() const {return const_iterator(mData);}
29091ea6c037471a1acd97b03c3097223777906f748Nicolas Catania    iterator end() {return iterator(mData + mLength);}
29191ea6c037471a1acd97b03c3097223777906f748Nicolas Catania    const_iterator end() const {return const_iterator(mData + mLength);}
29291ea6c037471a1acd97b03c3097223777906f748Nicolas Catania
29340d9e34c926f59cae26c4bae4ab55377cf3cdd40Nicolas Catania    // @return the substring [pos, pos + n].
29440d9e34c926f59cae26c4bae4ab55377cf3cdd40Nicolas Catania    // Requires pos <= size(). If n > size() - pos, size() - pos is used.
29540d9e34c926f59cae26c4bae4ab55377cf3cdd40Nicolas Catania    string substr(size_type pos = 0, size_type n = npos) const;
29640d9e34c926f59cae26c4bae4ab55377cf3cdd40Nicolas Catania
297cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    // Find char methods. Return the position or npos if the char was not found.
298cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    size_type find_first_of(value_type c, size_type pos = 0) const;
299cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    size_type find_last_of(value_type c, size_type pos = npos) const;
300cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    size_type find_first_not_of(value_type c, size_type pos = 0) const;
301cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania    size_type find_last_not_of(value_type c, size_type pos = npos) const;
302cb8eb8e1390d1343563a55c117b5c39cfa87fe1dNicolas Catania
303fe47ae56a8808c836923466e44704db3a8371593Niko Catania  private:
304d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    bool SafeMalloc(size_type n);
305d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    void SafeRealloc(size_type n);
3067618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    void SafeFree(value_type *str);
3077618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    void ResetTo(value_type *str);
308fe47ae56a8808c836923466e44704db3a8371593Niko Catania    void ConstructEmptyString();
309d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    void Constructor(const value_type *str, size_type n);
310d881e3f36a074eb9fd8e353fb5e5e3e539f2d460Nicolas Catania    void Constructor(const value_type *str, size_type pos, size_type n);
3117618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    void Constructor(size_type num, char c);
312fe47ae56a8808c836923466e44704db3a8371593Niko Catania    void DeleteSafe();
3137618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    void Append(const value_type *str, size_type len);
314fe47ae56a8808c836923466e44704db3a8371593Niko Catania
3157618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    value_type *mData;  // pointer to the buffer
3167618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    size_type mCapacity;  // size of the buffer.
3177618d7b9a011b1158ef868d6ae3ead242ddaccacNicolas Catania    size_type mLength;  // len of the string excl. null-terminator.
318fe47ae56a8808c836923466e44704db3a8371593Niko Catania};
319fe47ae56a8808c836923466e44704db3a8371593Niko Catania
3206309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Catania// Comparaison:
3216309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Cataniabool operator<(const string& lhs, const string& rhs);
3226309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Cataniabool operator<=(const string& lhs, const string& rhs);
3236309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Cataniabool operator>(const string& lhs, const string& rhs);
3246309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Cataniabool operator>=(const string& lhs, const string& rhs);
3256309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Catania
3266309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Catania// Swap
3276309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Cataniavoid swap(string& lhs, string& rhs);
3286309a85f3be27b49451e37d0b31446e0cf727f23Nicolas Catania
32944d38f3d1ae0fd02030e259c4f93a265f91e96fcNicolas Catania// I/O
33044d38f3d1ae0fd02030e259c4f93a265f91e96fcNicolas Cataniaostream& operator<<(ostream& os, const string& str);
33144d38f3d1ae0fd02030e259c4f93a265f91e96fcNicolas Catania
3320cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania
3330cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania// Specialization of append(iterator, iterator) using string iterators
3340cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania// (const and non const).
3350cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Cataniatemplate<>
3360cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Cataniastring& string::append<__wrapper_iterator<const char *,string> >(
3370cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    __wrapper_iterator<const char *,string> first,
3380cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    __wrapper_iterator<const char *,string> last);
3390cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Cataniatemplate<>
3400cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Cataniastring& string::append<__wrapper_iterator<char *,string> >(
3410cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    __wrapper_iterator<char *,string> first,
3420cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    __wrapper_iterator<char *,string> last);
3430cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania
3440cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania// append(iterator,iterator) default implementation.
3450cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Cataniatemplate<typename _InputIterator>
3460cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Cataniastring& string::append(_InputIterator first, _InputIterator last) {
3470cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    size_type dist = std::distance(first, last);
3480cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    size_type new_len = mLength + dist;
3490cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    if (new_len <= mLength) {
3500cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania        return *this;  // 0 / overflow
3510cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    }
3520cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    reserve(new_len);
3530cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    if (new_len > mCapacity) {
3540cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania        return *this;  // memory allocation failed.
3550cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    }
3560cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    std::copy(first, last, mData + mLength);
3570cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    mLength = new_len;
3580cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    mData[mLength] = '\0';
3590cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania    return *this;
3600cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania}
3610cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania
3620cc3ee31c3cddd2bb5322398d17c388975e96d64Nicolas Catania
363fe47ae56a8808c836923466e44704db3a8371593Niko Catania}  // namespace std
364fe47ae56a8808c836923466e44704db3a8371593Niko Catania
365fe47ae56a8808c836923466e44704db3a8371593Niko Catania#endif  // ANDROID_ASTL_STRING__
366