AString.h revision 3c1da7224155516a08d94598eb64b64204bf10f8
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 <sys/types.h>
22
23namespace android {
24
25struct String8;
26
27struct AString {
28    AString();
29    AString(const char *s);
30    AString(const char *s, size_t size);
31    AString(const String8 &from);
32    AString(const AString &from);
33    AString(const AString &from, size_t offset, size_t n);
34    ~AString();
35
36    AString &operator=(const AString &from);
37    void setTo(const char *s);
38    void setTo(const char *s, size_t size);
39    void setTo(const AString &from, size_t offset, size_t n);
40
41    size_t size() const;
42    const char *c_str() const;
43
44    bool empty() const;
45
46    void clear();
47    void trim();
48    void erase(size_t start, size_t n);
49
50    void append(char c) { append(&c, 1); }
51    void append(const char *s);
52    void append(const char *s, size_t size);
53    void append(const AString &from);
54    void append(const AString &from, size_t offset, size_t n);
55    void append(int x);
56    void append(unsigned x);
57    void append(long x);
58    void append(unsigned long x);
59    void append(long long x);
60    void append(unsigned long long x);
61    void append(float x);
62    void append(double x);
63    void append(void *x);
64
65    void insert(const AString &from, size_t insertionPos);
66    void insert(const char *from, size_t size, size_t insertionPos);
67
68    ssize_t find(const char *substring, size_t start = 0) const;
69
70    size_t hash() const;
71
72    bool operator==(const AString &other) const;
73    bool operator<(const AString &other) const;
74    bool operator>(const AString &other) const;
75
76    int compare(const AString &other) const;
77
78    bool startsWith(const char *prefix) const;
79    bool endsWith(const char *suffix) const;
80    bool startsWithIgnoreCase(const char *prefix) const;
81    bool endsWithIgnoreCase(const char *suffix) const;
82
83    void tolower();
84
85private:
86    static const char *kEmptyString;
87
88    char *mData;
89    size_t mSize;
90    size_t mAllocSize;
91
92    void makeMutable();
93};
94
95AString StringPrintf(const char *format, ...);
96
97}  // namespace android
98
99#endif  // A_STRING_H_
100
101