1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #include "config.h"
32 #include "public/platform/WebString.h"
33
34 #include "public/platform/WebCString.h"
35 #include "wtf/text/AtomicString.h"
36 #include "wtf/text/CString.h"
37 #include "wtf/text/StringUTF8Adaptor.h"
38 #include "wtf/text/WTFString.h"
39
40 namespace blink {
41
reset()42 void WebString::reset()
43 {
44 m_private.reset();
45 }
46
assign(const WebString & other)47 void WebString::assign(const WebString& other)
48 {
49 assign(other.m_private.get());
50 }
51
assign(const WebUChar * data,size_t length)52 void WebString::assign(const WebUChar* data, size_t length)
53 {
54 assign(StringImpl::create8BitIfPossible(data, length).get());
55 }
56
length() const57 size_t WebString::length() const
58 {
59 return m_private.isNull() ? 0 : m_private->length();
60 }
61
at(unsigned i) const62 WebUChar WebString::at(unsigned i) const
63 {
64 ASSERT(!m_private.isNull());
65 return (*m_private.get())[i];
66 }
67
is8Bit() const68 bool WebString::is8Bit() const
69 {
70 return m_private->is8Bit();
71 }
72
data8() const73 const WebLChar* WebString::data8() const
74 {
75 return !m_private.isNull() && is8Bit() ? m_private->characters8() : 0;
76 }
77
data16() const78 const WebUChar* WebString::data16() const
79 {
80 return !m_private.isNull() && !is8Bit() ? m_private->characters16() : 0;
81 }
82
utf8() const83 std::string WebString::utf8() const
84 {
85 StringUTF8Adaptor utf8(m_private.get());
86 return std::string(utf8.data(), utf8.length());
87 }
88
fromUTF8(const char * data,size_t length)89 WebString WebString::fromUTF8(const char* data, size_t length)
90 {
91 return String::fromUTF8(data, length);
92 }
93
fromUTF8(const char * data)94 WebString WebString::fromUTF8(const char* data)
95 {
96 return String::fromUTF8(data);
97 }
98
latin1() const99 std::string WebString::latin1() const
100 {
101 String string(m_private.get());
102
103 if (string.isEmpty())
104 return std::string();
105
106 if (string.is8Bit())
107 return std::string(reinterpret_cast<const char*>(string.characters8()), string.length());
108
109 WebCString latin1 = string.latin1();
110 return std::string(latin1.data(), latin1.length());
111 }
112
fromLatin1(const WebLChar * data,size_t length)113 WebString WebString::fromLatin1(const WebLChar* data, size_t length)
114 {
115 return String(data, length);
116 }
117
equals(const WebString & s) const118 bool WebString::equals(const WebString& s) const
119 {
120 return equal(m_private.get(), s.m_private.get());
121 }
122
WebString(const WTF::String & s)123 WebString::WebString(const WTF::String& s)
124 : m_private(s.impl())
125 {
126 }
127
operator =(const WTF::String & s)128 WebString& WebString::operator=(const WTF::String& s)
129 {
130 assign(s.impl());
131 return *this;
132 }
133
operator WTF::String() const134 WebString::operator WTF::String() const
135 {
136 return m_private.get();
137 }
138
WebString(const WTF::AtomicString & s)139 WebString::WebString(const WTF::AtomicString& s)
140 {
141 assign(s.string());
142 }
143
operator =(const WTF::AtomicString & s)144 WebString& WebString::operator=(const WTF::AtomicString& s)
145 {
146 assign(s.string());
147 return *this;
148 }
149
operator WTF::AtomicString() const150 WebString::operator WTF::AtomicString() const
151 {
152 return WTF::AtomicString(m_private.get());
153 }
154
assign(WTF::StringImpl * p)155 void WebString::assign(WTF::StringImpl* p)
156 {
157 m_private = p;
158 }
159
160 } // namespace blink
161