blob: cdce160adca48c278775563df0caed79565e119a [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/sgl/SkString.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "SkString.h"
19#include "SkFixed.h"
20#include "SkUtils.h"
21#include <stdarg.h>
22
23bool SkStrStartsWith(const char string[], const char prefix[])
24{
25 SkASSERT(string);
26 SkASSERT(prefix);
27 return !strncmp(string, prefix, strlen(prefix));
28}
29
30bool SkStrEndsWith(const char string[], const char suffix[])
31{
32 SkASSERT(string);
33 SkASSERT(suffix);
34 size_t strLen = strlen(string);
35 size_t suffixLen = strlen(suffix);
36 return strLen >= suffixLen &&
37 !strncmp(string + strLen - suffixLen, suffix, suffixLen);
38}
39
40int SkStrStartsWithOneOf(const char string[], const char prefixes[])
41{
42 int index = 0;
43 do {
44 const char* limit = strchr(prefixes, '\0');
45 if (!strncmp(string, prefixes, limit - prefixes))
46 return index;
47 prefixes = limit + 1;
48 index++;
49 } while (prefixes[0]);
50 return -1;
51}
52
53char* SkStrAppendS32(char string[], int32_t dec)
54{
55 SkDEBUGCODE(char* start = string;)
56
57 char buffer[SkStrAppendS32_MaxSize];
58 char* p = buffer + sizeof(buffer);
59 bool neg = false;
60
61 if (dec < 0)
62 {
63 neg = true;
64 dec = -dec;
65 }
66 do {
67 *--p = SkToU8('0' + dec % 10);
68 dec /= 10;
69 } while (dec != 0);
70 if (neg)
71 *--p = '-';
72
73 SkASSERT(p >= buffer);
74 char* stop = buffer + sizeof(buffer);
75 while (p < stop)
76 *string++ = *p++;
77
78 SkASSERT(string - start <= SkStrAppendS32_MaxSize);
79 return string;
80}
81
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000082char* SkStrAppendS64(char string[], int64_t dec, int minDigits)
83{
84 SkDEBUGCODE(char* start = string;)
85
86 char buffer[SkStrAppendS64_MaxSize];
87 char* p = buffer + sizeof(buffer);
88 bool neg = false;
89
90 if (dec < 0) {
91 neg = true;
92 dec = -dec;
93 }
94 do {
95 *--p = SkToU8('0' + dec % 10);
96 dec /= 10;
97 minDigits--;
98 } while (dec != 0);
99 while (minDigits > 0) {
100 *--p = '0';
101 minDigits--;
102 }
103 if (neg)
104 *--p = '-';
105
106 SkASSERT(p >= buffer);
107 size_t cp_len = buffer + sizeof(buffer) - p;
108 memcpy(string, p, cp_len);
109 string += cp_len;
110
111 SkASSERT(string - start <= SkStrAppendS64_MaxSize);
112 return string;
113}
114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115char* SkStrAppendScalar(char string[], SkScalar value)
116{
117 SkDEBUGCODE(char* start = string;)
118
119 SkFixed x = SkScalarToFixed(value);
120
121 if (x < 0)
122 {
123 *string++ = '-';
124 x = -x;
125 }
126
127 unsigned frac = x & 0xFFFF;
128 x >>= 16;
129 if (frac == 0xFFFF) // need to do this to "round up", since 65535/65536 is closer to 1 than to .9999
130 {
131 x += 1;
132 frac = 0;
133 }
134 string = SkStrAppendS32(string, x);
135
136 // now handle the fractional part (if any)
137 if (frac)
138 {
139 static const uint16_t gTens[] = { 1000, 100, 10, 1 };
140 const uint16_t* tens = gTens;
141
142 x = SkFixedRound(frac * 10000);
143 SkASSERT(x <= 10000);
144 if (x == 10000) {
145 x -= 1;
146 }
147 *string++ = '.';
148 do {
149 unsigned powerOfTen = *tens++;
150 *string++ = SkToU8('0' + x / powerOfTen);
151 x %= powerOfTen;
152 } while (x != 0);
153 }
154
155 SkASSERT(string - start <= SkStrAppendScalar_MaxSize);
156 return string;
157}
158
159////////////////////////////////////////////////////////////////////////////////////
160
161#define kMaxRefCnt_SkString SK_MaxU16
162
163// the 3 values are [length] [refcnt] [terminating zero data]
164const SkString::Rec SkString::gEmptyRec = { 0, 0, 0 };
165
166#define SizeOfRec() (gEmptyRec.data() - (const char*)&gEmptyRec)
167
168SkString::Rec* SkString::AllocRec(const char text[], U16CPU len)
169{
170 Rec* rec;
171
172 if (len == 0)
173 rec = const_cast<Rec*>(&gEmptyRec);
174 else
175 {
176 // add 1 for terminating 0, then align4 so we can have some slop when growing the string
177 rec = (Rec*)sk_malloc_throw(SizeOfRec() + SkAlign4(len + 1));
178 rec->fLength = SkToU16(len);
179 rec->fRefCnt = 1;
180 if (text)
181 memcpy(rec->data(), text, len);
182 rec->data()[len] = 0;
183 }
184 return rec;
185}
186
187SkString::Rec* SkString::RefRec(Rec* src)
188{
189 if (src != &gEmptyRec)
190 {
191 if (src->fRefCnt == kMaxRefCnt_SkString) {
192 src = AllocRec(src->data(), src->fLength);
193 } else
194 src->fRefCnt += 1;
195 }
196 return src;
197}
198
199#ifdef SK_DEBUG
200void SkString::validate() const
201{
202 // make sure know one has written over our global
203 SkASSERT(gEmptyRec.fLength == 0);
204 SkASSERT(gEmptyRec.fRefCnt == 0);
205 SkASSERT(gEmptyRec.data()[0] == 0);
206
207 if (fRec != &gEmptyRec)
208 {
209 SkASSERT(fRec->fLength > 0);
210 SkASSERT(fRec->fRefCnt > 0);
211 SkASSERT(fRec->data()[fRec->fLength] == 0);
212 }
213 SkASSERT(fStr == c_str());
214}
215#endif
216
217///////////////////////////////////////////////////////////////////////
218
219SkString::SkString() : fRec(const_cast<Rec*>(&gEmptyRec)) {
220#ifdef SK_DEBUG
221 fStr = fRec->data();
222#endif
223}
224
225SkString::SkString(size_t len)
226{
227 SkASSERT(SkToU16(len) == len); // can't handle larger than 64K
228
229 fRec = AllocRec(NULL, (U16CPU)len);
230#ifdef SK_DEBUG
231 fStr = fRec->data();
232#endif
233}
234
235SkString::SkString(const char text[])
236{
237 size_t len = text ? strlen(text) : 0;
238
239 fRec = AllocRec(text, (U16CPU)len);
240#ifdef SK_DEBUG
241 fStr = fRec->data();
242#endif
243}
244
245SkString::SkString(const char text[], size_t len)
246{
247 fRec = AllocRec(text, (U16CPU)len);
248#ifdef SK_DEBUG
249 fStr = fRec->data();
250#endif
251}
252
253SkString::SkString(const SkString& src)
254{
255 src.validate();
256
257 fRec = RefRec(src.fRec);
258#ifdef SK_DEBUG
259 fStr = fRec->data();
260#endif
261}
262
263SkString::~SkString()
264{
265 this->validate();
266
267 if (fRec->fLength)
268 {
269 SkASSERT(fRec->fRefCnt > 0);
270 if (--fRec->fRefCnt == 0)
271 sk_free(fRec);
272 }
273}
274
275bool SkString::equals(const SkString& src) const
276{
277 return fRec == src.fRec || this->equals(src.c_str(), src.size());
278}
279
280bool SkString::equals(const char text[]) const
281{
282 return this->equals(text, text ? strlen(text) : 0);
283}
284
285bool SkString::equals(const char text[], size_t len) const
286{
287 SkASSERT(len == 0 || text != NULL);
288
289 return fRec->fLength == len && !memcmp(fRec->data(), text, len);
290}
291
292SkString& SkString::operator=(const SkString& src)
293{
294 this->validate();
295
296 if (fRec != src.fRec)
297 {
298 SkString tmp(src);
299 this->swap(tmp);
300 }
301 return *this;
302}
303
304void SkString::reset()
305{
306 this->validate();
307
308 if (fRec->fLength)
309 {
310 SkASSERT(fRec->fRefCnt > 0);
311 if (--fRec->fRefCnt == 0)
312 sk_free(fRec);
313 }
314
315 fRec = const_cast<Rec*>(&gEmptyRec);
316#ifdef SK_DEBUG
317 fStr = fRec->data();
318#endif
319}
320
321char* SkString::writable_str()
322{
323 this->validate();
324
325 if (fRec->fLength)
326 {
327 if (fRec->fRefCnt > 1)
328 {
329 fRec->fRefCnt -= 1;
330 fRec = AllocRec(fRec->data(), fRec->fLength);
331 #ifdef SK_DEBUG
332 fStr = fRec->data();
333 #endif
334 }
335 }
336 return fRec->data();
337}
338
339void SkString::set(const char text[])
340{
341 this->set(text, text ? strlen(text) : 0);
342}
343
344void SkString::set(const char text[], size_t len)
345{
346 if (len == 0)
347 this->reset();
348 else if (fRec->fRefCnt == 1 && len <= fRec->fLength) // should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))
349 {
350 // just use less of the buffer without allocating a smaller one
351 char* p = this->writable_str();
352 if (text)
353 memcpy(p, text, len);
354 p[len] = 0;
355 fRec->fLength = SkToU16(len);
356 }
357 else if (fRec->fRefCnt == 1 && ((unsigned)fRec->fLength >> 2) == (len >> 2))
358 {
359 // we have spare room in the current allocation, so don't alloc a larger one
360 char* p = this->writable_str();
361 if (text)
362 memcpy(p, text, len);
363 p[len] = 0;
364 fRec->fLength = SkToU16(len);
365 }
366 else
367 {
368 SkString tmp(text, len);
369 this->swap(tmp);
370 }
371}
372
373void SkString::setUTF16(const uint16_t src[])
374{
375 int count = 0;
376
377 while (src[count])
378 count += 1;
379 setUTF16(src, count);
380}
381
382void SkString::setUTF16(const uint16_t src[], size_t count)
383{
384 if (count == 0)
385 this->reset();
386 else if (count <= fRec->fLength) // should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))
387 {
388 if (count < fRec->fLength)
389 this->resize(count);
390 char* p = this->writable_str();
391 for (size_t i = 0; i < count; i++)
392 p[i] = SkToU8(src[i]);
393 p[count] = 0;
394 }
395 else
396 {
397 SkString tmp(count); // puts a null terminator at the end of the string
398 char* p = tmp.writable_str();
399
400 for (size_t i = 0; i < count; i++)
401 p[i] = SkToU8(src[i]);
402
403 this->swap(tmp);
404 }
405}
406
407void SkString::insert(size_t offset, const char text[])
408{
409 this->insert(offset, text, text ? strlen(text) : 0);
410}
411
412void SkString::insert(size_t offset, const char text[], size_t len)
413{
414 if (len)
415 {
416 size_t length = fRec->fLength;
417 if (offset > length)
418 offset = length;
419
420 /* If we're the only owner, and we have room in our allocation for the insert,
421 do it in place, rather than allocating a new buffer.
422
423 To know we have room, compare the allocated sizes
424 beforeAlloc = SkAlign4(length + 1)
425 afterAlloc = SkAligh4(length + 1 + len)
426 but SkAlign4(x) is (x + 3) >> 2 << 2
427 which is equivalent for testing to (length + 1 + 3) >> 2 == (length + 1 + 3 + len) >> 2
428 and we can then eliminate the +1+3 since that doesn't affec the answer
429 */
430 if (fRec->fRefCnt == 1 && (length >> 2) == ((length + len) >> 2))
431 {
432 char* dst = this->writable_str();
433
434 if (offset < length)
435 memmove(dst + offset + len, dst + offset, length - offset);
436 memcpy(dst + offset, text, len);
437
438 dst[length + len] = 0;
439 fRec->fLength = SkToU16(length + len);
440 }
441 else
442 {
443 /* Seems we should use realloc here, since that is safe if it fails
444 (we have the original data), and might be faster than alloc/copy/free.
445 */
446 SkString tmp(fRec->fLength + len);
447 char* dst = tmp.writable_str();
448
449 if (offset > 0)
450 memcpy(dst, fRec->data(), offset);
451 memcpy(dst + offset, text, len);
452 if (offset < fRec->fLength)
453 memcpy(dst + offset + len, fRec->data() + offset, fRec->fLength - offset);
454
455 this->swap(tmp);
456 }
457 }
458}
459
460void SkString::insertUnichar(size_t offset, SkUnichar uni)
461{
462 char buffer[kMaxBytesInUTF8Sequence];
463 size_t len = SkUTF8_FromUnichar(uni, buffer);
464
465 if (len)
466 this->insert(offset, buffer, len);
467}
468
469void SkString::insertS32(size_t offset, int32_t dec)
470{
471 char buffer[SkStrAppendS32_MaxSize];
472 char* stop = SkStrAppendS32(buffer, dec);
473 this->insert(offset, buffer, stop - buffer);
474}
475
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000476void SkString::insertS64(size_t offset, int64_t dec, int minDigits)
477{
478 char buffer[SkStrAppendS64_MaxSize];
479 char* stop = SkStrAppendS64(buffer, dec, minDigits);
480 this->insert(offset, buffer, stop - buffer);
481}
482
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483void SkString::insertHex(size_t offset, uint32_t hex, int minDigits)
484{
485 minDigits = SkPin32(minDigits, 0, 8);
486
487 static const char gHex[] = "0123456789ABCDEF";
488
489 char buffer[8];
490 char* p = buffer + sizeof(buffer);
491
492 do {
493 *--p = gHex[hex & 0xF];
494 hex >>= 4;
495 minDigits -= 1;
496 } while (hex != 0);
497 while (--minDigits >= 0)
498 *--p = '0';
499
500 SkASSERT(p >= buffer);
501 this->insert(offset, p, buffer + sizeof(buffer) - p);
502}
503
504void SkString::insertScalar(size_t offset, SkScalar value)
505{
506 char buffer[SkStrAppendScalar_MaxSize];
507 char* stop = SkStrAppendScalar(buffer, value);
508 this->insert(offset, buffer, stop - buffer);
509}
510
511///////////////////////////////////////////////////////////////////////////
512
513#include <stdio.h>
514
515// number of bytes (on the stack) to receive the printf result
516static const size_t kBufferSize = 256;
517
518#ifdef SK_BUILD_FOR_WIN
519 #define VSNPRINTF _vsnprintf
520#else
521 #define VSNPRINTF vsnprintf
522#endif
523
524#define ARGS_TO_BUFFER(format, buffer, size) \
525 do { \
526 va_list args; \
527 va_start(args, format); \
528 VSNPRINTF(buffer, size, format, args); \
529 va_end(args); \
530 } while (0)
531
532void SkString::printf(const char format[], ...) {
533 char buffer[kBufferSize];
534 ARGS_TO_BUFFER(format, buffer, kBufferSize);
535
536 this->set(buffer, strlen(buffer));
537}
538
539void SkString::appendf(const char format[], ...) {
540 char buffer[kBufferSize];
541 ARGS_TO_BUFFER(format, buffer, kBufferSize);
542
543 this->append(buffer, strlen(buffer));
544}
545
546void SkString::prependf(const char format[], ...) {
547 char buffer[kBufferSize];
548 ARGS_TO_BUFFER(format, buffer, kBufferSize);
549
550 this->prepend(buffer, strlen(buffer));
551}
552
553#undef VSNPRINTF
554
555///////////////////////////////////////////////////////////////////////////
556
557void SkString::remove(size_t offset, size_t length)
558{
559 size_t size = this->size();
560
561 if (offset < size)
562 {
563 if (offset + length > size)
564 length = size - offset;
565 if (length > 0)
566 {
567 SkASSERT(size > length);
568 SkString tmp(size - length);
569 char* dst = tmp.writable_str();
570 const char* src = this->c_str();
571
572 if (offset)
573 {
574 SkASSERT(offset <= tmp.size());
575 memcpy(dst, src, offset);
576 }
577 size_t tail = size - offset - length;
578 SkASSERT((int32_t)tail >= 0);
579 if (tail)
580 {
581 // SkASSERT(offset + length <= tmp.size());
582 memcpy(dst + offset, src + offset + length, tail);
583 }
584 SkASSERT(dst[tmp.size()] == 0);
585 this->swap(tmp);
586 }
587 }
588}
589
590void SkString::swap(SkString& other)
591{
592 this->validate();
593 other.validate();
594
595 SkTSwap<Rec*>(fRec, other.fRec);
596#ifdef SK_DEBUG
597 SkTSwap<const char*>(fStr, other.fStr);
598#endif
599}
600
601/////////////////////////////////////////////////////////////////////////////////
602
603SkAutoUCS2::SkAutoUCS2(const char utf8[])
604{
605 size_t len = strlen(utf8);
606 fUCS2 = (uint16_t*)sk_malloc_throw((len + 1) * sizeof(uint16_t));
607
608 uint16_t* dst = fUCS2;
609 for (;;)
610 {
611 SkUnichar uni = SkUTF8_NextUnichar(&utf8);
612 *dst++ = SkToU16(uni);
613 if (uni == 0)
614 break;
615 }
616 fCount = (int)(dst - fUCS2);
617}
618
619SkAutoUCS2::~SkAutoUCS2()
620{
621 delete[] fUCS2;
622}