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 
24 namespace android {
25 
26 class String8;
27 class Parcel;
28 
29 struct AString {
30     AString();
31     AString(const char *s);  // NOLINT, implicit conversion
32     AString(const char *s, size_t size);
33     AString(const String8 &from);  // NOLINT, implicit conversion
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 
appendAString52     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 #ifndef __ANDROID_VNDK__
93     static AString FromParcel(const Parcel &parcel);
94     status_t writeToParcel(Parcel *parcel) const;
95 #endif
96 
97 private:
98     constexpr static const char *kEmptyString = "";
99 
100     char *mData;
101     size_t mSize;
102     size_t mAllocSize;
103 
104     void makeMutable();
105 };
106 
107 AString AStringPrintf(const char *format, ...);
108 
109 }  // namespace android
110 
111 #endif  // A_STRING_H_
112 
113