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 "WebDOMString.h" 23 24#include "WebDOMCString.h" 25#include <wtf/text/AtomicString.h> 26#include <wtf/text/CString.h> 27#include <wtf/text/WTFString.h> 28 29class WebDOMStringPrivate : public WTF::StringImpl { 30}; 31 32void WebDOMString::reset() 33{ 34 if (m_private) { 35 m_private->deref(); 36 m_private = 0; 37 } 38} 39 40void WebDOMString::assign(const WebDOMString& other) 41{ 42 assign(const_cast<WebDOMStringPrivate*>(other.m_private)); 43} 44 45void WebDOMString::assign(const WebUChar* data, size_t length) 46{ 47 assign(static_cast<WebDOMStringPrivate*>( 48 WTF::StringImpl::create(data, length).get())); 49} 50 51size_t WebDOMString::length() const 52{ 53 return m_private ? const_cast<WebDOMStringPrivate*>(m_private)->length() : 0; 54} 55 56const WebUChar* WebDOMString::data() const 57{ 58 return m_private ? const_cast<WebDOMStringPrivate*>(m_private)->characters() : 0; 59} 60 61WebDOMCString WebDOMString::utf8() const 62{ 63 return WTF::String(m_private).utf8(); 64} 65 66WebDOMString WebDOMString::fromUTF8(const char* data, size_t length) 67{ 68 return WTF::String::fromUTF8(data, length); 69} 70 71WebDOMString WebDOMString::fromUTF8(const char* data) 72{ 73 return WTF::String::fromUTF8(data); 74} 75 76WebDOMString::WebDOMString(const WTF::String& s) 77 : m_private(static_cast<WebDOMStringPrivate*>(s.impl())) 78{ 79 if (m_private) 80 m_private->ref(); 81} 82 83WebDOMString& WebDOMString::operator=(const WTF::String& s) 84{ 85 assign(static_cast<WebDOMStringPrivate*>(s.impl())); 86 return *this; 87} 88 89WebDOMString::operator WTF::String() const 90{ 91 return m_private; 92} 93 94WebDOMString::WebDOMString(const WTF::AtomicString& s) 95 : m_private(0) 96{ 97 assign(s.string()); 98} 99 100WebDOMString& WebDOMString::operator=(const WTF::AtomicString& s) 101{ 102 assign(s.string()); 103 return *this; 104} 105 106WebDOMString::operator WTF::AtomicString() const 107{ 108 return WTF::AtomicString(static_cast<WTF::StringImpl *>(m_private)); 109} 110 111bool WebDOMString::equals(const char* string) const 112{ 113 return WTF::equal(m_private, string); 114} 115 116void WebDOMString::assign(WebDOMStringPrivate* p) 117{ 118 // Take care to handle the case where m_private == p 119 if (p) 120 p->ref(); 121 if (m_private) 122 m_private->deref(); 123 m_private = p; 124} 125