SkString.h 11 KB

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