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 "WebString.h"
33
34 #include "PlatformString.h"
35 #include <wtf/text/CString.h>
36 #include <wtf/text/AtomicString.h>
37
38 #include "WebCString.h"
39
40 namespace WebKit {
41
42 class WebStringPrivate : public WTF::StringImpl {
43 };
44
reset()45 void WebString::reset()
46 {
47 if (m_private) {
48 m_private->deref();
49 m_private = 0;
50 }
51 }
52
assign(const WebString & other)53 void WebString::assign(const WebString& other)
54 {
55 assign(const_cast<WebStringPrivate*>(other.m_private));
56 }
57
assign(const WebUChar * data,size_t length)58 void WebString::assign(const WebUChar* data, size_t length)
59 {
60 assign(static_cast<WebStringPrivate*>(
61 WTF::StringImpl::create(data, length).get()));
62 }
63
length() const64 size_t WebString::length() const
65 {
66 return m_private ? const_cast<WebStringPrivate*>(m_private)->length() : 0;
67 }
68
data() const69 const WebUChar* WebString::data() const
70 {
71 return m_private ? const_cast<WebStringPrivate*>(m_private)->characters() : 0;
72 }
73
utf8() const74 WebCString WebString::utf8() const
75 {
76 return WTF::String(m_private).utf8();
77 }
78
fromUTF8(const char * data,size_t length)79 WebString WebString::fromUTF8(const char* data, size_t length)
80 {
81 return WTF::String::fromUTF8(data, length);
82 }
83
fromUTF8(const char * data)84 WebString WebString::fromUTF8(const char* data)
85 {
86 return WTF::String::fromUTF8(data);
87 }
88
equals(const WebString & s) const89 bool WebString::equals(const WebString& s) const
90 {
91 return equal(m_private, s.m_private);
92 }
93
WebString(const WTF::String & s)94 WebString::WebString(const WTF::String& s)
95 : m_private(static_cast<WebStringPrivate*>(s.impl()))
96 {
97 if (m_private)
98 m_private->ref();
99 }
100
operator =(const WTF::String & s)101 WebString& WebString::operator=(const WTF::String& s)
102 {
103 assign(static_cast<WebStringPrivate*>(s.impl()));
104 return *this;
105 }
106
operator WTF::String() const107 WebString::operator WTF::String() const
108 {
109 return m_private;
110 }
111
WebString(const WTF::AtomicString & s)112 WebString::WebString(const WTF::AtomicString& s)
113 : m_private(0)
114 {
115 assign(s.string());
116 }
117
operator =(const WTF::AtomicString & s)118 WebString& WebString::operator=(const WTF::AtomicString& s)
119 {
120 assign(s.string());
121 return *this;
122 }
123
operator WTF::AtomicString() const124 WebString::operator WTF::AtomicString() const
125 {
126 return WTF::AtomicString(static_cast<WTF::StringImpl *>(m_private));
127 }
128
assign(WebStringPrivate * p)129 void WebString::assign(WebStringPrivate* p)
130 {
131 // Take care to handle the case where m_private == p
132 if (p)
133 p->ref();
134 if (m_private)
135 m_private->deref();
136 m_private = p;
137 }
138
139 } // namespace WebKit
140