Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: third_party/WebKit/public/platform/WebString.h

Issue 2571863003: Add explicit conversions methods for WebString <-> string16 (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/public/platform/DEPS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 15 matching lines...) Expand all
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef WebString_h 31 #ifndef WebString_h
32 #define WebString_h 32 #define WebString_h
33 33
34 #include "WebCommon.h" 34 #include "WebCommon.h"
35 #include "WebPrivatePtr.h" 35 #include "WebPrivatePtr.h"
36 #include "base/strings/latin1_string_conversions.h"
37 #include "base/strings/nullable_string16.h"
38 #include "base/strings/string16.h"
36 #include <string> 39 #include <string>
37 40
38 #if INSIDE_BLINK 41 #if INSIDE_BLINK
39 #include "wtf/Forward.h" 42 #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 43 #endif
45 44
46 namespace WTF { 45 namespace WTF {
47 class StringImpl; 46 class StringImpl;
48 } 47 }
49 48
50 namespace blink { 49 namespace blink {
51 50
52 // A UTF-16 string container. It is inexpensive to copy a WebString 51 // Use either one of static methods to convert ASCII, Latin1, UTF-8 or
53 // object. 52 // UTF-16 string into WebString:
54 // 53 //
54 // * WebString::fromASCII(const std::string& ascii)
55 // * WebString::fromLatin1(const std::string& latin1)
56 // * WebString::fromUTF8(const std::string& utf8)
57 // * WebString::fromUTF16(const base::string16& utf16)
58 // * WebString::fromUTF16(const base::NullableString16& utf16)
59 //
60 // Similarly, use either of following methods to convert WebString to
61 // ASCII, Latin1, UTF-8 or UTF-16:
62 //
63 // * webstring.ascii()
64 // * webstring.latin1()
65 // * webstring.utf8()
66 // * webstring.utf16()
67 // * WebString::toNullableString16(webstring)
68 //
69 // Some types like GURL and base::FilePath can directly take either utf-8 or
70 // utf-16 strings. Use following methods to convert WebString to/from GURL or
71 // FilePath rather than going through intermediate string types:
72 //
73 // * GURL WebStringToGURL(const WebString&)
74 // * base::FilePath WebStringToFilePath(const WebString&)
75 //
76 // It is inexpensive to copy a WebString object.
55 // WARNING: It is not safe to pass a WebString across threads!!! 77 // WARNING: It is not safe to pass a WebString across threads!!!
56 // 78 //
57 class WebString { 79 class WebString {
58 public: 80 public:
59 ~WebString() { reset(); } 81 ~WebString() { reset(); }
60 82
61 WebString() {} 83 WebString() {}
62 84
63 WebString(const WebUChar* data, size_t len) { assign(data, len); } 85 WebString(const WebUChar* data, size_t len) { assign(data, len); }
64 86
(...skipping 19 matching lines...) Expand all
84 BLINK_COMMON_EXPORT std::string utf8() const; 106 BLINK_COMMON_EXPORT std::string utf8() const;
85 107
86 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data, 108 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data,
87 size_t length); 109 size_t length);
88 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data); 110 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data);
89 111
90 static WebString fromUTF8(const std::string& s) { 112 static WebString fromUTF8(const std::string& s) {
91 return fromUTF8(s.data(), s.length()); 113 return fromUTF8(s.data(), s.length());
92 } 114 }
93 115
116 base::string16 utf16() const {
117 return base::Latin1OrUTF16ToUTF16(length(), data8(), data16());
118 }
119
120 BLINK_COMMON_EXPORT static WebString fromUTF16(const base::string16&);
121
122 BLINK_COMMON_EXPORT static WebString fromUTF16(const base::NullableString16&);
123
124 static base::NullableString16 toNullableString16(const WebString& s) {
125 return base::NullableString16(s.utf16(), s.isNull());
126 }
127
94 BLINK_COMMON_EXPORT std::string latin1() const; 128 BLINK_COMMON_EXPORT std::string latin1() const;
95 129
96 BLINK_COMMON_EXPORT static WebString fromLatin1(const WebLChar* data, 130 BLINK_COMMON_EXPORT static WebString fromLatin1(const WebLChar* data,
97 size_t length); 131 size_t length);
98 132
99 static WebString fromLatin1(const std::string& s) { 133 static WebString fromLatin1(const std::string& s) {
100 return fromLatin1(reinterpret_cast<const WebLChar*>(s.data()), s.length()); 134 return fromLatin1(reinterpret_cast<const WebLChar*>(s.data()), s.length());
101 } 135 }
102 136
103 // This asserts if the string contains non-ascii characters. 137 // This asserts if the string contains non-ascii characters.
(...skipping 22 matching lines...) Expand all
126 BLINK_COMMON_EXPORT WebString(const WTF::String&); 160 BLINK_COMMON_EXPORT WebString(const WTF::String&);
127 BLINK_COMMON_EXPORT WebString& operator=(const WTF::String&); 161 BLINK_COMMON_EXPORT WebString& operator=(const WTF::String&);
128 BLINK_COMMON_EXPORT operator WTF::String() const; 162 BLINK_COMMON_EXPORT operator WTF::String() const;
129 163
130 BLINK_COMMON_EXPORT operator WTF::StringView() const; 164 BLINK_COMMON_EXPORT operator WTF::StringView() const;
131 165
132 BLINK_COMMON_EXPORT WebString(const WTF::AtomicString&); 166 BLINK_COMMON_EXPORT WebString(const WTF::AtomicString&);
133 BLINK_COMMON_EXPORT WebString& operator=(const WTF::AtomicString&); 167 BLINK_COMMON_EXPORT WebString& operator=(const WTF::AtomicString&);
134 BLINK_COMMON_EXPORT operator WTF::AtomicString() const; 168 BLINK_COMMON_EXPORT operator WTF::AtomicString() const;
135 #else 169 #else
170 // WARNING: implicit conversions to/from string16 are being deprecated,
171 // use fromUTF16() or utf16() instead in new changes.
136 WebString(const base::string16& s) { assign(s.data(), s.length()); } 172 WebString(const base::string16& s) { assign(s.data(), s.length()); }
137 173
138 WebString& operator=(const base::string16& s) { 174 WebString& operator=(const base::string16& s) {
139 assign(s.data(), s.length()); 175 assign(s.data(), s.length());
140 return *this; 176 return *this;
141 } 177 }
142 178
143 operator base::string16() const { 179 operator base::string16() const {
144 return base::Latin1OrUTF16ToUTF16(length(), data8(), data16()); 180 return base::Latin1OrUTF16ToUTF16(length(), data8(), data16());
145 } 181 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 return a.equals(b); 231 return a.equals(b);
196 } 232 }
197 233
198 inline bool operator!=(const WebString& a, const WebString& b) { 234 inline bool operator!=(const WebString& a, const WebString& b) {
199 return !(a == b); 235 return !(a == b);
200 } 236 }
201 237
202 } // namespace blink 238 } // namespace blink
203 239
204 #endif 240 #endif
OLDNEW
« no previous file with comments | « third_party/WebKit/public/platform/DEPS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698