blob: c2fd9c0e3684ed361d651efe24a9d53bdd05e75b [file] [log] [blame]
Hal Canaryfdcfb8b2018-06-13 09:42:32 -04001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkString_DEFINED
11#define SkString_DEFINED
12
bungemanbf521ff2016-02-17 13:13:44 -080013#include "../private/SkTArray.h"
Hal Canary2a2f6752018-06-11 21:44:01 -040014#include "SkScalar.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040015#include "SkRefCnt.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
Ben Wagneraf893662017-10-03 11:08:14 -040017#include <atomic>
bsalomon@google.comb9cf3932013-03-07 18:05:08 +000018#include <stdarg.h>
19
reed@android.com8a1c16f2008-12-17 15:59:43 +000020/* Some helper functions for C strings
21*/
22
epoger@google.come8ebeb12012-10-29 16:42:11 +000023static bool SkStrStartsWith(const char string[], const char prefixStr[]) {
epoger@google.comc4ae9742012-04-27 17:11:31 +000024 SkASSERT(string);
epoger@google.come8ebeb12012-10-29 16:42:11 +000025 SkASSERT(prefixStr);
26 return !strncmp(string, prefixStr, strlen(prefixStr));
epoger@google.comc4ae9742012-04-27 17:11:31 +000027}
epoger@google.come8ebeb12012-10-29 16:42:11 +000028static bool SkStrStartsWith(const char string[], const char prefixChar) {
29 SkASSERT(string);
30 return (prefixChar == *string);
31}
32
33bool SkStrEndsWith(const char string[], const char suffixStr[]);
34bool SkStrEndsWith(const char string[], const char suffixChar);
35
reed@android.com8a1c16f2008-12-17 15:59:43 +000036int SkStrStartsWithOneOf(const char string[], const char prefixes[]);
epoger@google.come8ebeb12012-10-29 16:42:11 +000037
humper@google.com7af56be2013-01-14 18:49:19 +000038static int SkStrFind(const char string[], const char substring[]) {
humper@google.com61a972f2013-01-14 19:03:46 +000039 const char *first = strstr(string, substring);
Ben Wagnera93a14a2017-08-28 10:34:05 -040040 if (nullptr == first) return -1;
bungeman68c14d92016-03-19 15:06:56 -070041 return SkToInt(first - &string[0]);
humper@google.com7af56be2013-01-14 18:49:19 +000042}
43
wangyix7ef45a12015-08-13 06:51:35 -070044static int SkStrFindLastOf(const char string[], const char subchar) {
45 const char* last = strrchr(string, subchar);
Ben Wagnera93a14a2017-08-28 10:34:05 -040046 if (nullptr == last) return -1;
bungeman68c14d92016-03-19 15:06:56 -070047 return SkToInt(last - &string[0]);
wangyix7ef45a12015-08-13 06:51:35 -070048}
49
epoger@google.comc4ae9742012-04-27 17:11:31 +000050static bool SkStrContains(const char string[], const char substring[]) {
51 SkASSERT(string);
52 SkASSERT(substring);
humper@google.com7af56be2013-01-14 18:49:19 +000053 return (-1 != SkStrFind(string, substring));
epoger@google.comc4ae9742012-04-27 17:11:31 +000054}
epoger@google.come8ebeb12012-10-29 16:42:11 +000055static bool SkStrContains(const char string[], const char subchar) {
56 SkASSERT(string);
humper@google.com7af56be2013-01-14 18:49:19 +000057 char tmp[2];
58 tmp[0] = subchar;
59 tmp[1] = '\0';
60 return (-1 != SkStrFind(string, tmp));
epoger@google.come8ebeb12012-10-29 16:42:11 +000061}
reed@android.com8a1c16f2008-12-17 15:59:43 +000062
humper@google.com7af56be2013-01-14 18:49:19 +000063static inline char *SkStrDup(const char string[]) {
64 char *ret = (char *) sk_malloc_throw(strlen(string)+1);
robertphillips@google.com37ebe3f2013-03-04 20:03:44 +000065 memcpy(ret,string,strlen(string)+1);
humper@google.com7af56be2013-01-14 18:49:19 +000066 return ret;
67}
68
reedd23f45d2014-11-07 07:37:33 -080069/*
70 * The SkStrAppend... methods will write into the provided buffer, assuming it is large enough.
71 * Each method has an associated const (e.g. SkStrAppendU32_MaxSize) which will be the largest
72 * value needed for that method's buffer.
73 *
74 * char storage[SkStrAppendU32_MaxSize];
75 * SkStrAppendU32(storage, value);
76 *
77 * Note : none of the SkStrAppend... methods write a terminating 0 to their buffers. Instead,
78 * the methods return the ptr to the end of the written part of the buffer. This can be used
79 * to compute the length, and/or know where to write a 0 if that is desired.
80 *
81 * char storage[SkStrAppendU32_MaxSize + 1];
82 * char* stop = SkStrAppendU32(storage, value);
83 * size_t len = stop - storage;
84 * *stop = 0; // valid, since storage was 1 byte larger than the max.
85 */
humper@google.com7af56be2013-01-14 18:49:19 +000086
epoger@google.comd88a3d82013-06-19 18:27:20 +000087#define SkStrAppendU32_MaxSize 10
epoger@google.com27d30252013-06-19 18:52:42 +000088char* SkStrAppendU32(char buffer[], uint32_t);
epoger@google.comd88a3d82013-06-19 18:27:20 +000089#define SkStrAppendU64_MaxSize 20
epoger@google.com27d30252013-06-19 18:52:42 +000090char* SkStrAppendU64(char buffer[], uint64_t, int minDigits);
epoger@google.comd88a3d82013-06-19 18:27:20 +000091
92#define SkStrAppendS32_MaxSize (SkStrAppendU32_MaxSize + 1)
reed@android.com8a1c16f2008-12-17 15:59:43 +000093char* SkStrAppendS32(char buffer[], int32_t);
epoger@google.comd88a3d82013-06-19 18:27:20 +000094#define SkStrAppendS64_MaxSize (SkStrAppendU64_MaxSize + 1)
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000095char* SkStrAppendS64(char buffer[], int64_t, int minDigits);
reed@google.com8072e4f2011-03-01 15:44:08 +000096
97/**
98 * Floats have at most 8 significant digits, so we limit our %g to that.
reed@google.come280f1d2011-03-03 18:43:14 +000099 * However, the total string could be 15 characters: -1.2345678e-005
100 *
101 * In theory we should only expect up to 2 digits for the exponent, but on
102 * some platforms we have seen 3 (as in the example above).
reed@google.com8072e4f2011-03-01 15:44:08 +0000103 */
reed@google.come280f1d2011-03-03 18:43:14 +0000104#define SkStrAppendScalar_MaxSize 15
reed@google.com8072e4f2011-03-01 15:44:08 +0000105
106/**
107 * Write the scaler in decimal format into buffer, and return a pointer to
108 * the next char after the last one written. Note: a terminating 0 is not
109 * written into buffer, which must be at least SkStrAppendScalar_MaxSize.
110 * Thus if the caller wants to add a 0 at the end, buffer must be at least
111 * SkStrAppendScalar_MaxSize + 1 bytes large.
112 */
reed@google.com8f4d2302013-12-17 16:44:46 +0000113#define SkStrAppendScalar SkStrAppendFloat
vandebo@chromium.org677cbed2011-03-03 18:20:12 +0000114
vandebo@chromium.org677cbed2011-03-03 18:20:12 +0000115char* SkStrAppendFloat(char buffer[], float);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116
117/** \class SkString
118
119 Light weight class for managing strings. Uses reference
120 counting to make string assignments and copies very fast
121 with no extra RAM cost. Assumes UTF8 encoding.
122*/
vandebo@chromium.orga56fedc2012-10-18 23:26:44 +0000123class SK_API SkString {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124public:
125 SkString();
126 explicit SkString(size_t len);
127 explicit SkString(const char text[]);
128 SkString(const char text[], size_t len);
reed@android.com8015dd82009-06-21 00:49:18 +0000129 SkString(const SkString&);
bungeman9d552972016-02-07 18:42:54 -0800130 SkString(SkString&&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 ~SkString();
132
reed@google.com4bce1152011-09-14 16:13:58 +0000133 bool isEmpty() const { return 0 == fRec->fLength; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 size_t size() const { return (size_t) fRec->fLength; }
135 const char* c_str() const { return fRec->data(); }
reed@android.com8015dd82009-06-21 00:49:18 +0000136 char operator[](size_t n) const { return this->c_str()[n]; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000138 bool equals(const SkString&) const;
139 bool equals(const char text[]) const;
140 bool equals(const char text[], size_t len) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141
epoger@google.come8ebeb12012-10-29 16:42:11 +0000142 bool startsWith(const char prefixStr[]) const {
143 return SkStrStartsWith(fRec->data(), prefixStr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 }
epoger@google.come8ebeb12012-10-29 16:42:11 +0000145 bool startsWith(const char prefixChar) const {
146 return SkStrStartsWith(fRec->data(), prefixChar);
147 }
148 bool endsWith(const char suffixStr[]) const {
149 return SkStrEndsWith(fRec->data(), suffixStr);
150 }
151 bool endsWith(const char suffixChar) const {
152 return SkStrEndsWith(fRec->data(), suffixChar);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 }
epoger@google.comc4ae9742012-04-27 17:11:31 +0000154 bool contains(const char substring[]) const {
155 return SkStrContains(fRec->data(), substring);
156 }
epoger@google.come8ebeb12012-10-29 16:42:11 +0000157 bool contains(const char subchar) const {
158 return SkStrContains(fRec->data(), subchar);
159 }
humper@google.com7af56be2013-01-14 18:49:19 +0000160 int find(const char substring[]) const {
161 return SkStrFind(fRec->data(), substring);
162 }
wangyix7ef45a12015-08-13 06:51:35 -0700163 int findLastOf(const char subchar) const {
164 return SkStrFindLastOf(fRec->data(), subchar);
165 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166
reed@google.comb530ef52011-07-20 19:55:42 +0000167 friend bool operator==(const SkString& a, const SkString& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 return a.equals(b);
169 }
reed@google.comb530ef52011-07-20 19:55:42 +0000170 friend bool operator!=(const SkString& a, const SkString& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 return !a.equals(b);
172 }
173
174 // these methods edit the string
175
reed@google.comb530ef52011-07-20 19:55:42 +0000176 SkString& operator=(const SkString&);
bungeman9d552972016-02-07 18:42:54 -0800177 SkString& operator=(SkString&&);
reed@google.comb530ef52011-07-20 19:55:42 +0000178 SkString& operator=(const char text[]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179
reed@google.comb530ef52011-07-20 19:55:42 +0000180 char* writable_str();
reed@android.com8015dd82009-06-21 00:49:18 +0000181 char& operator[](size_t n) { return this->writable_str()[n]; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000183 void reset();
bungemanb2885d52015-08-31 14:36:48 -0700184 /** Destructive resize, does not preserve contents. */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400185 void resize(size_t len) { this->set(nullptr, len); }
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000186 void set(const SkString& src) { *this = src; }
187 void set(const char text[]);
188 void set(const char text[], size_t len);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000190 void insert(size_t offset, const SkString& src) { this->insert(offset, src.c_str(), src.size()); }
191 void insert(size_t offset, const char text[]);
192 void insert(size_t offset, const char text[], size_t len);
193 void insertUnichar(size_t offset, SkUnichar);
194 void insertS32(size_t offset, int32_t value);
195 void insertS64(size_t offset, int64_t value, int minDigits = 0);
epoger@google.comd88a3d82013-06-19 18:27:20 +0000196 void insertU32(size_t offset, uint32_t value);
197 void insertU64(size_t offset, uint64_t value, int minDigits = 0);
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000198 void insertHex(size_t offset, uint32_t value, int minDigits = 0);
199 void insertScalar(size_t offset, SkScalar);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000201 void append(const SkString& str) { this->insert((size_t)-1, str); }
202 void append(const char text[]) { this->insert((size_t)-1, text); }
203 void append(const char text[], size_t len) { this->insert((size_t)-1, text, len); }
204 void appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); }
205 void appendS32(int32_t value) { this->insertS32((size_t)-1, value); }
206 void appendS64(int64_t value, int minDigits = 0) { this->insertS64((size_t)-1, value, minDigits); }
epoger@google.comd88a3d82013-06-19 18:27:20 +0000207 void appendU32(uint32_t value) { this->insertU32((size_t)-1, value); }
208 void appendU64(uint64_t value, int minDigits = 0) { this->insertU64((size_t)-1, value, minDigits); }
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000209 void appendHex(uint32_t value, int minDigits = 0) { this->insertHex((size_t)-1, value, minDigits); }
210 void appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000212 void prepend(const SkString& str) { this->insert(0, str); }
213 void prepend(const char text[]) { this->insert(0, text); }
214 void prepend(const char text[], size_t len) { this->insert(0, text, len); }
215 void prependUnichar(SkUnichar uni) { this->insertUnichar(0, uni); }
216 void prependS32(int32_t value) { this->insertS32(0, value); }
217 void prependS64(int32_t value, int minDigits = 0) { this->insertS64(0, value, minDigits); }
218 void prependHex(uint32_t value, int minDigits = 0) { this->insertHex(0, value, minDigits); }
219 void prependScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +0000221 void printf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
222 void appendf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
commit-bot@chromium.orgce0e4ef2013-11-21 17:20:17 +0000223 void appendVAList(const char format[], va_list);
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +0000224 void prependf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
joshualitt30ba4362014-08-21 20:18:45 -0700225 void prependVAList(const char format[], va_list);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000227 void remove(size_t offset, size_t length);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228
bsalomon@google.comfc296292011-05-06 13:53:47 +0000229 SkString& operator+=(const SkString& s) { this->append(s); return *this; }
230 SkString& operator+=(const char text[]) { this->append(text); return *this; }
epoger@google.com4a8084c2013-05-09 20:01:26 +0000231 SkString& operator+=(const char c) { this->append(&c, 1); return *this; }
bsalomon@google.comfc296292011-05-06 13:53:47 +0000232
mike@reedtribe.org4e1d3ac2011-04-10 01:04:37 +0000233 /**
234 * Swap contents between this and other. This function is guaranteed
235 * to never fail or throw.
236 */
237 void swap(SkString& other);
reed@google.com8072e4f2011-03-01 15:44:08 +0000238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239private:
240 struct Rec {
241 public:
Ben Wagnereccda1c2017-10-05 10:13:51 -0400242 constexpr Rec(uint32_t len, int32_t refCnt)
243 : fLength(len), fRefCnt(refCnt), fBeginningOfData(0)
244 { }
245 static sk_sp<Rec> Make(const char text[], size_t len);
reed@google.com8cb10882013-06-04 20:36:52 +0000246 uint32_t fLength; // logically size_t, but we want it to stay 32bits
Ben Wagnereccda1c2017-10-05 10:13:51 -0400247 mutable std::atomic<int32_t> fRefCnt;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 char fBeginningOfData;
249
250 char* data() { return &fBeginningOfData; }
251 const char* data() const { return &fBeginningOfData; }
Ben Wagneraf893662017-10-03 11:08:14 -0400252
Ben Wagnereccda1c2017-10-05 10:13:51 -0400253 void ref() const;
254 void unref() const;
255 bool unique() const;
256 private:
257 // Ensure the unsized delete is called.
258 void operator delete(void* p) { ::operator delete(p); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 };
Ben Wagnereccda1c2017-10-05 10:13:51 -0400260 sk_sp<Rec> fRec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261
262#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 void validate() const;
264#else
265 void validate() const {}
266#endif
267
268 static const Rec gEmptyRec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269};
270
tomhudson@google.com3a1f6a02011-06-30 14:39:52 +0000271/// Creates a new string and writes into it using a printf()-style format.
272SkString SkStringPrintf(const char* format, ...);
Brian Salomon1c80e992018-01-29 09:50:47 -0500273/// This makes it easier to write a caller as a VAR_ARGS function where the format string is
274/// optional.
275static inline SkString SkStringPrintf() { return SkString(); }
tomhudson@google.com3a1f6a02011-06-30 14:39:52 +0000276
bsalomon@google.comff436612013-02-27 19:07:32 +0000277// Specialized to take advantage of SkString's fast swap path. The unspecialized function is
278// declared in SkTypes.h and called by SkTSort.
279template <> inline void SkTSwap(SkString& a, SkString& b) {
280 a.swap(b);
281}
282
kkinnunen3e980c32015-12-23 01:33:00 -0800283enum SkStrSplitMode {
284 // Strictly return all results. If the input is ",," and the separator is ',' this will return
285 // an array of three empty strings.
286 kStrict_SkStrSplitMode,
287
288 // Only nonempty results will be added to the results. Multiple separators will be
289 // coalesced. Separators at the beginning and end of the input will be ignored. If the input is
290 // ",," and the separator is ',', this will return an empty vector.
291 kCoalesce_SkStrSplitMode
292};
293
rmistry@google.comd6bab022013-12-02 13:50:38 +0000294// Split str on any characters in delimiters into out. (Think, strtok with a sane API.)
kkinnunen3e980c32015-12-23 01:33:00 -0800295void SkStrSplit(const char* str, const char* delimiters, SkStrSplitMode splitMode,
296 SkTArray<SkString>* out);
297inline void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out) {
298 SkStrSplit(str, delimiters, kCoalesce_SkStrSplitMode, out);
299}
rmistry@google.comd6bab022013-12-02 13:50:38 +0000300
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301#endif