Line data Source code
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 : #include <ctype.h>
18 : #include <stdarg.h>
19 : #include <stdio.h>
20 : #include <stdlib.h>
21 : #include <string.h>
22 :
23 : #include "ADebug.h"
24 : #include "AString.h"
25 :
26 : namespace stagefright {
27 :
28 : // static
29 : const char *AString::kEmptyString = "";
30 :
31 0 : AString::AString()
32 : : mData((char *)kEmptyString),
33 : mSize(0),
34 0 : mAllocSize(1) {
35 0 : }
36 :
37 0 : AString::AString(const char *s)
38 : : mData(NULL),
39 : mSize(0),
40 0 : mAllocSize(1) {
41 0 : setTo(s);
42 0 : }
43 :
44 0 : AString::AString(const char *s, size_t size)
45 : : mData(NULL),
46 : mSize(0),
47 0 : mAllocSize(1) {
48 0 : setTo(s, size);
49 0 : }
50 :
51 0 : AString::AString(const AString &from)
52 : : mData(NULL),
53 : mSize(0),
54 0 : mAllocSize(1) {
55 0 : setTo(from, 0, from.size());
56 0 : }
57 :
58 0 : AString::AString(const AString &from, size_t offset, size_t n)
59 : : mData(NULL),
60 : mSize(0),
61 0 : mAllocSize(1) {
62 0 : setTo(from, offset, n);
63 0 : }
64 :
65 0 : AString::~AString() {
66 0 : clear();
67 0 : }
68 :
69 0 : AString &AString::operator=(const AString &from) {
70 0 : if (&from != this) {
71 0 : setTo(from, 0, from.size());
72 : }
73 :
74 0 : return *this;
75 : }
76 :
77 0 : size_t AString::size() const {
78 0 : return mSize;
79 : }
80 :
81 0 : const char *AString::c_str() const {
82 0 : return mData;
83 : }
84 :
85 0 : bool AString::empty() const {
86 0 : return mSize == 0;
87 : }
88 :
89 0 : void AString::setTo(const char *s) {
90 0 : setTo(s, strlen(s));
91 0 : }
92 :
93 0 : void AString::setTo(const char *s, size_t size) {
94 0 : clear();
95 0 : append(s, size);
96 0 : }
97 :
98 0 : void AString::setTo(const AString &from, size_t offset, size_t n) {
99 0 : CHECK(&from != this);
100 :
101 0 : clear();
102 0 : setTo(from.mData + offset, n);
103 0 : }
104 :
105 0 : void AString::clear() {
106 0 : if (mData && mData != kEmptyString) {
107 0 : free(mData);
108 0 : mData = NULL;
109 : }
110 :
111 0 : mData = (char *)kEmptyString;
112 0 : mSize = 0;
113 0 : mAllocSize = 1;
114 0 : }
115 :
116 0 : size_t AString::hash() const {
117 0 : size_t x = 0;
118 0 : for (size_t i = 0; i < mSize; ++i) {
119 0 : x = (x * 31) + mData[i];
120 : }
121 :
122 0 : return x;
123 : }
124 :
125 0 : bool AString::operator==(const AString &other) const {
126 0 : return mSize == other.mSize && !memcmp(mData, other.mData, mSize);
127 : }
128 :
129 0 : void AString::trim() {
130 0 : makeMutable();
131 :
132 0 : size_t i = 0;
133 0 : while (i < mSize && isspace(mData[i])) {
134 0 : ++i;
135 : }
136 :
137 0 : size_t j = mSize;
138 0 : while (j > i && isspace(mData[j - 1])) {
139 0 : --j;
140 : }
141 :
142 0 : memmove(mData, &mData[i], j - i);
143 0 : mSize = j - i;
144 0 : mData[mSize] = '\0';
145 0 : }
146 :
147 0 : void AString::erase(size_t start, size_t n) {
148 0 : CHECK_LT(start, mSize);
149 0 : CHECK_LE(start + n, mSize);
150 :
151 0 : makeMutable();
152 :
153 0 : memmove(&mData[start], &mData[start + n], mSize - start - n);
154 0 : mSize -= n;
155 0 : mData[mSize] = '\0';
156 0 : }
157 :
158 0 : void AString::makeMutable() {
159 0 : if (mData == kEmptyString) {
160 0 : mData = strdup(kEmptyString);
161 : }
162 0 : }
163 :
164 0 : void AString::append(const char *s) {
165 0 : append(s, strlen(s));
166 0 : }
167 :
168 0 : void AString::append(const char *s, size_t size) {
169 0 : makeMutable();
170 :
171 0 : if (mSize + size + 1 > mAllocSize) {
172 0 : mAllocSize = (mAllocSize + size + 31) & -32;
173 0 : mData = (char *)realloc(mData, mAllocSize);
174 0 : CHECK(mData != NULL);
175 : }
176 :
177 0 : memcpy(&mData[mSize], s, size);
178 0 : mSize += size;
179 0 : mData[mSize] = '\0';
180 0 : }
181 :
182 0 : void AString::append(const AString &from) {
183 0 : append(from.c_str(), from.size());
184 0 : }
185 :
186 0 : void AString::append(const AString &from, size_t offset, size_t n) {
187 0 : append(from.c_str() + offset, n);
188 0 : }
189 :
190 0 : void AString::append(int x) {
191 : char s[16];
192 0 : sprintf(s, "%d", x);
193 :
194 0 : append(s);
195 0 : }
196 :
197 0 : void AString::append(unsigned x) {
198 : char s[16];
199 0 : sprintf(s, "%u", x);
200 :
201 0 : append(s);
202 0 : }
203 :
204 0 : void AString::append(long x) {
205 : char s[16];
206 0 : sprintf(s, "%ld", x);
207 :
208 0 : append(s);
209 0 : }
210 :
211 0 : void AString::append(unsigned long x) {
212 : char s[16];
213 0 : sprintf(s, "%lu", x);
214 :
215 0 : append(s);
216 0 : }
217 :
218 0 : void AString::append(long long x) {
219 : char s[32];
220 0 : sprintf(s, "%lld", x);
221 :
222 0 : append(s);
223 0 : }
224 :
225 0 : void AString::append(unsigned long long x) {
226 : char s[32];
227 0 : sprintf(s, "%llu", x);
228 :
229 0 : append(s);
230 0 : }
231 :
232 0 : void AString::append(float x) {
233 : char s[16];
234 0 : sprintf(s, "%f", x);
235 :
236 0 : append(s);
237 0 : }
238 :
239 0 : void AString::append(double x) {
240 : char s[16];
241 0 : sprintf(s, "%f", x);
242 :
243 0 : append(s);
244 0 : }
245 :
246 0 : void AString::append(void *x) {
247 : char s[16];
248 0 : sprintf(s, "%p", x);
249 :
250 0 : append(s);
251 0 : }
252 :
253 0 : ssize_t AString::find(const char *substring, size_t start) const {
254 0 : CHECK_LE(start, size());
255 :
256 0 : const char *match = strstr(mData + start, substring);
257 :
258 0 : if (match == NULL) {
259 0 : return -1;
260 : }
261 :
262 0 : return match - mData;
263 : }
264 :
265 0 : void AString::insert(const AString &from, size_t insertionPos) {
266 0 : insert(from.c_str(), from.size(), insertionPos);
267 0 : }
268 :
269 0 : void AString::insert(const char *from, size_t size, size_t insertionPos) {
270 0 : CHECK_GE(insertionPos, 0u);
271 0 : CHECK_LE(insertionPos, mSize);
272 :
273 0 : makeMutable();
274 :
275 0 : if (mSize + size + 1 > mAllocSize) {
276 0 : mAllocSize = (mAllocSize + size + 31) & -32;
277 0 : mData = (char *)realloc(mData, mAllocSize);
278 0 : CHECK(mData != NULL);
279 : }
280 :
281 0 : memmove(&mData[insertionPos + size],
282 0 : &mData[insertionPos], mSize - insertionPos + 1);
283 :
284 0 : memcpy(&mData[insertionPos], from, size);
285 :
286 0 : mSize += size;
287 0 : }
288 :
289 0 : bool AString::operator<(const AString &other) const {
290 0 : return compare(other) < 0;
291 : }
292 :
293 0 : bool AString::operator>(const AString &other) const {
294 0 : return compare(other) > 0;
295 : }
296 :
297 0 : int AString::compare(const AString &other) const {
298 0 : return strcmp(mData, other.mData);
299 : }
300 :
301 0 : void AString::tolower() {
302 0 : makeMutable();
303 :
304 0 : for (size_t i = 0; i < mSize; ++i) {
305 0 : mData[i] = ::tolower(mData[i]);
306 : }
307 0 : }
308 :
309 0 : bool AString::startsWith(const char *prefix) const {
310 0 : return !strncmp(mData, prefix, strlen(prefix));
311 : }
312 :
313 0 : bool AString::endsWith(const char *suffix) const {
314 0 : size_t suffixLen = strlen(suffix);
315 :
316 0 : if (mSize < suffixLen) {
317 0 : return false;
318 : }
319 :
320 0 : return !strcmp(mData + mSize - suffixLen, suffix);
321 : }
322 :
323 0 : AString StringPrintf(const char *format, ...) {
324 : va_list ap;
325 0 : va_start(ap, format);
326 :
327 : char *buffer;
328 : #ifdef WIN32
329 : int n = vsnprintf(NULL, 0, format, ap);
330 : buffer = new char[n+1];
331 : vsnprintf(buffer, n+1, format, ap);
332 : #else
333 0 : vasprintf(&buffer, format, ap);
334 : #endif
335 :
336 0 : va_end(ap);
337 :
338 0 : AString result(buffer);
339 :
340 0 : free(buffer);
341 0 : buffer = NULL;
342 :
343 0 : return result;
344 : }
345 :
346 : } // namespace stagefright
347 :
|