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
40namespace WebKit {
41
42void WebString::reset()
43{
44    m_private.reset();
45}
46
47void WebString::assign(const WebString& other)
48{
49    assign(other.m_private.get());
50}
51
52void WebString::assign(const WebUChar* data, size_t length)
53{
54    assign(StringImpl::create8BitIfPossible(data, length).get());
55}
56
57size_t WebString::length() const
58{
59    return m_private.isNull() ? 0 : m_private->length();
60}
61
62WebUChar WebString::at(unsigned i) const
63{
64    ASSERT(!m_private.isNull());
65    return (*m_private.get())[i];
66}
67
68bool WebString::is8Bit() const
69{
70    return m_private->is8Bit();
71}
72
73const WebLChar* WebString::data8() const
74{
75    return !m_private.isNull() && is8Bit() ? m_private->characters8() : 0;
76}
77
78const WebUChar* WebString::data16() const
79{
80    return !m_private.isNull() && !is8Bit() ? m_private->characters16() : 0;
81}
82
83std::string WebString::utf8() const
84{
85    StringUTF8Adaptor utf8(m_private.get());
86    return std::string(utf8.data(), utf8.length());
87}
88
89WebString WebString::fromUTF8(const char* data, size_t length)
90{
91    return WTF::String::fromUTF8(data, length);
92}
93
94WebString WebString::fromUTF8(const char* data)
95{
96    return WTF::String::fromUTF8(data);
97}
98
99bool WebString::equals(const WebString& s) const
100{
101    return equal(m_private.get(), s.m_private.get());
102}
103
104WebString::WebString(const WTF::String& s)
105{
106    m_private = s.impl();
107}
108
109WebString& WebString::operator=(const WTF::String& s)
110{
111    assign(s.impl());
112    return *this;
113}
114
115WebString::operator WTF::String() const
116{
117    return m_private.get();
118}
119
120WebString::WebString(const WTF::AtomicString& s)
121{
122    assign(s.string());
123}
124
125WebString& WebString::operator=(const WTF::AtomicString& s)
126{
127    assign(s.string());
128    return *this;
129}
130
131WebString::operator WTF::AtomicString() const
132{
133    return WTF::AtomicString(m_private.get());
134}
135
136void WebString::assign(WTF::StringImpl* p)
137{
138    m_private = p;
139}
140
141} // namespace WebKit
142