Home | History | Annotate | Download | only in src
      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 "TextEncoding.h"
     35 #include <wtf/text/CString.h>
     36 
     37 #include "WebString.h"
     38 
     39 namespace WebKit {
     40 
     41 class WebCStringPrivate : public WTF::CStringBuffer {
     42 };
     43 
     44 int WebCString::compare(const WebCString& other) const
     45 {
     46     // A null string is always less than a non null one.
     47     if (isNull() != other.isNull())
     48         return isNull() ? -1 : 1;
     49 
     50     if (isNull())
     51         return 0; // Both WebStrings are null.
     52 
     53     return strcmp(m_private->data(), other.m_private->data());
     54 }
     55 
     56 void WebCString::reset()
     57 {
     58     if (m_private) {
     59         m_private->deref();
     60         m_private = 0;
     61     }
     62 }
     63 
     64 void WebCString::assign(const WebCString& other)
     65 {
     66     assign(const_cast<WebCStringPrivate*>(other.m_private));
     67 }
     68 
     69 void WebCString::assign(const char* data, size_t length)
     70 {
     71     char* newData;
     72     RefPtr<WTF::CStringBuffer> buffer =
     73         WTF::CString::newUninitialized(length, newData).buffer();
     74     memcpy(newData, data, length);
     75     assign(static_cast<WebCStringPrivate*>(buffer.get()));
     76 }
     77 
     78 size_t WebCString::length() const
     79 {
     80     if (!m_private)
     81         return 0;
     82     // NOTE: The buffer's length includes the null byte.
     83     return const_cast<WebCStringPrivate*>(m_private)->length() - 1;
     84 }
     85 
     86 const char* WebCString::data() const
     87 {
     88     if (!m_private)
     89         return 0;
     90     return const_cast<WebCStringPrivate*>(m_private)->data();
     91 }
     92 
     93 WebString WebCString::utf16() const
     94 {
     95     return WebCore::UTF8Encoding().decode(data(), length());
     96 }
     97 
     98 WebCString WebCString::fromUTF16(const WebUChar* data, size_t length)
     99 {
    100     return WebCore::UTF8Encoding().encode(
    101         data, length, WebCore::QuestionMarksForUnencodables);
    102 }
    103 
    104 WebCString WebCString::fromUTF16(const WebUChar* data)
    105 {
    106     size_t len = 0;
    107     while (data[len] != WebUChar(0))
    108         len++;
    109     return fromUTF16(data, len);
    110 }
    111 
    112 WebCString::WebCString(const WTF::CString& s)
    113     : m_private(static_cast<WebCStringPrivate*>(s.buffer()))
    114 {
    115     if (m_private)
    116         m_private->ref();
    117 }
    118 
    119 WebCString& WebCString::operator=(const WTF::CString& s)
    120 {
    121     assign(static_cast<WebCStringPrivate*>(s.buffer()));
    122     return *this;
    123 }
    124 
    125 WebCString::operator WTF::CString() const
    126 {
    127     return m_private;
    128 }
    129 
    130 void WebCString::assign(WebCStringPrivate* p)
    131 {
    132     // Take care to handle the case where m_private == p
    133     if (p)
    134         p->ref();
    135     if (m_private)
    136         m_private->deref();
    137     m_private = p;
    138 }
    139 
    140 } // namespace WebKit
    141