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>
52 // A UTF-16 string container. It is inexpensive to copy a WebString
55 // WARNING: It is not safe to pass a WebString across threads!!!
59 ~WebString() { reset(); }
63 WebString(const WebUChar
* data
, size_t len
)
68 WebString(const WebString
& s
) { assign(s
); }
70 WebString
& operator=(const WebString
& s
)
76 BLINK_COMMON_EXPORT
void reset();
77 BLINK_COMMON_EXPORT
void assign(const WebString
&);
78 BLINK_COMMON_EXPORT
void assign(const WebUChar
* data
, size_t len
);
80 BLINK_COMMON_EXPORT
bool equals(const WebString
&) const;
82 BLINK_COMMON_EXPORT
size_t length() const;
84 // Caller must check bounds.
85 BLINK_COMMON_EXPORT WebUChar
at(unsigned) const;
87 bool isEmpty() const { return !length(); }
88 bool isNull() const { return m_private
.isNull(); }
90 BLINK_COMMON_EXPORT
std::string
utf8() const;
92 BLINK_COMMON_EXPORT
static WebString
fromUTF8(const char* data
, size_t length
);
93 BLINK_COMMON_EXPORT
static WebString
fromUTF8(const char* data
);
95 static WebString
fromUTF8(const std::string
& s
)
97 return fromUTF8(s
.data(), s
.length());
100 BLINK_COMMON_EXPORT
std::string
latin1() const;
102 BLINK_COMMON_EXPORT
static WebString
fromLatin1(const WebLChar
* data
, size_t length
);
104 static WebString
fromLatin1(const std::string
& s
)
106 return fromLatin1(reinterpret_cast<const WebLChar
*>(s
.data()), s
.length());
109 template <int N
> WebString(const char (&data
)[N
])
111 assign(fromUTF8(data
, N
- 1));
114 template <int N
> WebString
& operator=(const char (&data
)[N
])
116 assign(fromUTF8(data
, N
- 1));
121 BLINK_COMMON_EXPORT
WebString(const WTF::String
&);
122 BLINK_COMMON_EXPORT WebString
& operator=(const WTF::String
&);
123 BLINK_COMMON_EXPORT
operator WTF::String() const;
125 BLINK_COMMON_EXPORT
WebString(const WTF::AtomicString
&);
126 BLINK_COMMON_EXPORT WebString
& operator=(const WTF::AtomicString
&);
127 BLINK_COMMON_EXPORT
operator WTF::AtomicString() const;
129 WebString(const base::string16
& s
)
131 assign(s
.data(), s
.length());
134 WebString
& operator=(const base::string16
& s
)
136 assign(s
.data(), s
.length());
140 operator base::string16() const
142 return base::Latin1OrUTF16ToUTF16(length(), data8(), data16());
145 WebString(const base::NullableString16
& s
)
150 assign(s
.string().data(), s
.string().length());
153 WebString
& operator=(const base::NullableString16
& s
)
158 assign(s
.string().data(), s
.string().length());
162 operator base::NullableString16() const
164 return base::NullableString16(operator base::string16(), m_private
.isNull());
169 BLINK_COMMON_EXPORT
bool is8Bit() const;
170 BLINK_COMMON_EXPORT
const WebLChar
* data8() const;
171 BLINK_COMMON_EXPORT
const WebUChar
* data16() const;
173 BLINK_COMMON_EXPORT
void assign(WTF::StringImpl
*);
175 WebPrivatePtr
<WTF::StringImpl
> m_private
;
178 inline bool operator==(const WebString
& a
, const WebString
& b
)
183 inline bool operator!=(const WebString
& a
, const WebString
& b
)