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"
35 #include "WebPrivatePtr.h"
39 #include "wtf/Forward.h"
41 #include <base/strings/latin1_string_conversions.h>
42 #include <base/strings/nullable_string16.h>
43 #include <base/strings/string16.h>
54 // A UTF-16 string container. It is inexpensive to copy a WebString
57 // WARNING: It is not safe to pass a WebString across threads!!!
61 ~WebString() { reset(); }
65 WebString(const WebUChar* data, size_t len)
70 WebString(const WebString& s) { assign(s); }
72 WebString& operator=(const WebString& s)
78 BLINK_COMMON_EXPORT void reset();
79 BLINK_COMMON_EXPORT void assign(const WebString&);
80 BLINK_COMMON_EXPORT void assign(const WebUChar* data, size_t len);
82 BLINK_COMMON_EXPORT bool equals(const WebString&) const;
84 BLINK_COMMON_EXPORT size_t length() const;
86 // Caller must check bounds.
87 BLINK_COMMON_EXPORT WebUChar at(unsigned) const;
89 bool isEmpty() const { return !length(); }
90 bool isNull() const { return m_private.isNull(); }
92 BLINK_COMMON_EXPORT std::string utf8() const;
94 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data, size_t length);
95 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data);
97 static WebString fromUTF8(const std::string& s)
99 return fromUTF8(s.data(), s.length());
102 BLINK_COMMON_EXPORT std::string latin1() const;
104 BLINK_COMMON_EXPORT static WebString fromLatin1(const WebLChar* data, size_t length);
106 static WebString fromLatin1(const std::string& s)
108 return fromLatin1(reinterpret_cast<const WebLChar*>(s.data()), s.length());
111 template <int N> WebString(const char (&data)[N])
113 assign(fromUTF8(data, N - 1));
116 template <int N> WebString& operator=(const char (&data)[N])
118 assign(fromUTF8(data, N - 1));
123 BLINK_COMMON_EXPORT WebString(const WTF::String&);
124 BLINK_COMMON_EXPORT WebString& operator=(const WTF::String&);
125 BLINK_COMMON_EXPORT operator WTF::String() const;
127 BLINK_COMMON_EXPORT WebString(const WTF::AtomicString&);
128 BLINK_COMMON_EXPORT WebString& operator=(const WTF::AtomicString&);
129 BLINK_COMMON_EXPORT operator WTF::AtomicString() const;
131 WebString(const base::string16& s)
133 assign(s.data(), s.length());
136 WebString& operator=(const base::string16& s)
138 assign(s.data(), s.length());
142 operator base::string16() const
144 return base::Latin1OrUTF16ToUTF16(length(), data8(), data16());
147 WebString(const base::NullableString16& s)
152 assign(s.string().data(), s.string().length());
155 WebString& operator=(const base::NullableString16& s)
160 assign(s.string().data(), s.string().length());
164 operator base::NullableString16() const
166 return base::NullableString16(operator base::string16(), m_private.isNull());
171 BLINK_COMMON_EXPORT bool is8Bit() const;
172 BLINK_COMMON_EXPORT const WebLChar* data8() const;
173 BLINK_COMMON_EXPORT const WebUChar* data16() const;
175 BLINK_COMMON_EXPORT void assign(WTF::StringImpl*);
177 WebPrivatePtr<WTF::StringImpl> m_private;
180 inline bool operator==(const WebString& a, const WebString& b)
185 inline bool operator!=(const WebString& a, const WebString& b)