1
2 /*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10 #ifndef SkString_DEFINED
11 #define SkString_DEFINED
12
13 #include "SkScalar.h"
14 #include "SkTArray.h"
15
16 #include <stdarg.h>
17
18 /* Some helper functions for C strings
19 */
20
SkStrStartsWith(const char string[],const char prefixStr[])21 static bool SkStrStartsWith(const char string[], const char prefixStr[]) {
22 SkASSERT(string);
23 SkASSERT(prefixStr);
24 return !strncmp(string, prefixStr, strlen(prefixStr));
25 }
SkStrStartsWith(const char string[],const char prefixChar)26 static bool SkStrStartsWith(const char string[], const char prefixChar) {
27 SkASSERT(string);
28 return (prefixChar == *string);
29 }
30
31 bool SkStrEndsWith(const char string[], const char suffixStr[]);
32 bool SkStrEndsWith(const char string[], const char suffixChar);
33
34 int SkStrStartsWithOneOf(const char string[], const char prefixes[]);
35
SkStrFind(const char string[],const char substring[])36 static int SkStrFind(const char string[], const char substring[]) {
37 const char *first = strstr(string, substring);
38 if (NULL == first) return -1;
39 return SkToS32(first - &string[0]);
40 }
41
SkStrContains(const char string[],const char substring[])42 static bool SkStrContains(const char string[], const char substring[]) {
43 SkASSERT(string);
44 SkASSERT(substring);
45 return (-1 != SkStrFind(string, substring));
46 }
SkStrContains(const char string[],const char subchar)47 static bool SkStrContains(const char string[], const char subchar) {
48 SkASSERT(string);
49 char tmp[2];
50 tmp[0] = subchar;
51 tmp[1] = '\0';
52 return (-1 != SkStrFind(string, tmp));
53 }
54
SkStrDup(const char string[])55 static inline char *SkStrDup(const char string[]) {
56 char *ret = (char *) sk_malloc_throw(strlen(string)+1);
57 memcpy(ret,string,strlen(string)+1);
58 return ret;
59 }
60
61 /*
62 * The SkStrAppend... methods will write into the provided buffer, assuming it is large enough.
63 * Each method has an associated const (e.g. SkStrAppendU32_MaxSize) which will be the largest
64 * value needed for that method's buffer.
65 *
66 * char storage[SkStrAppendU32_MaxSize];
67 * SkStrAppendU32(storage, value);
68 *
69 * Note : none of the SkStrAppend... methods write a terminating 0 to their buffers. Instead,
70 * the methods return the ptr to the end of the written part of the buffer. This can be used
71 * to compute the length, and/or know where to write a 0 if that is desired.
72 *
73 * char storage[SkStrAppendU32_MaxSize + 1];
74 * char* stop = SkStrAppendU32(storage, value);
75 * size_t len = stop - storage;
76 * *stop = 0; // valid, since storage was 1 byte larger than the max.
77 */
78
79 #define SkStrAppendU32_MaxSize 10
80 char* SkStrAppendU32(char buffer[], uint32_t);
81 #define SkStrAppendU64_MaxSize 20
82 char* SkStrAppendU64(char buffer[], uint64_t, int minDigits);
83
84 #define SkStrAppendS32_MaxSize (SkStrAppendU32_MaxSize + 1)
85 char* SkStrAppendS32(char buffer[], int32_t);
86 #define SkStrAppendS64_MaxSize (SkStrAppendU64_MaxSize + 1)
87 char* SkStrAppendS64(char buffer[], int64_t, int minDigits);
88
89 /**
90 * Floats have at most 8 significant digits, so we limit our %g to that.
91 * However, the total string could be 15 characters: -1.2345678e-005
92 *
93 * In theory we should only expect up to 2 digits for the exponent, but on
94 * some platforms we have seen 3 (as in the example above).
95 */
96 #define SkStrAppendScalar_MaxSize 15
97
98 /**
99 * Write the scaler in decimal format into buffer, and return a pointer to
100 * the next char after the last one written. Note: a terminating 0 is not
101 * written into buffer, which must be at least SkStrAppendScalar_MaxSize.
102 * Thus if the caller wants to add a 0 at the end, buffer must be at least
103 * SkStrAppendScalar_MaxSize + 1 bytes large.
104 */
105 #define SkStrAppendScalar SkStrAppendFloat
106
107 char* SkStrAppendFloat(char buffer[], float);
108 char* SkStrAppendFixed(char buffer[], SkFixed);
109
110 /** \class SkString
111
112 Light weight class for managing strings. Uses reference
113 counting to make string assignments and copies very fast
114 with no extra RAM cost. Assumes UTF8 encoding.
115 */
116 class SK_API SkString {
117 public:
118 SkString();
119 explicit SkString(size_t len);
120 explicit SkString(const char text[]);
121 SkString(const char text[], size_t len);
122 SkString(const SkString&);
123 ~SkString();
124
isEmpty()125 bool isEmpty() const { return 0 == fRec->fLength; }
size()126 size_t size() const { return (size_t) fRec->fLength; }
c_str()127 const char* c_str() const { return fRec->data(); }
128 char operator[](size_t n) const { return this->c_str()[n]; }
129
130 bool equals(const SkString&) const;
131 bool equals(const char text[]) const;
132 bool equals(const char text[], size_t len) const;
133
startsWith(const char prefixStr[])134 bool startsWith(const char prefixStr[]) const {
135 return SkStrStartsWith(fRec->data(), prefixStr);
136 }
startsWith(const char prefixChar)137 bool startsWith(const char prefixChar) const {
138 return SkStrStartsWith(fRec->data(), prefixChar);
139 }
endsWith(const char suffixStr[])140 bool endsWith(const char suffixStr[]) const {
141 return SkStrEndsWith(fRec->data(), suffixStr);
142 }
endsWith(const char suffixChar)143 bool endsWith(const char suffixChar) const {
144 return SkStrEndsWith(fRec->data(), suffixChar);
145 }
contains(const char substring[])146 bool contains(const char substring[]) const {
147 return SkStrContains(fRec->data(), substring);
148 }
contains(const char subchar)149 bool contains(const char subchar) const {
150 return SkStrContains(fRec->data(), subchar);
151 }
find(const char substring[])152 int find(const char substring[]) const {
153 return SkStrFind(fRec->data(), substring);
154 }
155
156 friend bool operator==(const SkString& a, const SkString& b) {
157 return a.equals(b);
158 }
159 friend bool operator!=(const SkString& a, const SkString& b) {
160 return !a.equals(b);
161 }
162
163 // these methods edit the string
164
165 SkString& operator=(const SkString&);
166 SkString& operator=(const char text[]);
167
168 char* writable_str();
169 char& operator[](size_t n) { return this->writable_str()[n]; }
170
171 void reset();
resize(size_t len)172 void resize(size_t len) { this->set(NULL, len); }
set(const SkString & src)173 void set(const SkString& src) { *this = src; }
174 void set(const char text[]);
175 void set(const char text[], size_t len);
176 void setUTF16(const uint16_t[]);
177 void setUTF16(const uint16_t[], size_t len);
178
insert(size_t offset,const SkString & src)179 void insert(size_t offset, const SkString& src) { this->insert(offset, src.c_str(), src.size()); }
180 void insert(size_t offset, const char text[]);
181 void insert(size_t offset, const char text[], size_t len);
182 void insertUnichar(size_t offset, SkUnichar);
183 void insertS32(size_t offset, int32_t value);
184 void insertS64(size_t offset, int64_t value, int minDigits = 0);
185 void insertU32(size_t offset, uint32_t value);
186 void insertU64(size_t offset, uint64_t value, int minDigits = 0);
187 void insertHex(size_t offset, uint32_t value, int minDigits = 0);
188 void insertScalar(size_t offset, SkScalar);
189
append(const SkString & str)190 void append(const SkString& str) { this->insert((size_t)-1, str); }
append(const char text[])191 void append(const char text[]) { this->insert((size_t)-1, text); }
append(const char text[],size_t len)192 void append(const char text[], size_t len) { this->insert((size_t)-1, text, len); }
appendUnichar(SkUnichar uni)193 void appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); }
appendS32(int32_t value)194 void appendS32(int32_t value) { this->insertS32((size_t)-1, value); }
195 void appendS64(int64_t value, int minDigits = 0) { this->insertS64((size_t)-1, value, minDigits); }
appendU32(uint32_t value)196 void appendU32(uint32_t value) { this->insertU32((size_t)-1, value); }
197 void appendU64(uint64_t value, int minDigits = 0) { this->insertU64((size_t)-1, value, minDigits); }
198 void appendHex(uint32_t value, int minDigits = 0) { this->insertHex((size_t)-1, value, minDigits); }
appendScalar(SkScalar value)199 void appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
200
prepend(const SkString & str)201 void prepend(const SkString& str) { this->insert(0, str); }
prepend(const char text[])202 void prepend(const char text[]) { this->insert(0, text); }
prepend(const char text[],size_t len)203 void prepend(const char text[], size_t len) { this->insert(0, text, len); }
prependUnichar(SkUnichar uni)204 void prependUnichar(SkUnichar uni) { this->insertUnichar(0, uni); }
prependS32(int32_t value)205 void prependS32(int32_t value) { this->insertS32(0, value); }
206 void prependS64(int32_t value, int minDigits = 0) { this->insertS64(0, value, minDigits); }
207 void prependHex(uint32_t value, int minDigits = 0) { this->insertHex(0, value, minDigits); }
prependScalar(SkScalar value)208 void prependScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
209
210 void printf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
211 void appendf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
212 void appendVAList(const char format[], va_list);
213 void prependf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
214 void prependVAList(const char format[], va_list);
215
216 void remove(size_t offset, size_t length);
217
218 SkString& operator+=(const SkString& s) { this->append(s); return *this; }
219 SkString& operator+=(const char text[]) { this->append(text); return *this; }
220 SkString& operator+=(const char c) { this->append(&c, 1); return *this; }
221
222 /**
223 * Swap contents between this and other. This function is guaranteed
224 * to never fail or throw.
225 */
226 void swap(SkString& other);
227
228 private:
229 struct Rec {
230 public:
231 uint32_t fLength; // logically size_t, but we want it to stay 32bits
232 int32_t fRefCnt;
233 char fBeginningOfData;
234
dataRec235 char* data() { return &fBeginningOfData; }
dataRec236 const char* data() const { return &fBeginningOfData; }
237 };
238 Rec* fRec;
239
240 #ifdef SK_DEBUG
241 void validate() const;
242 #else
validate()243 void validate() const {}
244 #endif
245
246 static const Rec gEmptyRec;
247 static Rec* AllocRec(const char text[], size_t len);
248 static Rec* RefRec(Rec*);
249 };
250
251 /// Creates a new string and writes into it using a printf()-style format.
252 SkString SkStringPrintf(const char* format, ...);
253
254 // Specialized to take advantage of SkString's fast swap path. The unspecialized function is
255 // declared in SkTypes.h and called by SkTSort.
SkTSwap(SkString & a,SkString & b)256 template <> inline void SkTSwap(SkString& a, SkString& b) {
257 a.swap(b);
258 }
259
260 // Split str on any characters in delimiters into out. (Think, strtok with a sane API.)
261 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out);
262
263 #endif
264