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