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