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.
32 #include "WebCString.h"
34 #include "TextEncoding.h"
35 #include <wtf/text/CString.h>
37 #include "WebString.h"
41 class WebCStringPrivate : public WTF::CStringBuffer {
44 int WebCString::compare(const WebCString& other) const
46 // A null string is always less than a non null one.
47 if (isNull() != other.isNull())
48 return isNull() ? -1 : 1;
51 return 0; // Both WebStrings are null.
53 return strcmp(m_private->data(), other.m_private->data());
56 void WebCString::reset()
64 void WebCString::assign(const WebCString& other)
66 assign(const_cast<WebCStringPrivate*>(other.m_private));
69 void WebCString::assign(const char* data, size_t length)
72 RefPtr<WTF::CStringBuffer> buffer =
73 WTF::CString::newUninitialized(length, newData).buffer();
74 memcpy(newData, data, length);
75 assign(static_cast<WebCStringPrivate*>(buffer.get()));
78 size_t WebCString::length() const
82 // NOTE: The buffer's length includes the null byte.
83 return const_cast<WebCStringPrivate*>(m_private)->length() - 1;
86 const char* WebCString::data() const
90 return const_cast<WebCStringPrivate*>(m_private)->data();
93 WebString WebCString::utf16() const
95 return WebCore::UTF8Encoding().decode(data(), length());
98 WebCString WebCString::fromUTF16(const WebUChar* data, size_t length)
100 return WebCore::UTF8Encoding().encode(
101 data, length, WebCore::QuestionMarksForUnencodables);
104 WebCString WebCString::fromUTF16(const WebUChar* data)
107 while (data[len] != WebUChar(0))
109 return fromUTF16(data, len);
112 WebCString::WebCString(const WTF::CString& s)
113 : m_private(static_cast<WebCStringPrivate*>(s.buffer()))
119 WebCString& WebCString::operator=(const WTF::CString& s)
121 assign(static_cast<WebCStringPrivate*>(s.buffer()));
125 WebCString::operator WTF::CString() const
130 void WebCString::assign(WebCStringPrivate* p)
132 // Take care to handle the case where m_private == p
140 } // namespace WebKit