2 * This file is part of the DOM implementation for KDE.
4 * (C) 1999 Lars Knoll (knoll@kde.org)
5 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 // This file would be called String.h, but that conflicts with <string.h>
28 // on systems without case-sensitive file systems.
30 #include "StringImpl.h"
33 #include <CoreFoundation/CoreFoundation.h>
45 * Currently, strings are explicitly shared (they behave like pointers), meaning
46 * that modifications to one instance will also modify all others. If you
47 * wish to get a String that is independent, use @ref copy(). In the future,
48 * we plan to change this to have immutable copy on write semantics.
53 String() { } // gives null string, distinguishable from an empty string
54 String(const UChar*, unsigned length);
55 explicit String(const UChar*); // Specifically and explicitly for null terminated UTF-16
56 String(const KJS::Identifier&);
57 String(const KJS::UString&);
59 String(const char*, unsigned length);
60 String(StringImpl* i) : m_impl(i) { }
62 operator KJS::Identifier() const;
63 operator KJS::UString() const;
65 unsigned length() const;
66 const UChar* characters() const;
67 const UChar* charactersWithNullTermination();
69 UChar operator[](unsigned i) const; // if i >= length(), returns 0
71 bool contains(UChar c) const { return find(c) != -1; }
72 bool contains(const char* str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != -1; }
73 bool contains(const String& str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != -1; }
75 int find(UChar c, int start = 0) const
76 { return m_impl ? m_impl->find(c, start) : -1; }
77 int find(const char* str, int start = 0, bool caseSensitive = true) const
78 { return m_impl ? m_impl->find(str, start, caseSensitive) : -1; }
79 int find(const String& str, int start = 0, bool caseSensitive = true) const
80 { return m_impl ? m_impl->find(str.impl(), start, caseSensitive) : -1; }
82 bool startsWith(const String& s, bool caseSensitive = true) const
83 { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); }
84 bool endsWith(const String& s, bool caseSensitive = true) const
85 { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(); }
87 void append(const String&);
90 void insert(const String&, unsigned pos);
92 String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; }
93 String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; }
94 String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; }
96 void truncate(unsigned len);
97 void remove(unsigned pos, int len = 1);
99 String substring(unsigned pos, unsigned len = UINT_MAX) const;
100 String left(unsigned len) const { return substring(0, len); }
101 String right(unsigned len) const { return substring(length() - len, len); }
103 // Splits the string into two. The original string gets truncated to pos, and the rest is returned.
104 String split(unsigned pos);
106 // Returns a lowercase/uppercase version of the string
107 String lower() const;
108 String upper() const;
110 // Return the string with case folded for case insensitive comparison.
111 String foldCase() const;
113 static String number(int);
114 static String number(unsigned);
115 static String number(long);
116 static String number(unsigned long);
117 static String number(long long);
118 static String number(unsigned long long);
119 static String number(double);
121 static String sprintf(const char *, ...)
123 __attribute__ ((format (printf, 1, 2)))
127 int toInt(bool* ok = 0) const;
128 Length* toLengthArray(int& len) const;
129 Length* toCoordsArray(int& len) const;
130 bool percentage(int &_percentage) const;
134 bool isNull() const { return !m_impl; }
135 bool isEmpty() const;
137 StringImpl* impl() const { return m_impl.get(); }
141 CFStringRef createCFString() const { return m_impl ? m_impl->createCFString() : CFSTR(""); }
146 operator NSString*() const { if (!m_impl) return @""; return *m_impl; }
150 String(const QString&);
151 operator QString() const;
155 // For debugging only, leaks memory.
156 Vector<char> ascii() const;
159 CString latin1() const;
160 CString utf8() const;
162 String(const DeprecatedString&);
163 DeprecatedString deprecatedString() const;
166 RefPtr<StringImpl> m_impl;
169 String operator+(const String&, const String&);
170 String operator+(const String&, const char*);
171 String operator+(const char*, const String&);
173 inline String& operator+=(String& a, const String& b) { a.append(b); return a; }
175 inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
176 inline bool operator==(const String& a, const char* b) { return equal(a.impl(), b); }
177 inline bool operator==(const char* a, const String& b) { return equal(a, b.impl()); }
179 inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
180 inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), b); }
181 inline bool operator!=(const char* a, const String& b) { return !equal(a, b.impl()); }
183 inline bool equalIgnoringCase(const String& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
184 inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgnoringCase(a.impl(), b); }
185 inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgnoringCase(a, b.impl()); }
187 bool operator==(const String& a, const DeprecatedString& b);
188 inline bool operator==(const DeprecatedString& b, const String& a) { return a == b; }
189 inline bool operator!=(const String& a, const DeprecatedString& b) { return !(a == b); }
190 inline bool operator!=(const DeprecatedString& b, const String& a ) { return !(a == b); }
196 // StrHash is the default hash for String
197 template<typename T> struct DefaultHash;
198 template<typename T> struct StrHash;
199 template<> struct DefaultHash<WebCore::String> {
200 typedef StrHash<WebCore::String> Hash;