reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 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 | |
| 49 | bool isEmpty() const { return fRec->fLength == 0; } |
| 50 | size_t size() const { return (size_t) fRec->fLength; } |
| 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 | |
| 57 | bool startsWith(const char prefix[]) const |
| 58 | { |
| 59 | return SkStrStartsWith(fRec->data(), prefix); |
| 60 | } |
| 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(); |
| 82 | void resize(size_t len) { this->set(NULL, len); } |
| 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 | |
| 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 | |
| 97 | void append(const SkString& str) { this->insert((size_t)-1, str); } |
| 98 | void append(const char text[]) { this->insert((size_t)-1, text); } |
| 99 | void append(const char text[], size_t len) { this->insert((size_t)-1, text, len); } |
| 100 | void appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); } |
| 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); } |
| 103 | void appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); } |
| 104 | |
| 105 | void prepend(const SkString& str) { this->insert(0, str); } |
| 106 | void prepend(const char text[]) { this->insert(0, text); } |
| 107 | void prepend(const char text[], size_t len) { this->insert(0, text, len); } |
| 108 | void prependUnichar(SkUnichar uni) { this->insertUnichar(0, uni); } |
| 109 | void prependS32(int32_t value) { this->insertS32(0, value); } |
| 110 | void prependHex(uint32_t value, int minDigits = 0) { this->insertHex(0, value, minDigits); } |
| 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 | /** @cond UNIT_TEST */ |
| 125 | SkDEBUGCODE(static void UnitTest();) |
| 126 | /** @endcond */ |
| 127 | |
| 128 | private: |
| 129 | struct Rec { |
| 130 | public: |
| 131 | uint16_t fLength; |
| 132 | uint16_t fRefCnt; |
| 133 | char fBeginningOfData; |
| 134 | |
| 135 | char* data() { return &fBeginningOfData; } |
| 136 | const char* data() const { return &fBeginningOfData; } |
| 137 | }; |
| 138 | Rec* fRec; |
| 139 | |
| 140 | #ifdef SK_DEBUG |
| 141 | const char* fStr; |
| 142 | void validate() const; |
| 143 | #else |
| 144 | void validate() const {} |
| 145 | #endif |
| 146 | |
| 147 | static const Rec gEmptyRec; |
| 148 | static Rec* AllocRec(const char text[], U16CPU len); |
| 149 | static Rec* RefRec(Rec*); |
| 150 | }; |
| 151 | |
| 152 | class SkAutoUCS2 { |
| 153 | public: |
| 154 | SkAutoUCS2(const char utf8[]); |
| 155 | ~SkAutoUCS2(); |
| 156 | |
| 157 | /** This returns the number of ucs2 characters |
| 158 | */ |
| 159 | int count() const { return fCount; } |
| 160 | /** This returns a null terminated ucs2 string |
| 161 | */ |
| 162 | const uint16_t* getUCS2() const { return fUCS2; } |
| 163 | |
| 164 | private: |
| 165 | int fCount; |
| 166 | uint16_t* fUCS2; |
| 167 | }; |
| 168 | |
| 169 | #endif |
| 170 | |