OLD | NEW |
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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // | 59 // |
60 // Similarly, use either of following methods to convert WebString to | 60 // Similarly, use either of following methods to convert WebString to |
61 // ASCII, Latin1, UTF-8 or UTF-16: | 61 // ASCII, Latin1, UTF-8 or UTF-16: |
62 // | 62 // |
63 // * webstring.ascii() | 63 // * webstring.ascii() |
64 // * webstring.latin1() | 64 // * webstring.latin1() |
65 // * webstring.utf8() | 65 // * webstring.utf8() |
66 // * webstring.utf16() | 66 // * webstring.utf16() |
67 // * WebString::toNullableString16(webstring) | 67 // * WebString::toNullableString16(webstring) |
68 // | 68 // |
| 69 // Note that if you need to convert the UTF8 string converted from WebString |
| 70 // back to WebString with fromUTF8() you may want to specify Strict |
| 71 // UTF8ConversionMode when you call utf8(), as fromUTF8 rejects strings |
| 72 // with invalid UTF8 characters. |
| 73 // |
69 // Some types like GURL and base::FilePath can directly take either utf-8 or | 74 // 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 | 75 // utf-16 strings. Use following methods to convert WebString to/from GURL or |
71 // FilePath rather than going through intermediate string types: | 76 // FilePath rather than going through intermediate string types: |
72 // | 77 // |
73 // * GURL WebStringToGURL(const WebString&) | 78 // * GURL WebStringToGURL(const WebString&) |
74 // * base::FilePath WebStringToFilePath(const WebString&) | 79 // * base::FilePath WebStringToFilePath(const WebString&) |
75 // * WebString FilePathToWebString(const base::FilePath&); | 80 // * WebString FilePathToWebString(const base::FilePath&); |
76 // | 81 // |
77 // It is inexpensive to copy a WebString object. | 82 // It is inexpensive to copy a WebString object. |
78 // WARNING: It is not safe to pass a WebString across threads!!! | 83 // WARNING: It is not safe to pass a WebString across threads!!! |
79 // | 84 // |
80 class WebString { | 85 class WebString { |
81 public: | 86 public: |
| 87 enum class UTF8ConversionMode { |
| 88 // Ignores errors for invalid characters. |
| 89 kLenient, |
| 90 // Errors out on invalid characters, returns null string. |
| 91 kStrict, |
| 92 // Replace invalid characters with 0xFFFD. |
| 93 // (This is the same conversion mode as base::UTF16ToUTF8) |
| 94 kStrictReplacingErrorsWithFFFD, |
| 95 }; |
| 96 |
82 ~WebString() { reset(); } | 97 ~WebString() { reset(); } |
83 | 98 |
84 WebString() {} | 99 WebString() {} |
85 | 100 |
86 WebString(const WebUChar* data, size_t len) { assign(data, len); } | 101 WebString(const WebUChar* data, size_t len) { assign(data, len); } |
87 | 102 |
88 WebString(const WebString& s) { assign(s); } | 103 WebString(const WebString& s) { assign(s); } |
89 | 104 |
90 WebString& operator=(const WebString& s) { | 105 WebString& operator=(const WebString& s) { |
91 assign(s); | 106 assign(s); |
92 return *this; | 107 return *this; |
93 } | 108 } |
94 | 109 |
95 BLINK_COMMON_EXPORT void reset(); | 110 BLINK_COMMON_EXPORT void reset(); |
96 BLINK_COMMON_EXPORT void assign(const WebString&); | 111 BLINK_COMMON_EXPORT void assign(const WebString&); |
97 BLINK_COMMON_EXPORT void assign(const WebUChar* data, size_t len); | 112 BLINK_COMMON_EXPORT void assign(const WebUChar* data, size_t len); |
98 | 113 |
99 BLINK_COMMON_EXPORT bool equals(const WebString&) const; | 114 BLINK_COMMON_EXPORT bool equals(const WebString&) const; |
100 BLINK_COMMON_EXPORT bool equals(const char* characters) const; | 115 BLINK_COMMON_EXPORT bool equals(const char* characters) const; |
101 | 116 |
102 BLINK_COMMON_EXPORT size_t length() const; | 117 BLINK_COMMON_EXPORT size_t length() const; |
103 | 118 |
104 bool isEmpty() const { return !length(); } | 119 bool isEmpty() const { return !length(); } |
105 bool isNull() const { return m_private.isNull(); } | 120 bool isNull() const { return m_private.isNull(); } |
106 | 121 |
107 BLINK_COMMON_EXPORT std::string utf8() const; | 122 BLINK_COMMON_EXPORT std::string utf8( |
| 123 UTF8ConversionMode = UTF8ConversionMode::kLenient) const; |
108 | 124 |
109 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data, | 125 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data, |
110 size_t length); | 126 size_t length); |
111 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data); | 127 BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data); |
112 | 128 |
113 static WebString fromUTF8(const std::string& s) { | 129 static WebString fromUTF8(const std::string& s) { |
114 return fromUTF8(s.data(), s.length()); | 130 return fromUTF8(s.data(), s.length()); |
115 } | 131 } |
116 | 132 |
117 base::string16 utf16() const { | 133 base::string16 utf16() const { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 return a.equals(b); | 248 return a.equals(b); |
233 } | 249 } |
234 | 250 |
235 inline bool operator!=(const WebString& a, const WebString& b) { | 251 inline bool operator!=(const WebString& a, const WebString& b) { |
236 return !(a == b); | 252 return !(a == b); |
237 } | 253 } |
238 | 254 |
239 } // namespace blink | 255 } // namespace blink |
240 | 256 |
241 #endif | 257 #endif |
OLD | NEW |