1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SkString_DEFINED 18 #define SkString_DEFINED 19 20 #include "SkScalar.h" 21 22 /* Some helper functions for C strings 23 */ 24 25 bool SkStrStartsWith(const char string[], const char prefix[]); 26 bool SkStrEndsWith(const char string[], const char suffix[]); 27 int SkStrStartsWithOneOf(const char string[], const char prefixes[]); 28 29 #define SkStrAppendS32_MaxSize 11 30 char* SkStrAppendS32(char buffer[], int32_t); 31 #define SkStrAppendScalar_MaxSize 11 32 char* SkStrAppendScalar(char buffer[], SkScalar); 33 34 /** \class SkString 35 36 Light weight class for managing strings. Uses reference 37 counting to make string assignments and copies very fast 38 with no extra RAM cost. Assumes UTF8 encoding. 39 */ 40 class SkString { 41 public: 42 SkString(); 43 explicit SkString(size_t len); 44 explicit SkString(const char text[]); 45 SkString(const char text[], size_t len); 46 explicit SkString(const SkString&); 47 ~SkString(); 48 isEmpty()49 bool isEmpty() const { return fRec->fLength == 0; } size()50 size_t size() const { return (size_t) fRec->fLength; } c_str()51 const char* c_str() const { return fRec->data(); } 52 53 bool equals(const SkString&) const; 54 bool equals(const char text[]) const; 55 bool equals(const char text[], size_t len) const; 56 startsWith(const char prefix[])57 bool startsWith(const char prefix[]) const 58 { 59 return SkStrStartsWith(fRec->data(), prefix); 60 } endsWith(const char suffix[])61 bool endsWith(const char suffix[]) const 62 { 63 return SkStrEndsWith(fRec->data(), suffix); 64 } 65 66 friend int operator==(const SkString& a, const SkString& b) 67 { 68 return a.equals(b); 69 } 70 friend int operator!=(const SkString& a, const SkString& b) 71 { 72 return !a.equals(b); 73 } 74 75 // these methods edit the string 76 77 SkString& operator=(const SkString&); 78 79 char* writable_str(); 80 81 void reset(); resize(size_t len)82 void resize(size_t len) { this->set(NULL, len); } set(const SkString & src)83 void set(const SkString& src) { *this = src; } 84 void set(const char text[]); 85 void set(const char text[], size_t len); 86 void setUTF16(const uint16_t[]); 87 void setUTF16(const uint16_t[], size_t len); 88 insert(size_t offset,const SkString & src)89 void insert(size_t offset, const SkString& src) { this->insert(offset, src.c_str(), src.size()); } 90 void insert(size_t offset, const char text[]); 91 void insert(size_t offset, const char text[], size_t len); 92 void insertUnichar(size_t offset, SkUnichar); 93 void insertS32(size_t offset, int32_t value); 94 void insertHex(size_t offset, uint32_t value, int minDigits = 0); 95 void insertScalar(size_t offset, SkScalar); 96 append(const SkString & str)97 void append(const SkString& str) { this->insert((size_t)-1, str); } append(const char text[])98 void append(const char text[]) { this->insert((size_t)-1, text); } append(const char text[],size_t len)99 void append(const char text[], size_t len) { this->insert((size_t)-1, text, len); } appendUnichar(SkUnichar uni)100 void appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); } appendS32(int32_t value)101 void appendS32(int32_t value) { this->insertS32((size_t)-1, value); } 102 void appendHex(uint32_t value, int minDigits = 0) { this->insertHex((size_t)-1, value, minDigits); } appendScalar(SkScalar value)103 void appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); } 104 prepend(const SkString & str)105 void prepend(const SkString& str) { this->insert(0, str); } prepend(const char text[])106 void prepend(const char text[]) { this->insert(0, text); } prepend(const char text[],size_t len)107 void prepend(const char text[], size_t len) { this->insert(0, text, len); } prependUnichar(SkUnichar uni)108 void prependUnichar(SkUnichar uni) { this->insertUnichar(0, uni); } prependS32(int32_t value)109 void prependS32(int32_t value) { this->insertS32(0, value); } 110 void prependHex(uint32_t value, int minDigits = 0) { this->insertHex(0, value, minDigits); } prependScalar(SkScalar value)111 void prependScalar(SkScalar value) { this->insertScalar((size_t)-1, value); } 112 113 void printf(const char format[], ...); 114 void appendf(const char format[], ...); 115 void prependf(const char format[], ...); 116 117 void remove(size_t offset, size_t length); 118 119 /** Swap contents between this and other. This function is guaranteed 120 to never fail or throw. 121 */ 122 void swap(SkString& other); 123 124 private: 125 struct Rec { 126 public: 127 uint16_t fLength; 128 uint16_t fRefCnt; 129 char fBeginningOfData; 130 dataRec131 char* data() { return &fBeginningOfData; } dataRec132 const char* data() const { return &fBeginningOfData; } 133 }; 134 Rec* fRec; 135 136 #ifdef SK_DEBUG 137 const char* fStr; 138 void validate() const; 139 #else validate()140 void validate() const {} 141 #endif 142 143 static const Rec gEmptyRec; 144 static Rec* AllocRec(const char text[], U16CPU len); 145 static Rec* RefRec(Rec*); 146 }; 147 148 class SkAutoUCS2 { 149 public: 150 SkAutoUCS2(const char utf8[]); 151 ~SkAutoUCS2(); 152 153 /** This returns the number of ucs2 characters 154 */ count()155 int count() const { return fCount; } 156 /** This returns a null terminated ucs2 string 157 */ getUCS2()158 const uint16_t* getUCS2() const { return fUCS2; } 159 160 private: 161 int fCount; 162 uint16_t* fUCS2; 163 }; 164 165 #endif 166 167