1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkAtomics.h"
#include "SkSafeMath.h"
#include "SkString.h"
#include "SkUtils.h"
#include <stdarg.h>
#include <stdio.h>
// number of bytes (on the stack) to receive the printf result
static const size_t kBufferSize = 1024;
#define ARGS_TO_BUFFER(format, buffer, size, written) \
do { \
va_list args; \
va_start(args, format); \
written = vsnprintf(buffer, size, format, args); \
SkASSERT(written >= 0 && written < SkToInt(size)); \
va_end(args); \
} while (0)
#define V_SKSTRING_PRINTF(output, format) \
do { \
va_list args; \
va_start(args, format); \
char buffer[kBufferSize]; \
int length = vsnprintf(buffer, sizeof(buffer), format, args); \
va_end(args); \
if (length < 0) { \
break; \
} \
if (length < (int)sizeof(buffer)) { \
output.set(buffer, length); \
break; \
} \
SkString tmp((size_t)length); \
va_start(args, format); \
SkDEBUGCODE(int check = ) vsnprintf(tmp.writable_str(), \
length + 1, format, args); \
va_end(args); \
SkASSERT(check == length); \
output = std::move(tmp); \
SkASSERT(output[length] == '\0'); \
} while (false)
///////////////////////////////////////////////////////////////////////////////
bool SkStrEndsWith(const char string[], const char suffixStr[]) {
SkASSERT(string);
SkASSERT(suffixStr);
size_t strLen = strlen(string);
size_t suffixLen = strlen(suffixStr);
return strLen >= suffixLen &&
!strncmp(string + strLen - suffixLen, suffixStr, suffixLen);
}
bool SkStrEndsWith(const char string[], const char suffixChar) {
SkASSERT(string);
size_t strLen = strlen(string);
if (0 == strLen) {
return false;
} else {
return (suffixChar == string[strLen-1]);
}
}
int SkStrStartsWithOneOf(const char string[], const char prefixes[]) {
int index = 0;
do {
const char* limit = strchr(prefixes, '\0');
if (!strncmp(string, prefixes, limit - prefixes)) {
return index;
}
prefixes = limit + 1;
index++;
} while (prefixes[0]);
return -1;
}
char* SkStrAppendU32(char string[], uint32_t dec) {
SkDEBUGCODE(char* start = string;)
char buffer[SkStrAppendU32_MaxSize];
char* p = buffer + sizeof(buffer);
do {
*--p = SkToU8('0' + dec % 10);
dec /= 10;
} while (dec != 0);
SkASSERT(p >= buffer);
char* stop = buffer + sizeof(buffer);
while (p < stop) {
*string++ = *p++;
}
SkASSERT(string - start <= SkStrAppendU32_MaxSize);
return string;
}
char* SkStrAppendS32(char string[], int32_t dec) {
uint32_t udec = dec;
if (dec < 0) {
*string++ = '-';
udec = ~udec + 1; // udec = -udec, but silences some warnings that are trying to be helpful
}
return SkStrAppendU32(string, udec);
}
char* SkStrAppendU64(char string[], uint64_t dec, int minDigits) {
SkDEBUGCODE(char* start = string;)
char buffer[SkStrAppendU64_MaxSize];
char* p = buffer + sizeof(buffer);
do {
*--p = SkToU8('0' + (int32_t) (dec % 10));
dec /= 10;
minDigits--;
} while (dec != 0);
while (minDigits > 0) {
*--p = '0';
minDigits--;
}
SkASSERT(p >= buffer);
size_t cp_len = buffer + sizeof(buffer) - p;
memcpy(string, p, cp_len);
string += cp_len;
SkASSERT(string - start <= SkStrAppendU64_MaxSize);
return string;
}
char* SkStrAppendS64(char string[], int64_t dec, int minDigits) {
uint64_t udec = dec;
if (dec < 0) {
*string++ = '-';
udec = ~udec + 1; // udec = -udec, but silences some warnings that are trying to be helpful
}
return SkStrAppendU64(string, udec, minDigits);
}
char* SkStrAppendFloat(char string[], float value) {
// since floats have at most 8 significant digits, we limit our %g to that.
static const char gFormat[] = "%.8g";
// make it 1 larger for the terminating 0
char buffer[SkStrAppendScalar_MaxSize + 1];
int len = snprintf(buffer, sizeof(buffer), gFormat, value);
memcpy(string, buffer, len);
SkASSERT(len <= SkStrAppendScalar_MaxSize);
return string + len;
}
///////////////////////////////////////////////////////////////////////////////
const SkString::Rec SkString::gEmptyRec(0, 0);
#define SizeOfRec() (gEmptyRec.data() - (const char*)&gEmptyRec)
static uint32_t trim_size_t_to_u32(size_t value) {
if (sizeof(size_t) > sizeof(uint32_t)) {
if (value > SK_MaxU32) {
value = SK_MaxU32;
}
}
return (uint32_t)value;
}
static size_t check_add32(size_t base, size_t extra) {
SkASSERT(base <= SK_MaxU32);
if (sizeof(size_t) > sizeof(uint32_t)) {
if (base + extra > SK_MaxU32) {
extra = SK_MaxU32 - base;
}
}
return extra;
}
sk_sp<SkString::Rec> SkString::Rec::Make(const char text[], size_t len) {
if (0 == len) {
return sk_sp<SkString::Rec>(const_cast<Rec*>(&gEmptyRec));
}
SkSafeMath safe;
// We store a 32bit version of the length
uint32_t stringLen = safe.castTo<uint32_t>(len);
// Add SizeOfRec() for our overhead and 1 for null-termination
size_t allocationSize = safe.add(len, SizeOfRec() + sizeof(char));
// Align up to a multiple of 4
allocationSize = safe.alignUp(allocationSize, 4);
SkASSERT_RELEASE(safe.ok());
void* storage = ::operator new (allocationSize);
sk_sp<Rec> rec(new (storage) Rec(stringLen, 1));
if (text) {
memcpy(rec->data(), text, len);
}
rec->data()[len] = 0;
return rec;
}
void SkString::Rec::ref() const {
if (this == &SkString::gEmptyRec) {
return;
}
SkAssertResult(this->fRefCnt.fetch_add(+1, std::memory_order_relaxed));
}
void SkString::Rec::unref() const {
if (this == &SkString::gEmptyRec) {
return;
}
int32_t oldRefCnt = this->fRefCnt.fetch_add(-1, std::memory_order_acq_rel);
SkASSERT(oldRefCnt);
if (1 == oldRefCnt) {
delete this;
}
}
bool SkString::Rec::unique() const {
return fRefCnt.load(std::memory_order_acquire) == 1;
}
#ifdef SK_DEBUG
void SkString::validate() const {
// make sure know one has written over our global
SkASSERT(0 == gEmptyRec.fLength);
SkASSERT(0 == gEmptyRec.fRefCnt.load(std::memory_order_relaxed));
SkASSERT(0 == gEmptyRec.data()[0]);
if (fRec.get() != &gEmptyRec) {
SkASSERT(fRec->fLength > 0);
SkASSERT(fRec->fRefCnt.load(std::memory_order_relaxed) > 0);
SkASSERT(0 == fRec->data()[fRec->fLength]);
}
}
#endif
///////////////////////////////////////////////////////////////////////////////
SkString::SkString() : fRec(const_cast<Rec*>(&gEmptyRec)) {
}
SkString::SkString(size_t len) {
fRec = Rec::Make(nullptr, len);
}
SkString::SkString(const char text[]) {
size_t len = text ? strlen(text) : 0;
fRec = Rec::Make(text, len);
}
SkString::SkString(const char text[], size_t len) {
fRec = Rec::Make(text, len);
}
SkString::SkString(const SkString& src) {
src.validate();
fRec = src.fRec;
}
SkString::SkString(SkString&& src) {
src.validate();
fRec = std::move(src.fRec);
src.fRec.reset(const_cast<Rec*>(&gEmptyRec));
}
SkString::~SkString() {
this->validate();
}
bool SkString::equals(const SkString& src) const {
return fRec == src.fRec || this->equals(src.c_str(), src.size());
}
bool SkString::equals(const char text[]) const {
return this->equals(text, text ? strlen(text) : 0);
}
bool SkString::equals(const char text[], size_t len) const {
SkASSERT(len == 0 || text != nullptr);
return fRec->fLength == len && !memcmp(fRec->data(), text, len);
}
SkString& SkString::operator=(const SkString& src) {
this->validate();
if (fRec != src.fRec) {
SkString tmp(src);
this->swap(tmp);
}
return *this;
}
SkString& SkString::operator=(SkString&& src) {
this->validate();
if (fRec != src.fRec) {
this->swap(src);
}
return *this;
}
SkString& SkString::operator=(const char text[]) {
this->validate();
SkString tmp(text);
this->swap(tmp);
return *this;
}
void SkString::reset() {
this->validate();
fRec.reset(const_cast<Rec*>(&gEmptyRec));
}
char* SkString::writable_str() {
this->validate();
if (fRec->fLength) {
if (!fRec->unique()) {
fRec = Rec::Make(fRec->data(), fRec->fLength);
}
}
return fRec->data();
}
void SkString::set(const char text[]) {
this->set(text, text ? strlen(text) : 0);
}
void SkString::set(const char text[], size_t len) {
len = trim_size_t_to_u32(len);
bool unique = fRec->unique();
if (0 == len) {
this->reset();
} else if (unique && len <= fRec->fLength) {
// should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))?
// just use less of the buffer without allocating a smaller one
char* p = this->writable_str();
if (text) {
memcpy(p, text, len);
}
p[len] = 0;
fRec->fLength = SkToU32(len);
} else if (unique && (fRec->fLength >> 2) == (len >> 2)) {
// we have spare room in the current allocation, so don't alloc a larger one
char* p = this->writable_str();
if (text) {
memcpy(p, text, len);
}
p[len] = 0;
fRec->fLength = SkToU32(len);
} else {
SkString tmp(text, len);
this->swap(tmp);
}
}
void SkString::setUTF16(const uint16_t src[]) {
int count = 0;
while (src[count]) {
count += 1;
}
this->setUTF16(src, count);
}
static SkString utf8_from_utf16(const uint16_t src[], size_t count) {
SkString ret;
if (count > 0) {
SkASSERT(src);
size_t n = 0;
const uint16_t* end = src + count;
for (const uint16_t* ptr = src; ptr < end;) {
const uint16_t* last = ptr;
SkUnichar u = SkUTF16_NextUnichar(&ptr);
size_t s = SkUTF8_FromUnichar(u);
if (n > SK_MaxU32 - s) {
end = last; // truncate input string
break;
}
n += s;
}
ret = SkString(n);
char* out = ret.writable_str();
for (const uint16_t* ptr = src; ptr < end;) {
out += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&ptr), out);
}
SkASSERT(out == ret.writable_str() + n);
}
return ret;
}
void SkString::setUTF16(const uint16_t src[], size_t count) {
*this = utf8_from_utf16(src, count);
}
void SkString::insert(size_t offset, const char text[]) {
this->insert(offset, text, text ? strlen(text) : 0);
}
void SkString::insert(size_t offset, const char text[], size_t len) {
if (len) {
size_t length = fRec->fLength;
if (offset > length) {
offset = length;
}
// Check if length + len exceeds 32bits, we trim len
len = check_add32(length, len);
if (0 == len) {
return;
}
/* If we're the only owner, and we have room in our allocation for the insert,
do it in place, rather than allocating a new buffer.
To know we have room, compare the allocated sizes
beforeAlloc = SkAlign4(length + 1)
afterAlloc = SkAligh4(length + 1 + len)
but SkAlign4(x) is (x + 3) >> 2 << 2
which is equivalent for testing to (length + 1 + 3) >> 2 == (length + 1 + 3 + len) >> 2
and we can then eliminate the +1+3 since that doesn't affec the answer
*/
if (fRec->unique() && (length >> 2) == ((length + len) >> 2)) {
char* dst = this->writable_str();
if (offset < length) {
memmove(dst + offset + len, dst + offset, length - offset);
}
memcpy(dst + offset, text, len);
dst[length + len] = 0;
fRec->fLength = SkToU32(length + len);
} else {
/* Seems we should use realloc here, since that is safe if it fails
(we have the original data), and might be faster than alloc/copy/free.
*/
SkString tmp(fRec->fLength + len);
char* dst = tmp.writable_str();
if (offset > 0) {
memcpy(dst, fRec->data(), offset);
}
memcpy(dst + offset, text, len);
if (offset < fRec->fLength) {
memcpy(dst + offset + len, fRec->data() + offset,
fRec->fLength - offset);
}
this->swap(tmp);
}
}
}
void SkString::insertUnichar(size_t offset, SkUnichar uni) {
char buffer[kMaxBytesInUTF8Sequence];
size_t len = SkUTF8_FromUnichar(uni, buffer);
if (len) {
this->insert(offset, buffer, len);
}
}
void SkString::insertS32(size_t offset, int32_t dec) {
char buffer[SkStrAppendS32_MaxSize];
char* stop = SkStrAppendS32(buffer, dec);
this->insert(offset, buffer, stop - buffer);
}
void SkString::insertS64(size_t offset, int64_t dec, int minDigits) {
char buffer[SkStrAppendS64_MaxSize];
char* stop = SkStrAppendS64(buffer, dec, minDigits);
this->insert(offset, buffer, stop - buffer);
}
void SkString::insertU32(size_t offset, uint32_t dec) {
char buffer[SkStrAppendU32_MaxSize];
char* stop = SkStrAppendU32(buffer, dec);
this->insert(offset, buffer, stop - buffer);
}
void SkString::insertU64(size_t offset, uint64_t dec, int minDigits) {
char buffer[SkStrAppendU64_MaxSize];
char* stop = SkStrAppendU64(buffer, dec, minDigits);
this->insert(offset, buffer, stop - buffer);
}
void SkString::insertHex(size_t offset, uint32_t hex, int minDigits) {
minDigits = SkTPin(minDigits, 0, 8);
char buffer[8];
char* p = buffer + sizeof(buffer);
do {
*--p = SkHexadecimalDigits::gUpper[hex & 0xF];
hex >>= 4;
minDigits -= 1;
} while (hex != 0);
while (--minDigits >= 0) {
*--p = '0';
}
SkASSERT(p >= buffer);
this->insert(offset, p, buffer + sizeof(buffer) - p);
}
void SkString::insertScalar(size_t offset, SkScalar value) {
char buffer[SkStrAppendScalar_MaxSize];
char* stop = SkStrAppendScalar(buffer, value);
this->insert(offset, buffer, stop - buffer);
}
void SkString::printf(const char format[], ...) {
V_SKSTRING_PRINTF((*this), format);
}
void SkString::appendf(const char format[], ...) {
char buffer[kBufferSize];
int length;
ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
this->append(buffer, length);
}
void SkString::appendVAList(const char format[], va_list args) {
char buffer[kBufferSize];
int length = vsnprintf(buffer, kBufferSize, format, args);
SkASSERT(length >= 0 && length < SkToInt(kBufferSize));
this->append(buffer, length);
}
void SkString::prependf(const char format[], ...) {
char buffer[kBufferSize];
int length;
ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
this->prepend(buffer, length);
}
void SkString::prependVAList(const char format[], va_list args) {
char buffer[kBufferSize];
int length = vsnprintf(buffer, kBufferSize, format, args);
SkASSERT(length >= 0 && length < SkToInt(kBufferSize));
this->prepend(buffer, length);
}
///////////////////////////////////////////////////////////////////////////////
void SkString::remove(size_t offset, size_t length) {
size_t size = this->size();
if (offset < size) {
if (length > size - offset) {
length = size - offset;
}
SkASSERT(length <= size);
SkASSERT(offset <= size - length);
if (length > 0) {
SkString tmp(size - length);
char* dst = tmp.writable_str();
const char* src = this->c_str();
if (offset) {
memcpy(dst, src, offset);
}
size_t tail = size - (offset + length);
if (tail) {
memcpy(dst + offset, src + (offset + length), tail);
}
SkASSERT(dst[tmp.size()] == 0);
this->swap(tmp);
}
}
}
void SkString::swap(SkString& other) {
this->validate();
other.validate();
SkTSwap(fRec, other.fRec);
}
///////////////////////////////////////////////////////////////////////////////
SkString SkStringPrintf(const char* format, ...) {
SkString formattedOutput;
V_SKSTRING_PRINTF(formattedOutput, format);
return formattedOutput;
}
void SkStrSplit(const char* str, const char* delimiters, SkStrSplitMode splitMode,
SkTArray<SkString>* out) {
if (splitMode == kCoalesce_SkStrSplitMode) {
// Skip any delimiters.
str += strspn(str, delimiters);
}
if (!*str) {
return;
}
while (true) {
// Find a token.
const size_t len = strcspn(str, delimiters);
if (splitMode == kStrict_SkStrSplitMode || len > 0) {
out->push_back().set(str, len);
str += len;
}
if (!*str) {
return;
}
if (splitMode == kCoalesce_SkStrSplitMode) {
// Skip any delimiters.
str += strspn(str, delimiters);
} else {
// Skip one delimiter.
str += 1;
}
}
}
|