00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef PlatformString_h
00025 #define PlatformString_h
00026
00027
00028
00029
00030 #include "StringImpl.h"
00031
00032 #if PLATFORM(CF)
00033 typedef const struct __CFString * CFStringRef;
00034 #endif
00035
00036 #if PLATFORM(QT)
00037 class QString;
00038 #endif
00039
00040 namespace WebCore {
00041
00042 class CString;
00043
00051 class String {
00052 public:
00053 String() { }
00054 String(const UChar*, unsigned length);
00055 explicit String(const UChar*);
00056 #ifdef __OWB_JS__
00057 String(const KJS::Identifier&);
00058 String(const KJS::UString&);
00059 #endif //__OWB_JS__
00060 String(const char*);
00061 String(const char*, unsigned length);
00062 String(StringImpl* i) : m_impl(i) { }
00063
00064 static String newUninitialized(size_t length, UChar*& characterBuffer);
00065 static String adopt(Vector<UChar>&);
00066
00067 #ifdef __OWB_JS__
00068 operator KJS::Identifier() const;
00069 operator KJS::UString() const;
00070 #endif //__OWB_JS__
00071
00072 unsigned length() const;
00073 const UChar* characters() const;
00074 const UChar* charactersWithNullTermination();
00075
00076 UChar operator[](unsigned i) const;
00077
00078 bool contains(UChar c) const { return find(c) != -1; }
00079 bool contains(const char* str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != -1; }
00080 bool contains(const String& str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != -1; }
00081
00082 int find(UChar c, int start = 0) const
00083 { return m_impl ? m_impl->find(c, start) : -1; }
00084 int find(const char* str, int start = 0, bool caseSensitive = true) const
00085 { return m_impl ? m_impl->find(str, start, caseSensitive) : -1; }
00086 int find(const String& str, int start = 0, bool caseSensitive = true) const
00087 { return m_impl ? m_impl->find(str.impl(), start, caseSensitive) : -1; }
00088
00089 int reverseFind(UChar c, int start = -1) const
00090 { return m_impl ? m_impl->reverseFind(c, start) : -1; }
00091 int reverseFind(const String& str, int start = -1, bool caseSensitive = true) const
00092 { return m_impl ? m_impl->reverseFind(str.impl(), start, caseSensitive) : -1; }
00093
00094 bool startsWith(const String& s, bool caseSensitive = true) const
00095 { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); }
00096 bool endsWith(const String& s, bool caseSensitive = true) const
00097 { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(); }
00098
00099 void append(const String&);
00100 void append(char);
00101 void append(UChar);
00102 void insert(const String&, unsigned pos);
00103 void insert(const UChar*, unsigned length, unsigned pos);
00104
00105 String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; }
00106 String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; }
00107 String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_impl->replace(a.impl(), b.impl()); return *this; }
00108 String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; }
00109
00110 void truncate(unsigned len);
00111 void remove(unsigned pos, int len = 1);
00112
00113 String substring(unsigned pos, unsigned len = UINT_MAX) const;
00114 String left(unsigned len) const { return substring(0, len); }
00115 String right(unsigned len) const { return substring(length() - len, len); }
00116
00117
00118 String lower() const;
00119 String upper() const;
00120
00121 String stripWhiteSpace() const;
00122 String simplifyWhiteSpace() const;
00123
00124
00125 String foldCase() const;
00126
00127 static String number(int);
00128 static String number(unsigned);
00129 static String number(long);
00130 static String number(unsigned long);
00131 static String number(long long);
00132 static String number(unsigned long long);
00133 static String number(double);
00134
00135 static String format(const char *, ...)
00136 #if __GNUC__
00137 __attribute__ ((format (printf, 1, 2)))
00138 #endif
00139 ;
00140
00141 Vector<String> split(const String& separator, bool allowEmptyEntries = false) const;
00142 Vector<String> split(UChar separator, bool allowEmptyEntries = false) const;
00143
00144 int toInt(bool* ok = 0) const;
00145 int64_t toInt64(bool* ok = 0) const;
00146 uint64_t toUInt64(bool* ok = 0) const;
00147 double toDouble(bool* ok = 0) const;
00148 float toFloat(bool* ok = 0) const;
00149 Length* toLengthArray(int& len) const;
00150 Length* toCoordsArray(int& len) const;
00151 bool percentage(int &_percentage) const;
00152
00153 String copy() const;
00154
00155 bool isNull() const { return !m_impl; }
00156 bool isEmpty() const;
00157
00158 StringImpl* impl() const { return m_impl.get(); }
00159
00160 #if PLATFORM(CF)
00161 String(CFStringRef);
00162 CFStringRef createCFString() const;
00163 #endif
00164
00165 #ifdef __OBJC__
00166 String(NSString*);
00167
00168
00169
00170 operator NSString*() const { if (!m_impl) return @""; return *m_impl; }
00171 #endif
00172
00173 #if PLATFORM(QT)
00174 String(const QString&);
00175 String(const QStringRef&);
00176 operator QString() const;
00177 #endif
00178
00179 #if PLATFORM(SYMBIAN)
00180 String(const TDesC&);
00181 operator TPtrC() const { return des(); }
00182 TPtrC des() const { if (!m_impl) return KNullDesC(); return m_impl->des(); }
00183 #endif
00184
00185 #ifndef NDEBUG
00186
00187 Vector<char> ascii() const;
00188 #endif
00189
00190 CString latin1() const;
00191 CString utf8() const;
00192
00193 static String fromUTF8(const char*, size_t);
00194 static String fromUTF8(const char*);
00195
00196
00197 WTF::Unicode::Direction defaultWritingDirection() const { return m_impl ? m_impl->defaultWritingDirection() : WTF::Unicode::LeftToRight; }
00198
00199 String(const DeprecatedString&);
00200 DeprecatedString deprecatedString() const;
00201
00202 private:
00203 RefPtr<StringImpl> m_impl;
00204 };
00205
00206 String operator+(const String&, const String&);
00207 String operator+(const String&, const char*);
00208 String operator+(const char*, const String&);
00209
00210 inline String& operator+=(String& a, const String& b) { a.append(b); return a; }
00211
00212 inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
00213 inline bool operator==(const String& a, const char* b) { return equal(a.impl(), b); }
00214 inline bool operator==(const char* a, const String& b) { return equal(a, b.impl()); }
00215
00216 inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
00217 inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), b); }
00218 inline bool operator!=(const char* a, const String& b) { return !equal(a, b.impl()); }
00219
00220 inline bool equalIgnoringCase(const String& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
00221 inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgnoringCase(a.impl(), b); }
00222 inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgnoringCase(a, b.impl()); }
00223
00224 bool operator==(const String& a, const DeprecatedString& b);
00225 inline bool operator==(const DeprecatedString& b, const String& a) { return a == b; }
00226 inline bool operator!=(const String& a, const DeprecatedString& b) { return !(a == b); }
00227 inline bool operator!=(const DeprecatedString& b, const String& a ) { return !(a == b); }
00228
00229 inline bool operator!(const String& str) { return str.isNull(); }
00230
00231 #ifdef __OBJC__
00232
00233
00234
00235 inline NSString* nsStringNilIfEmpty(const String& str) { return str.isEmpty() ? nil : (NSString*)str; }
00236 #endif
00237
00238 }
00239
00240 namespace WTF {
00241
00242
00243 template<typename T> struct DefaultHash;
00244 template<typename T> struct StrHash;
00245 template<> struct DefaultHash<WebCore::String> {
00246 typedef StrHash<WebCore::String> Hash;
00247 };
00248
00249 }
00250
00251 #endif