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