2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "WebCommon.h"
36 #if WEBKIT_IMPLEMENTATION
37 #include <wtf/Forward.h>
39 #include <base/nullable_string16.h>
40 #include <base/string16.h>
46 class WebStringPrivate;
48 // A UTF-16 string container. It is inexpensive to copy a WebString
51 // WARNING: It is not safe to pass a WebString across threads!!!
55 ~WebString() { reset(); }
57 WebString() : m_private(0) { }
59 WebString(const WebUChar* data, size_t len) : m_private(0)
64 WebString(const WebString& s) : m_private(0) { assign(s); }
66 WebString& operator=(const WebString& s)
72 WEBKIT_EXPORT void reset();
73 WEBKIT_EXPORT void assign(const WebString&);
74 WEBKIT_EXPORT void assign(const WebUChar* data, size_t len);
76 WEBKIT_EXPORT bool equals(const WebString& s) const;
78 WEBKIT_EXPORT size_t length() const;
79 WEBKIT_EXPORT const WebUChar* data() const;
81 bool isEmpty() const { return !length(); }
82 bool isNull() const { return !m_private; }
84 WEBKIT_EXPORT WebCString utf8() const;
86 WEBKIT_EXPORT static WebString fromUTF8(const char* data, size_t length);
87 WEBKIT_EXPORT static WebString fromUTF8(const char* data);
89 template <int N> WebString(const char (&data)[N])
92 assign(fromUTF8(data, N - 1));
95 template <int N> WebString& operator=(const char (&data)[N])
97 assign(fromUTF8(data, N - 1));
101 #if WEBKIT_IMPLEMENTATION
102 WebString(const WTF::String&);
103 WebString& operator=(const WTF::String&);
104 operator WTF::String() const;
106 WebString(const WTF::AtomicString&);
107 WebString& operator=(const WTF::AtomicString&);
108 operator WTF::AtomicString() const;
111 WebString(const string16& s) : m_private(0)
113 assign(s.data(), s.length());
116 WebString& operator=(const string16& s)
118 assign(s.data(), s.length());
122 operator string16() const
124 size_t len = length();
125 return len ? string16(data(), len) : string16();
128 WebString(const NullableString16& s) : m_private(0)
133 assign(s.string().data(), s.string().length());
136 WebString& operator=(const NullableString16& s)
141 assign(s.string().data(), s.string().length());
145 operator NullableString16() const
148 return NullableString16(string16(), true);
149 size_t len = length();
150 return NullableString16(len ? string16(data(), len) : string16(), false);
153 template <class UTF8String>
154 static WebString fromUTF8(const UTF8String& s)
156 return fromUTF8(s.data(), s.length());
161 void assign(WebStringPrivate*);
162 WebStringPrivate* m_private;
165 inline bool operator==(const WebString& a, const WebString& b)
170 inline bool operator!=(const WebString& a, const WebString& b)
175 } // namespace WebKit