| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_STRING16_H_ | 5 #ifndef BASE_STRING16_H_ |
| 6 #define BASE_STRING16_H_ | 6 #define BASE_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 <string> | 30 #include <string> |
| 30 | 31 |
| 31 #include "base/basictypes.h" | 32 #include "base/basictypes.h" |
| 32 | 33 |
| 33 #if defined(WCHAR_T_IS_UTF16) | 34 #if defined(WCHAR_T_IS_UTF16) |
| 34 | 35 |
| 35 typedef wchar_t char16; | 36 typedef wchar_t char16; |
| 36 typedef std::wstring string16; | 37 typedef std::wstring string16; |
| 37 | 38 |
| 38 #elif defined(WCHAR_T_IS_UTF32) | 39 #elif defined(WCHAR_T_IS_UTF32) |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 123 |
| 123 } // namespace base | 124 } // namespace base |
| 124 | 125 |
| 125 typedef std::basic_string<char16, base::string16_char_traits> string16; | 126 typedef std::basic_string<char16, base::string16_char_traits> string16; |
| 126 | 127 |
| 127 extern std::ostream& operator<<(std::ostream& out, const string16& str); | 128 extern std::ostream& operator<<(std::ostream& out, const string16& str); |
| 128 | 129 |
| 129 #endif // WCHAR_T_IS_UTF32 | 130 #endif // WCHAR_T_IS_UTF32 |
| 130 | 131 |
| 131 #endif // BASE_STRING16_H_ | 132 #endif // BASE_STRING16_H_ |
| OLD | NEW |