AString.h revision 8accee4f0e94f19866d260be6eecd6c219eb4982
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef A_STRING_H_
18
19#define A_STRING_H_
20
21#include <utils/Errors.h>
22#include <sys/types.h>
23
24namespace android {
25
26struct String8;
27struct Parcel;
28
29struct AString {
30    AString();
31    AString(const char *s);
32    AString(const char *s, size_t size);
33    AString(const String8 &from);
34    AString(const AString &from);
35    AString(const AString &from, size_t offset, size_t n);
36    ~AString();
37
38    AString &operator=(const AString &from);
39    void setTo(const char *s);
40    void setTo(const char *s, size_t size);
41    void setTo(const AString &from, size_t offset, size_t n);
42
43    size_t size() const;
44    const char *c_str() const;
45
46    bool empty() const;
47
48    void clear();
49    void trim();
50    void erase(size_t start, size_t n);
51
52    void append(char c) { append(&c, 1); }
53    void append(const char *s);
54    void append(const char *s, size_t size);
55    void append(const AString &from);
56    void append(const AString &from, size_t offset, size_t n);
57    void append(int x);
58    void append(unsigned x);
59    void append(long x);
60    void append(unsigned long x);
61    void append(long long x);
62    void append(unsigned long long x);
63    void append(float x);
64    void append(double x);
65    void append(void *x);
66
67    void insert(const AString &from, size_t insertionPos);
68    void insert(const char *from, size_t size, size_t insertionPos);
69
70    ssize_t find(const char *substring, size_t start = 0) const;
71
72    size_t hash() const;
73
74    bool operator==(const AString &other) const;
75    bool operator!=(const AString &other) const {
76        return !operator==(other);
77    }
78    bool operator<(const AString &other) const;
79    bool operator>(const AString &other) const;
80
81    int compare(const AString &other) const;
82    int compareIgnoreCase(const AString &other) const;
83
84    bool equalsIgnoreCase(const AString &other) const;
85    bool startsWith(const char *prefix) const;
86    bool endsWith(const char *suffix) const;
87    bool startsWithIgnoreCase(const char *prefix) const;
88    bool endsWithIgnoreCase(const char *suffix) const;
89
90    void tolower();
91
92    static AString FromParcel(const Parcel &parcel);
93    status_t writeToParcel(Parcel *parcel) const;
94
95private:
96    static const char *kEmptyString;
97
98    char *mData;
99    size_t mSize;
100    size_t mAllocSize;
101
102    void makeMutable();
103};
104
105AString StringPrintf(const char *format, ...);
106
107}  // namespace android
108
109#endif  // A_STRING_H_
110
111