• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 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     char operator[](size_t n) const { return this->c_str()[n]; }
53 
54     bool    equals(const SkString&) const;
55     bool    equals(const char text[]) const;
56     bool    equals(const char text[], size_t len) const;
57 
startsWith(const char prefix[])58     bool    startsWith(const char prefix[]) const
59     {
60         return SkStrStartsWith(fRec->data(), prefix);
61     }
endsWith(const char suffix[])62     bool    endsWith(const char suffix[]) const
63     {
64         return SkStrEndsWith(fRec->data(), suffix);
65     }
66 
67     friend int operator==(const SkString& a, const SkString& b)
68     {
69         return a.equals(b);
70     }
71     friend int operator!=(const SkString& a, const SkString& b)
72     {
73         return !a.equals(b);
74     }
75 
76     // these methods edit the string
77 
78     SkString&   operator=(const SkString&);
79 
80     char*   writable_str();
81     char& operator[](size_t n) { return this->writable_str()[n]; }
82 
83     void    reset();
resize(size_t len)84     void    resize(size_t len) { this->set(NULL, len); }
set(const SkString & src)85     void    set(const SkString& src) { *this = src; }
86     void    set(const char text[]);
87     void    set(const char text[], size_t len);
88     void    setUTF16(const uint16_t[]);
89     void    setUTF16(const uint16_t[], size_t len);
90 
insert(size_t offset,const SkString & src)91     void    insert(size_t offset, const SkString& src) { this->insert(offset, src.c_str(), src.size()); }
92     void    insert(size_t offset, const char text[]);
93     void    insert(size_t offset, const char text[], size_t len);
94     void    insertUnichar(size_t offset, SkUnichar);
95     void    insertS32(size_t offset, int32_t value);
96     void    insertHex(size_t offset, uint32_t value, int minDigits = 0);
97     void    insertScalar(size_t offset, SkScalar);
98 
append(const SkString & str)99     void    append(const SkString& str) { this->insert((size_t)-1, str); }
append(const char text[])100     void    append(const char text[]) { this->insert((size_t)-1, text); }
append(const char text[],size_t len)101     void    append(const char text[], size_t len) { this->insert((size_t)-1, text, len); }
appendUnichar(SkUnichar uni)102     void    appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); }
appendS32(int32_t value)103     void    appendS32(int32_t value) { this->insertS32((size_t)-1, value); }
104     void    appendHex(uint32_t value, int minDigits = 0) { this->insertHex((size_t)-1, value, minDigits); }
appendScalar(SkScalar value)105     void    appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
106 
prepend(const SkString & str)107     void    prepend(const SkString& str) { this->insert(0, str); }
prepend(const char text[])108     void    prepend(const char text[]) { this->insert(0, text); }
prepend(const char text[],size_t len)109     void    prepend(const char text[], size_t len) { this->insert(0, text, len); }
prependUnichar(SkUnichar uni)110     void    prependUnichar(SkUnichar uni) { this->insertUnichar(0, uni); }
prependS32(int32_t value)111     void    prependS32(int32_t value) { this->insertS32(0, value); }
112     void    prependHex(uint32_t value, int minDigits = 0) { this->insertHex(0, value, minDigits); }
prependScalar(SkScalar value)113     void    prependScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
114 
115     void    printf(const char format[], ...);
116     void    appendf(const char format[], ...);
117     void    prependf(const char format[], ...);
118 
119     void    remove(size_t offset, size_t length);
120 
121     /** Swap contents between this and other. This function is guaranteed
122         to never fail or throw.
123     */
124     void    swap(SkString& other);
125 
126 private:
127     struct Rec {
128     public:
129         uint16_t    fLength;
130         uint16_t    fRefCnt;
131         char        fBeginningOfData;
132 
dataRec133         char* data() { return &fBeginningOfData; }
dataRec134         const char* data() const { return &fBeginningOfData; }
135     };
136     Rec* fRec;
137 
138 #ifdef SK_DEBUG
139     const char* fStr;
140     void validate() const;
141 #else
validate()142     void validate() const {}
143 #endif
144 
145     static const Rec gEmptyRec;
146     static Rec* AllocRec(const char text[], U16CPU len);
147     static Rec* RefRec(Rec*);
148 };
149 
150 class SkAutoUCS2 {
151 public:
152     SkAutoUCS2(const char utf8[]);
153     ~SkAutoUCS2();
154 
155     /** This returns the number of ucs2 characters
156     */
count()157     int count() const { return fCount; }
158     /** This returns a null terminated ucs2 string
159     */
getUCS2()160     const uint16_t* getUCS2() const { return fUCS2; }
161 
162 private:
163     int         fCount;
164     uint16_t*   fUCS2;
165 };
166 
167 #endif
168 
169