gfx/skia/trunk/include/core/SkString.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial