• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "WebCString.h"
33 
34 #include "CString.h"
35 #include "TextEncoding.h"
36 
37 #include "WebString.h"
38 
39 namespace WebKit {
40 
41 class WebCStringPrivate : public WebCore::CStringBuffer {
42 };
43 
reset()44 void WebCString::reset()
45 {
46     if (m_private) {
47         m_private->deref();
48         m_private = 0;
49     }
50 }
51 
assign(const WebCString & other)52 void WebCString::assign(const WebCString& other)
53 {
54     assign(const_cast<WebCStringPrivate*>(other.m_private));
55 }
56 
assign(const char * data,size_t length)57 void WebCString::assign(const char* data, size_t length)
58 {
59     char* newData;
60     RefPtr<WebCore::CStringBuffer> buffer =
61         WebCore::CString::newUninitialized(length, newData).buffer();
62     memcpy(newData, data, length);
63     assign(static_cast<WebCStringPrivate*>(buffer.get()));
64 }
65 
length() const66 size_t WebCString::length() const
67 {
68     if (!m_private)
69         return 0;
70     // NOTE: The buffer's length includes the null byte.
71     return const_cast<WebCStringPrivate*>(m_private)->length() - 1;
72 }
73 
data() const74 const char* WebCString::data() const
75 {
76     if (!m_private)
77         return 0;
78     return const_cast<WebCStringPrivate*>(m_private)->data();
79 }
80 
utf16() const81 WebString WebCString::utf16() const
82 {
83     return WebCore::UTF8Encoding().decode(data(), length());
84 }
85 
fromUTF16(const WebUChar * data,size_t length)86 WebCString WebCString::fromUTF16(const WebUChar* data, size_t length)
87 {
88     return WebCore::UTF8Encoding().encode(
89         data, length, WebCore::QuestionMarksForUnencodables);
90 }
91 
fromUTF16(const WebUChar * data)92 WebCString WebCString::fromUTF16(const WebUChar* data)
93 {
94     size_t len = 0;
95     while (data[len] != WebUChar(0))
96         len++;
97     return fromUTF16(data, len);
98 }
99 
WebCString(const WebCore::CString & s)100 WebCString::WebCString(const WebCore::CString& s)
101     : m_private(static_cast<WebCStringPrivate*>(s.buffer()))
102 {
103     if (m_private)
104         m_private->ref();
105 }
106 
operator =(const WebCore::CString & s)107 WebCString& WebCString::operator=(const WebCore::CString& s)
108 {
109     assign(static_cast<WebCStringPrivate*>(s.buffer()));
110     return *this;
111 }
112 
operator WebCore::CString() const113 WebCString::operator WebCore::CString() const
114 {
115     return m_private;
116 }
117 
assign(WebCStringPrivate * p)118 void WebCString::assign(WebCStringPrivate* p)
119 {
120     // Take care to handle the case where m_private == p
121     if (p)
122         p->ref();
123     if (m_private)
124         m_private->deref();
125     m_private = p;
126 }
127 
128 } // namespace WebKit
129