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 #define LOG_TAG "AString"
18 #include <utils/Log.h>
19 
20 #include <ctype.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <utils/String8.h>
27 #include "ADebug.h"
28 #include "AString.h"
29 
30 #ifndef __ANDROID_VNDK__
31 #include <binder/Parcel.h>
32 #endif
33 
34 namespace android {
35 
36 // static
37 constexpr const char *AString::kEmptyString;
38 
AString()39 AString::AString()
40     : mData((char *)kEmptyString),
41       mSize(0),
42       mAllocSize(1) {
43 }
44 
AString(const char * s)45 AString::AString(const char *s)
46     : mData(NULL),
47       mSize(0),
48       mAllocSize(1) {
49     if (!s) {
50         ALOGW("ctor got NULL, using empty string instead");
51         clear();
52     } else {
53         setTo(s);
54     }
55 }
56 
AString(const char * s,size_t size)57 AString::AString(const char *s, size_t size)
58     : mData(NULL),
59       mSize(0),
60       mAllocSize(1) {
61     if (!s) {
62         ALOGW("ctor got NULL, using empty string instead");
63         clear();
64     } else {
65         setTo(s, size);
66     }
67 }
68 
AString(const String8 & from)69 AString::AString(const String8 &from)
70     : mData(NULL),
71       mSize(0),
72       mAllocSize(1) {
73     setTo(from.string(), from.length());
74 }
75 
AString(const AString & from)76 AString::AString(const AString &from)
77     : mData(NULL),
78       mSize(0),
79       mAllocSize(1) {
80     setTo(from, 0, from.size());
81 }
82 
AString(const AString & from,size_t offset,size_t n)83 AString::AString(const AString &from, size_t offset, size_t n)
84     : mData(NULL),
85       mSize(0),
86       mAllocSize(1) {
87     setTo(from, offset, n);
88 }
89 
~AString()90 AString::~AString() {
91     clear();
92 }
93 
operator =(const AString & from)94 AString &AString::operator=(const AString &from) {
95     if (&from != this) {
96         setTo(from, 0, from.size());
97     }
98 
99     return *this;
100 }
101 
size() const102 size_t AString::size() const {
103     return mSize;
104 }
105 
c_str() const106 const char *AString::c_str() const {
107     return mData;
108 }
109 
empty() const110 bool AString::empty() const {
111     return mSize == 0;
112 }
113 
setTo(const char * s)114 void AString::setTo(const char *s) {
115     setTo(s, strlen(s));
116 }
117 
setTo(const char * s,size_t size)118 void AString::setTo(const char *s, size_t size) {
119     clear();
120     append(s, size);
121 }
122 
setTo(const AString & from,size_t offset,size_t n)123 void AString::setTo(const AString &from, size_t offset, size_t n) {
124     CHECK(&from != this);
125 
126     clear();
127     setTo(from.mData + offset, n);
128 }
129 
clear()130 void AString::clear() {
131     if (mData != kEmptyString) {
132         free(mData);
133         mData = (char *)kEmptyString;
134     }
135     mSize = 0;
136     mAllocSize = 1;
137 }
138 
hash() const139 size_t AString::hash() const {
140     size_t x = 0;
141     for (size_t i = 0; i < mSize; ++i) {
142         x = (x * 31) + mData[i];
143     }
144 
145     return x;
146 }
147 
operator ==(const AString & other) const148 bool AString::operator==(const AString &other) const {
149     return mSize == other.mSize && !memcmp(mData, other.mData, mSize);
150 }
151 
trim()152 void AString::trim() {
153     makeMutable();
154 
155     size_t i = 0;
156     while (i < mSize && isspace(mData[i])) {
157         ++i;
158     }
159 
160     size_t j = mSize;
161     while (j > i && isspace(mData[j - 1])) {
162         --j;
163     }
164 
165     memmove(mData, &mData[i], j - i);
166     mSize = j - i;
167     mData[mSize] = '\0';
168 }
169 
erase(size_t start,size_t n)170 void AString::erase(size_t start, size_t n) {
171     CHECK_LT(start, mSize);
172     CHECK_LE(start + n, mSize);
173 
174     makeMutable();
175 
176     memmove(&mData[start], &mData[start + n], mSize - start - n);
177     mSize -= n;
178     mData[mSize] = '\0';
179 }
180 
makeMutable()181 void AString::makeMutable() {
182     if (mData == kEmptyString) {
183         mData = strdup(kEmptyString);
184     }
185 }
186 
append(const char * s)187 void AString::append(const char *s) {
188     append(s, strlen(s));
189 }
190 
append(const char * s,size_t size)191 void AString::append(const char *s, size_t size) {
192     makeMutable();
193 
194     if (mSize + size + 1 > mAllocSize) {
195         mAllocSize = (mAllocSize + size + 31) & -32;
196         mData = (char *)realloc(mData, mAllocSize);
197         CHECK(mData != NULL);
198     }
199 
200     memcpy(&mData[mSize], s, size);
201     mSize += size;
202     mData[mSize] = '\0';
203 }
204 
append(const AString & from)205 void AString::append(const AString &from) {
206     append(from.c_str(), from.size());
207 }
208 
append(const AString & from,size_t offset,size_t n)209 void AString::append(const AString &from, size_t offset, size_t n) {
210     append(from.c_str() + offset, n);
211 }
212 
append(int x)213 void AString::append(int x) {
214     char s[16];
215     int result = snprintf(s, sizeof(s), "%d", x);
216     CHECK((result > 0) && ((size_t) result) < sizeof(s));
217     append(s);
218 }
219 
append(unsigned x)220 void AString::append(unsigned x) {
221     char s[16];
222     int result = snprintf(s, sizeof(s), "%u", x);
223     CHECK((result > 0) && ((size_t) result) < sizeof(s));
224     append(s);
225 }
226 
append(long x)227 void AString::append(long x) {
228     char s[32];
229     int result = snprintf(s, sizeof(s), "%ld", x);
230     CHECK((result > 0) && ((size_t) result) < sizeof(s));
231     append(s);
232 }
233 
append(unsigned long x)234 void AString::append(unsigned long x) {
235     char s[32];
236     int result = snprintf(s, sizeof(s), "%lu", x);
237     CHECK((result > 0) && ((size_t) result) < sizeof(s));
238     append(s);
239 }
240 
append(long long x)241 void AString::append(long long x) {
242     char s[32];
243     int result = snprintf(s, sizeof(s), "%lld", x);
244     CHECK((result > 0) && ((size_t) result) < sizeof(s));
245     append(s);
246 }
247 
append(unsigned long long x)248 void AString::append(unsigned long long x) {
249     char s[32];
250     int result = snprintf(s, sizeof(s), "%llu", x);
251     CHECK((result > 0) && ((size_t) result) < sizeof(s));
252     append(s);
253 }
254 
append(float x)255 void AString::append(float x) {
256     char s[16];
257     int result = snprintf(s, sizeof(s), "%f", x);
258     CHECK((result > 0) && ((size_t) result) < sizeof(s));
259     append(s);
260 }
261 
append(double x)262 void AString::append(double x) {
263     char s[16];
264     int result = snprintf(s, sizeof(s), "%f", x);
265     CHECK((result > 0) && ((size_t) result) < sizeof(s));
266     append(s);
267 }
268 
append(void * x)269 void AString::append(void *x) {
270     char s[32];
271     int result = snprintf(s, sizeof(s), "%p", x);
272     CHECK((result > 0) && ((size_t) result) < sizeof(s));
273     append(s);
274 }
275 
find(const char * substring,size_t start) const276 ssize_t AString::find(const char *substring, size_t start) const {
277     CHECK_LE(start, size());
278 
279     const char *match = strstr(mData + start, substring);
280 
281     if (match == NULL) {
282         return -1;
283     }
284 
285     return match - mData;
286 }
287 
insert(const AString & from,size_t insertionPos)288 void AString::insert(const AString &from, size_t insertionPos) {
289     insert(from.c_str(), from.size(), insertionPos);
290 }
291 
insert(const char * from,size_t size,size_t insertionPos)292 void AString::insert(const char *from, size_t size, size_t insertionPos) {
293     CHECK_GE(insertionPos, 0u);
294     CHECK_LE(insertionPos, mSize);
295 
296     makeMutable();
297 
298     if (mSize + size + 1 > mAllocSize) {
299         mAllocSize = (mAllocSize + size + 31) & -32;
300         mData = (char *)realloc(mData, mAllocSize);
301         CHECK(mData != NULL);
302     }
303 
304     memmove(&mData[insertionPos + size],
305             &mData[insertionPos], mSize - insertionPos + 1);
306 
307     memcpy(&mData[insertionPos], from, size);
308 
309     mSize += size;
310 }
311 
operator <(const AString & other) const312 bool AString::operator<(const AString &other) const {
313     return compare(other) < 0;
314 }
315 
operator >(const AString & other) const316 bool AString::operator>(const AString &other) const {
317     return compare(other) > 0;
318 }
319 
compare(const AString & other) const320 int AString::compare(const AString &other) const {
321     return strcmp(mData, other.mData);
322 }
323 
compareIgnoreCase(const AString & other) const324 int AString::compareIgnoreCase(const AString &other) const {
325     return strcasecmp(mData, other.mData);
326 }
327 
equalsIgnoreCase(const AString & other) const328 bool AString::equalsIgnoreCase(const AString &other) const {
329     return compareIgnoreCase(other) == 0;
330 }
331 
tolower()332 void AString::tolower() {
333     makeMutable();
334 
335     for (size_t i = 0; i < mSize; ++i) {
336         mData[i] = ::tolower(mData[i]);
337     }
338 }
339 
startsWith(const char * prefix) const340 bool AString::startsWith(const char *prefix) const {
341     return !strncmp(mData, prefix, strlen(prefix));
342 }
343 
endsWith(const char * suffix) const344 bool AString::endsWith(const char *suffix) const {
345     size_t suffixLen = strlen(suffix);
346 
347     if (mSize < suffixLen) {
348         return false;
349     }
350 
351     return !strcmp(mData + mSize - suffixLen, suffix);
352 }
353 
startsWithIgnoreCase(const char * prefix) const354 bool AString::startsWithIgnoreCase(const char *prefix) const {
355     return !strncasecmp(mData, prefix, strlen(prefix));
356 }
357 
endsWithIgnoreCase(const char * suffix) const358 bool AString::endsWithIgnoreCase(const char *suffix) const {
359     size_t suffixLen = strlen(suffix);
360 
361     if (mSize < suffixLen) {
362         return false;
363     }
364 
365     return !strcasecmp(mData + mSize - suffixLen, suffix);
366 }
367 
368 #ifndef __ANDROID_VNDK__
369 // static
FromParcel(const Parcel & parcel)370 AString AString::FromParcel(const Parcel &parcel) {
371     size_t size = static_cast<size_t>(parcel.readInt32());
372     return AString(static_cast<const char *>(parcel.readInplace(size)), size);
373 }
374 
writeToParcel(Parcel * parcel) const375 status_t AString::writeToParcel(Parcel *parcel) const {
376     CHECK_LE(mSize, static_cast<size_t>(INT32_MAX));
377     status_t err = parcel->writeInt32(mSize);
378     if (err == OK) {
379         err = parcel->write(mData, mSize);
380     }
381     return err;
382 }
383 #endif
384 
AStringPrintf(const char * format,...)385 AString AStringPrintf(const char *format, ...) {
386     va_list ap;
387     va_start(ap, format);
388 
389     char *buffer;
390     vasprintf(&buffer, format, ap);
391 
392     va_end(ap);
393 
394     AString result(buffer);
395 
396     free(buffer);
397     buffer = NULL;
398 
399     return result;
400 }
401 
402 }  // namespace android
403 
404