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