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 #ifndef WebString_h 32 #define WebString_h 33 34 #include "WebCommon.h" 35 36 #if WEBKIT_IMPLEMENTATION 37 #include <wtf/Forward.h> 38 #else 39 #include <base/nullable_string16.h> 40 #include <base/string16.h> 41 #endif 42 43 namespace WebKit { 44 45 class WebCString; 46 class WebStringPrivate; 47 48 // A UTF-16 string container. It is inexpensive to copy a WebString 49 // object. 50 // 51 // WARNING: It is not safe to pass a WebString across threads!!! 52 // 53 class WebString { 54 public: ~WebString()55 ~WebString() { reset(); } 56 WebString()57 WebString() : m_private(0) { } 58 WebString(const WebUChar * data,size_t len)59 WebString(const WebUChar* data, size_t len) : m_private(0) 60 { 61 assign(data, len); 62 } 63 WebString(const WebString & s)64 WebString(const WebString& s) : m_private(0) { assign(s); } 65 66 WebString& operator=(const WebString& s) 67 { 68 assign(s); 69 return *this; 70 } 71 72 WEBKIT_API void reset(); 73 WEBKIT_API void assign(const WebString&); 74 WEBKIT_API void assign(const WebUChar* data, size_t len); 75 76 WEBKIT_API bool equals(const WebString& s) const; 77 78 WEBKIT_API size_t length() const; 79 WEBKIT_API const WebUChar* data() const; 80 isEmpty()81 bool isEmpty() const { return !length(); } isNull()82 bool isNull() const { return !m_private; } 83 84 WEBKIT_API WebCString utf8() const; 85 86 WEBKIT_API static WebString fromUTF8(const char* data, size_t length); 87 WEBKIT_API static WebString fromUTF8(const char* data); 88 WebString(const char (& data)[N])89 template <int N> WebString(const char (&data)[N]) 90 : m_private(0) 91 { 92 assign(fromUTF8(data, N - 1)); 93 } 94 95 template <int N> WebString& operator=(const char (&data)[N]) 96 { 97 assign(fromUTF8(data, N - 1)); 98 return *this; 99 } 100 101 #if WEBKIT_IMPLEMENTATION 102 WebString(const WTF::String&); 103 WebString& operator=(const WTF::String&); 104 operator WTF::String() const; 105 106 WebString(const WTF::AtomicString&); 107 WebString& operator=(const WTF::AtomicString&); 108 operator WTF::AtomicString() const; 109 #else 110 WebString(const string16 & s)111 WebString(const string16& s) : m_private(0) 112 { 113 assign(s.data(), s.length()); 114 } 115 116 WebString& operator=(const string16& s) 117 { 118 assign(s.data(), s.length()); 119 return *this; 120 } 121 string16()122 operator string16() const 123 { 124 size_t len = length(); 125 return len ? string16(data(), len) : string16(); 126 } 127 WebString(const NullableString16 & s)128 WebString(const NullableString16& s) : m_private(0) 129 { 130 if (s.is_null()) 131 reset(); 132 else 133 assign(s.string().data(), s.string().length()); 134 } 135 136 WebString& operator=(const NullableString16& s) 137 { 138 if (s.is_null()) 139 reset(); 140 else 141 assign(s.string().data(), s.string().length()); 142 return *this; 143 } 144 NullableString16()145 operator NullableString16() const 146 { 147 if (!m_private) 148 return NullableString16(string16(), true); 149 size_t len = length(); 150 return NullableString16(len ? string16(data(), len) : string16(), false); 151 } 152 153 template <class UTF8String> fromUTF8(const UTF8String & s)154 static WebString fromUTF8(const UTF8String& s) 155 { 156 return fromUTF8(s.data(), s.length()); 157 } 158 #endif 159 160 private: 161 void assign(WebStringPrivate*); 162 WebStringPrivate* m_private; 163 }; 164 165 inline bool operator==(const WebString& a, const WebString& b) 166 { 167 return a.equals(b); 168 } 169 170 inline bool operator!=(const WebString& a, const WebString& b) 171 { 172 return !(a == b); 173 } 174 175 } // namespace WebKit 176 177 #endif 178