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