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