Home | History | Annotate | Download | only in cpp
      1 /*
      2  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
      3  * Copyright (C) 2009 Google Inc. All rights reserved.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  */
     20 
     21 #include "config.h"
     22 #include "WebDOMCString.h"
     23 
     24 #include "TextEncoding.h"
     25 #include "WebDOMString.h"
     26 #include <wtf/text/CString.h>
     27 
     28 class WebDOMCStringPrivate : public WTF::CStringBuffer {
     29 };
     30 
     31 void WebDOMCString::reset()
     32 {
     33     if (m_private) {
     34         m_private->deref();
     35         m_private = 0;
     36     }
     37 }
     38 
     39 void WebDOMCString::assign(const WebDOMCString& other)
     40 {
     41     assign(const_cast<WebDOMCStringPrivate*>(other.m_private));
     42 }
     43 
     44 void WebDOMCString::assign(const char* data, size_t length)
     45 {
     46     char* newData;
     47     RefPtr<WTF::CStringBuffer> buffer =
     48         WTF::CString::newUninitialized(length, newData).buffer();
     49     memcpy(newData, data, length);
     50     assign(static_cast<WebDOMCStringPrivate*>(buffer.get()));
     51 }
     52 
     53 size_t WebDOMCString::length() const
     54 {
     55     if (!m_private)
     56         return 0;
     57     // NOTE: The buffer's length includes the null byte.
     58     return const_cast<WebDOMCStringPrivate*>(m_private)->length() - 1;
     59 }
     60 
     61 const char* WebDOMCString::data() const
     62 {
     63     if (!m_private)
     64         return 0;
     65     return const_cast<WebDOMCStringPrivate*>(m_private)->data();
     66 }
     67 
     68 WebDOMString WebDOMCString::utf16() const
     69 {
     70     return WebCore::UTF8Encoding().decode(data(), length());
     71 }
     72 
     73 WebDOMCString WebDOMCString::fromUTF16(const WebUChar* data, size_t length)
     74 {
     75     return WebCore::UTF8Encoding().encode(
     76         data, length, WebCore::QuestionMarksForUnencodables);
     77 }
     78 
     79 WebDOMCString WebDOMCString::fromUTF16(const WebUChar* data)
     80 {
     81     size_t len = 0;
     82     while (data[len] != WebUChar(0))
     83         len++;
     84     return fromUTF16(data, len);
     85 }
     86 
     87 WebDOMCString::WebDOMCString(const WTF::CString& s)
     88     : m_private(static_cast<WebDOMCStringPrivate*>(s.buffer()))
     89 {
     90     if (m_private)
     91         m_private->ref();
     92 }
     93 
     94 WebDOMCString& WebDOMCString::operator=(const WTF::CString& s)
     95 {
     96     assign(static_cast<WebDOMCStringPrivate*>(s.buffer()));
     97     return *this;
     98 }
     99 
    100 WebDOMCString::operator WTF::CString() const
    101 {
    102     return m_private;
    103 }
    104 
    105 void WebDOMCString::assign(WebDOMCStringPrivate* p)
    106 {
    107     // Take care to handle the case where m_private == p
    108     if (p)
    109         p->ref();
    110     if (m_private)
    111         m_private->deref();
    112     m_private = p;
    113 }
    114