epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkString_DEFINED |
| 11 | #define SkString_DEFINED |
| 12 | |
bungeman | bf521ff | 2016-02-17 13:13:44 -0800 | [diff] [blame] | 13 | #include "../private/SkTArray.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkScalar.h" |
| 15 | |
bsalomon@google.com | b9cf393 | 2013-03-07 18:05:08 +0000 | [diff] [blame] | 16 | #include <stdarg.h> |
| 17 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | /* Some helper functions for C strings |
| 19 | */ |
| 20 | |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 21 | static bool SkStrStartsWith(const char string[], const char prefixStr[]) { |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 22 | SkASSERT(string); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 23 | SkASSERT(prefixStr); |
| 24 | return !strncmp(string, prefixStr, strlen(prefixStr)); |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 25 | } |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | int SkStrStartsWithOneOf(const char string[], const char prefixes[]); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 35 | |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 36 | static int SkStrFind(const char string[], const char substring[]) { |
humper@google.com | 61a972f | 2013-01-14 19:03:46 +0000 | [diff] [blame] | 37 | const char *first = strstr(string, substring); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 38 | if (NULL == first) return -1; |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 39 | return SkToInt(first - &string[0]); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 40 | } |
| 41 | |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 42 | static int SkStrFindLastOf(const char string[], const char subchar) { |
| 43 | const char* last = strrchr(string, subchar); |
| 44 | if (NULL == last) return -1; |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 45 | return SkToInt(last - &string[0]); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 46 | } |
| 47 | |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 48 | static bool SkStrContains(const char string[], const char substring[]) { |
| 49 | SkASSERT(string); |
| 50 | SkASSERT(substring); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 51 | return (-1 != SkStrFind(string, substring)); |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 52 | } |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 53 | static bool SkStrContains(const char string[], const char subchar) { |
| 54 | SkASSERT(string); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 55 | char tmp[2]; |
| 56 | tmp[0] = subchar; |
| 57 | tmp[1] = '\0'; |
| 58 | return (-1 != SkStrFind(string, tmp)); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 59 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 61 | static inline char *SkStrDup(const char string[]) { |
| 62 | char *ret = (char *) sk_malloc_throw(strlen(string)+1); |
robertphillips@google.com | 37ebe3f | 2013-03-04 20:03:44 +0000 | [diff] [blame] | 63 | memcpy(ret,string,strlen(string)+1); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
reed | d23f45d | 2014-11-07 07:37:33 -0800 | [diff] [blame] | 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 | */ |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 84 | |
epoger@google.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 85 | #define SkStrAppendU32_MaxSize 10 |
epoger@google.com | 27d3025 | 2013-06-19 18:52:42 +0000 | [diff] [blame] | 86 | char* SkStrAppendU32(char buffer[], uint32_t); |
epoger@google.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 87 | #define SkStrAppendU64_MaxSize 20 |
epoger@google.com | 27d3025 | 2013-06-19 18:52:42 +0000 | [diff] [blame] | 88 | char* SkStrAppendU64(char buffer[], uint64_t, int minDigits); |
epoger@google.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 89 | |
| 90 | #define SkStrAppendS32_MaxSize (SkStrAppendU32_MaxSize + 1) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | char* SkStrAppendS32(char buffer[], int32_t); |
epoger@google.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 92 | #define SkStrAppendS64_MaxSize (SkStrAppendU64_MaxSize + 1) |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 93 | char* SkStrAppendS64(char buffer[], int64_t, int minDigits); |
reed@google.com | 8072e4f | 2011-03-01 15:44:08 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Floats have at most 8 significant digits, so we limit our %g to that. |
reed@google.com | e280f1d | 2011-03-03 18:43:14 +0000 | [diff] [blame] | 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). |
reed@google.com | 8072e4f | 2011-03-01 15:44:08 +0000 | [diff] [blame] | 101 | */ |
reed@google.com | e280f1d | 2011-03-03 18:43:14 +0000 | [diff] [blame] | 102 | #define SkStrAppendScalar_MaxSize 15 |
reed@google.com | 8072e4f | 2011-03-01 15:44:08 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 111 | #define SkStrAppendScalar SkStrAppendFloat |
vandebo@chromium.org | 677cbed | 2011-03-03 18:20:12 +0000 | [diff] [blame] | 112 | |
vandebo@chromium.org | 677cbed | 2011-03-03 18:20:12 +0000 | [diff] [blame] | 113 | char* SkStrAppendFloat(char buffer[], float); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | |
| 115 | /** \class SkString |
| 116 | |
| 117 | Light weight class for managing strings. Uses reference |
| 118 | counting to make string assignments and copies very fast |
| 119 | with no extra RAM cost. Assumes UTF8 encoding. |
| 120 | */ |
vandebo@chromium.org | a56fedc | 2012-10-18 23:26:44 +0000 | [diff] [blame] | 121 | class SK_API SkString { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | public: |
| 123 | SkString(); |
| 124 | explicit SkString(size_t len); |
| 125 | explicit SkString(const char text[]); |
| 126 | SkString(const char text[], size_t len); |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 127 | SkString(const SkString&); |
bungeman | 9d55297 | 2016-02-07 18:42:54 -0800 | [diff] [blame] | 128 | SkString(SkString&&); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | ~SkString(); |
| 130 | |
reed@google.com | 4bce115 | 2011-09-14 16:13:58 +0000 | [diff] [blame] | 131 | bool isEmpty() const { return 0 == fRec->fLength; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | size_t size() const { return (size_t) fRec->fLength; } |
| 133 | const char* c_str() const { return fRec->data(); } |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 134 | char operator[](size_t n) const { return this->c_str()[n]; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 136 | bool equals(const SkString&) const; |
| 137 | bool equals(const char text[]) const; |
| 138 | bool equals(const char text[], size_t len) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 140 | bool startsWith(const char prefixStr[]) const { |
| 141 | return SkStrStartsWith(fRec->data(), prefixStr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | } |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 143 | bool startsWith(const char prefixChar) const { |
| 144 | return SkStrStartsWith(fRec->data(), prefixChar); |
| 145 | } |
| 146 | bool endsWith(const char suffixStr[]) const { |
| 147 | return SkStrEndsWith(fRec->data(), suffixStr); |
| 148 | } |
| 149 | bool endsWith(const char suffixChar) const { |
| 150 | return SkStrEndsWith(fRec->data(), suffixChar); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | } |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 152 | bool contains(const char substring[]) const { |
| 153 | return SkStrContains(fRec->data(), substring); |
| 154 | } |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 155 | bool contains(const char subchar) const { |
| 156 | return SkStrContains(fRec->data(), subchar); |
| 157 | } |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 158 | int find(const char substring[]) const { |
| 159 | return SkStrFind(fRec->data(), substring); |
| 160 | } |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 161 | int findLastOf(const char subchar) const { |
| 162 | return SkStrFindLastOf(fRec->data(), subchar); |
| 163 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | |
reed@google.com | b530ef5 | 2011-07-20 19:55:42 +0000 | [diff] [blame] | 165 | friend bool operator==(const SkString& a, const SkString& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | return a.equals(b); |
| 167 | } |
reed@google.com | b530ef5 | 2011-07-20 19:55:42 +0000 | [diff] [blame] | 168 | friend bool operator!=(const SkString& a, const SkString& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | return !a.equals(b); |
| 170 | } |
| 171 | |
| 172 | // these methods edit the string |
| 173 | |
reed@google.com | b530ef5 | 2011-07-20 19:55:42 +0000 | [diff] [blame] | 174 | SkString& operator=(const SkString&); |
bungeman | 9d55297 | 2016-02-07 18:42:54 -0800 | [diff] [blame] | 175 | SkString& operator=(SkString&&); |
reed@google.com | b530ef5 | 2011-07-20 19:55:42 +0000 | [diff] [blame] | 176 | SkString& operator=(const char text[]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | |
reed@google.com | b530ef5 | 2011-07-20 19:55:42 +0000 | [diff] [blame] | 178 | char* writable_str(); |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 179 | char& operator[](size_t n) { return this->writable_str()[n]; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 181 | void reset(); |
bungeman | b2885d5 | 2015-08-31 14:36:48 -0700 | [diff] [blame] | 182 | /** Destructive resize, does not preserve contents. */ |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 183 | void resize(size_t len) { this->set(NULL, len); } |
| 184 | void set(const SkString& src) { *this = src; } |
| 185 | void set(const char text[]); |
| 186 | void set(const char text[], size_t len); |
| 187 | void setUTF16(const uint16_t[]); |
| 188 | void setUTF16(const uint16_t[], size_t len); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 190 | 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.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 196 | void insertU32(size_t offset, uint32_t value); |
| 197 | void insertU64(size_t offset, uint64_t value, int minDigits = 0); |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 198 | void insertHex(size_t offset, uint32_t value, int minDigits = 0); |
| 199 | void insertScalar(size_t offset, SkScalar); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 201 | 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.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 207 | 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.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 209 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 211 | |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 212 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | |
senorblanco@chromium.org | 3a67a66 | 2012-07-09 18:22:08 +0000 | [diff] [blame] | 221 | void printf(const char format[], ...) SK_PRINTF_LIKE(2, 3); |
| 222 | void appendf(const char format[], ...) SK_PRINTF_LIKE(2, 3); |
commit-bot@chromium.org | ce0e4ef | 2013-11-21 17:20:17 +0000 | [diff] [blame] | 223 | void appendVAList(const char format[], va_list); |
senorblanco@chromium.org | 3a67a66 | 2012-07-09 18:22:08 +0000 | [diff] [blame] | 224 | void prependf(const char format[], ...) SK_PRINTF_LIKE(2, 3); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 225 | void prependVAList(const char format[], va_list); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 227 | void remove(size_t offset, size_t length); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 229 | SkString& operator+=(const SkString& s) { this->append(s); return *this; } |
| 230 | SkString& operator+=(const char text[]) { this->append(text); return *this; } |
epoger@google.com | 4a8084c | 2013-05-09 20:01:26 +0000 | [diff] [blame] | 231 | SkString& operator+=(const char c) { this->append(&c, 1); return *this; } |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 232 | |
mike@reedtribe.org | 4e1d3ac | 2011-04-10 01:04:37 +0000 | [diff] [blame] | 233 | /** |
| 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.com | 8072e4f | 2011-03-01 15:44:08 +0000 | [diff] [blame] | 238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | private: |
| 240 | struct Rec { |
| 241 | public: |
reed@google.com | 8cb1088 | 2013-06-04 20:36:52 +0000 | [diff] [blame] | 242 | uint32_t fLength; // logically size_t, but we want it to stay 32bits |
reed@google.com | 4bce115 | 2011-09-14 16:13:58 +0000 | [diff] [blame] | 243 | int32_t fRefCnt; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | char fBeginningOfData; |
| 245 | |
| 246 | char* data() { return &fBeginningOfData; } |
| 247 | const char* data() const { return &fBeginningOfData; } |
| 248 | }; |
| 249 | Rec* fRec; |
| 250 | |
| 251 | #ifdef SK_DEBUG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | void validate() const; |
| 253 | #else |
| 254 | void validate() const {} |
| 255 | #endif |
| 256 | |
| 257 | static const Rec gEmptyRec; |
reed@google.com | 4bce115 | 2011-09-14 16:13:58 +0000 | [diff] [blame] | 258 | static Rec* AllocRec(const char text[], size_t len); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | static Rec* RefRec(Rec*); |
| 260 | }; |
| 261 | |
tomhudson@google.com | 3a1f6a0 | 2011-06-30 14:39:52 +0000 | [diff] [blame] | 262 | /// Creates a new string and writes into it using a printf()-style format. |
| 263 | SkString SkStringPrintf(const char* format, ...); |
| 264 | |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 265 | // Specialized to take advantage of SkString's fast swap path. The unspecialized function is |
| 266 | // declared in SkTypes.h and called by SkTSort. |
| 267 | template <> inline void SkTSwap(SkString& a, SkString& b) { |
| 268 | a.swap(b); |
| 269 | } |
| 270 | |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 271 | enum SkStrSplitMode { |
| 272 | // Strictly return all results. If the input is ",," and the separator is ',' this will return |
| 273 | // an array of three empty strings. |
| 274 | kStrict_SkStrSplitMode, |
| 275 | |
| 276 | // Only nonempty results will be added to the results. Multiple separators will be |
| 277 | // coalesced. Separators at the beginning and end of the input will be ignored. If the input is |
| 278 | // ",," and the separator is ',', this will return an empty vector. |
| 279 | kCoalesce_SkStrSplitMode |
| 280 | }; |
| 281 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 282 | // Split str on any characters in delimiters into out. (Think, strtok with a sane API.) |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 283 | void SkStrSplit(const char* str, const char* delimiters, SkStrSplitMode splitMode, |
| 284 | SkTArray<SkString>* out); |
| 285 | inline void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out) { |
| 286 | SkStrSplit(str, delimiters, kCoalesce_SkStrSplitMode, out); |
| 287 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 288 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 289 | #endif |