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