| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_STRINGS_STRING16_H_ | 5 #ifndef BASE_STRINGS_STRING16_H_ |
| 6 #define BASE_STRINGS_STRING16_H_ | 6 #define BASE_STRINGS_STRING16_H_ |
| 7 | 7 |
| 8 // WHAT: | 8 // WHAT: |
| 9 // A version of std::basic_string that provides 2-byte characters even when | 9 // A version of std::basic_string that provides 2-byte characters even when |
| 10 // wchar_t is not implemented as a 2-byte type. You can access this class as | 10 // wchar_t is not implemented as a 2-byte type. You can access this class as |
| 11 // string16. We also define char16, which string16 is based upon. | 11 // string16. We also define char16, which string16 is based upon. |
| 12 // | 12 // |
| 13 // WHY: | 13 // WHY: |
| 14 // On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2 | 14 // On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2 |
| 15 // data. Plenty of existing code operates on strings encoded as UTF-16. | 15 // data. Plenty of existing code operates on strings encoded as UTF-16. |
| 16 // | 16 // |
| 17 // On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make | 17 // On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make |
| 18 // it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails | 18 // it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails |
| 19 // at run time, because it calls some functions (like wcslen) that come from | 19 // at run time, because it calls some functions (like wcslen) that come from |
| 20 // the system's native C library -- which was built with a 4-byte wchar_t! | 20 // the system's native C library -- which was built with a 4-byte wchar_t! |
| 21 // It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's | 21 // It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's |
| 22 // entirely improper on those systems where the encoding of wchar_t is defined | 22 // entirely improper on those systems where the encoding of wchar_t is defined |
| 23 // as UTF-32. | 23 // as UTF-32. |
| 24 // | 24 // |
| 25 // Here, we define string16, which is similar to std::wstring but replaces all | 25 // Here, we define string16, which is similar to std::wstring but replaces all |
| 26 // libc functions with custom, 2-byte-char compatible routines. It is capable | 26 // libc functions with custom, 2-byte-char compatible routines. It is capable |
| 27 // of carrying UTF-16-encoded data. | 27 // of carrying UTF-16-encoded data. |
| 28 | 28 |
| 29 #include <stdio.h> | 29 #include <stdio.h> |
| 30 |
| 31 #include <functional> |
| 30 #include <string> | 32 #include <string> |
| 31 | 33 |
| 32 #include "base/base_export.h" | 34 #include "base/base_export.h" |
| 33 #include "base/basictypes.h" | 35 #include "base/basictypes.h" |
| 34 | 36 |
| 35 #if defined(WCHAR_T_IS_UTF16) | 37 #if defined(WCHAR_T_IS_UTF16) |
| 36 | 38 |
| 37 namespace base { | 39 namespace base { |
| 38 | 40 |
| 39 typedef wchar_t char16; | 41 typedef wchar_t char16; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // in which the linker does not fully coalesce symbols when dead code | 175 // in which the linker does not fully coalesce symbols when dead code |
| 174 // stripping is enabled. This bug causes the memory errors described above | 176 // stripping is enabled. This bug causes the memory errors described above |
| 175 // to occur even when a std::basic_string<> does not cross shared library | 177 // to occur even when a std::basic_string<> does not cross shared library |
| 176 // boundaries, such as in statically-linked executables. | 178 // boundaries, such as in statically-linked executables. |
| 177 // | 179 // |
| 178 // TODO(mark): File this bug with Apple and update this note with a bug number. | 180 // TODO(mark): File this bug with Apple and update this note with a bug number. |
| 179 | 181 |
| 180 extern template | 182 extern template |
| 181 class BASE_EXPORT std::basic_string<base::char16, base::string16_char_traits>; | 183 class BASE_EXPORT std::basic_string<base::char16, base::string16_char_traits>; |
| 182 | 184 |
| 185 // Specialize std::hash for base::string16 for consistency with WCHAR_T_IS_UTF16 |
| 186 // platforms, where base::string16 is std::wstring. |
| 187 namespace std { |
| 188 template<> |
| 189 struct hash<base::string16> { |
| 190 std::size_t operator()(const base::string16& s) const { |
| 191 std::size_t result = 0; |
| 192 for (base::char16 c : s) |
| 193 result = (result * 131) + c; |
| 194 return result; |
| 195 } |
| 196 }; |
| 197 } // namespace std |
| 198 |
| 183 #endif // WCHAR_T_IS_UTF32 | 199 #endif // WCHAR_T_IS_UTF32 |
| 184 | 200 |
| 185 #endif // BASE_STRINGS_STRING16_H_ | 201 #endif // BASE_STRINGS_STRING16_H_ |
| OLD | NEW |