[email protected] | 15785494 | 2012-03-08 18:34:50 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d1370190 | 2008-08-27 20:57:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/string16.h" |
| 6 | |
| 7 | #if defined(WCHAR_T_IS_UTF16) |
| 8 | |
| 9 | #error This file should not be used on 2-byte wchar_t systems |
| 10 | // If this winds up being needed on 2-byte wchar_t systems, either the |
| 11 | // definitions below can be used, or the host system's wide character |
| 12 | // functions like wmemcmp can be wrapped. |
| 13 | |
| 14 | #elif defined(WCHAR_T_IS_UTF32) |
| 15 | |
[email protected] | 5fe482a | 2010-10-07 18:23:28 | [diff] [blame] | 16 | #include <ostream> |
| 17 | |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 18 | #include "base/utf_string_conversions.h" |
[email protected] | a0ea1bc | 2009-03-02 22:40:58 | [diff] [blame] | 19 | |
[email protected] | d1370190 | 2008-08-27 20:57:35 | [diff] [blame] | 20 | namespace base { |
| 21 | |
| 22 | int c16memcmp(const char16* s1, const char16* s2, size_t n) { |
| 23 | // We cannot call memcmp because that changes the semantics. |
| 24 | while (n-- > 0) { |
| 25 | if (*s1 != *s2) { |
| 26 | // We cannot use (*s1 - *s2) because char16 is unsigned. |
| 27 | return ((*s1 < *s2) ? -1 : 1); |
| 28 | } |
| 29 | ++s1; |
| 30 | ++s2; |
| 31 | } |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | size_t c16len(const char16* s) { |
| 36 | const char16 *s_orig = s; |
| 37 | while (*s) { |
| 38 | ++s; |
| 39 | } |
| 40 | return s - s_orig; |
| 41 | } |
| 42 | |
| 43 | const char16* c16memchr(const char16* s, char16 c, size_t n) { |
| 44 | while (n-- > 0) { |
| 45 | if (*s == c) { |
| 46 | return s; |
| 47 | } |
| 48 | ++s; |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | char16* c16memmove(char16* s1, const char16* s2, size_t n) { |
[email protected] | c95c886 | 2011-08-26 13:07:29 | [diff] [blame] | 54 | return static_cast<char16*>(memmove(s1, s2, n * sizeof(char16))); |
[email protected] | d1370190 | 2008-08-27 20:57:35 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | char16* c16memcpy(char16* s1, const char16* s2, size_t n) { |
[email protected] | c95c886 | 2011-08-26 13:07:29 | [diff] [blame] | 58 | return static_cast<char16*>(memcpy(s1, s2, n * sizeof(char16))); |
[email protected] | d1370190 | 2008-08-27 20:57:35 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | char16* c16memset(char16* s, char16 c, size_t n) { |
| 62 | char16 *s_orig = s; |
| 63 | while (n-- > 0) { |
| 64 | *s = c; |
| 65 | ++s; |
| 66 | } |
| 67 | return s_orig; |
| 68 | } |
| 69 | |
| 70 | } // namespace base |
| 71 | |
[email protected] | 1bc2de4 | 2009-03-24 23:32:13 | [diff] [blame] | 72 | template class std::basic_string<char16, base::string16_char_traits>; |
| 73 | |
[email protected] | 1599077 | 2010-10-01 20:03:11 | [diff] [blame] | 74 | namespace base { |
[email protected] | a0ea1bc | 2009-03-02 22:40:58 | [diff] [blame] | 75 | std::ostream& operator<<(std::ostream& out, const string16& str) { |
| 76 | return out << UTF16ToUTF8(str); |
| 77 | } |
[email protected] | 15785494 | 2012-03-08 18:34:50 | [diff] [blame^] | 78 | |
| 79 | void PrintTo(const string16& str, std::ostream* out) { |
| 80 | *out << str; |
| 81 | } |
[email protected] | 1599077 | 2010-10-01 20:03:11 | [diff] [blame] | 82 | } |
[email protected] | a0ea1bc | 2009-03-02 22:40:58 | [diff] [blame] | 83 | |
[email protected] | d1370190 | 2008-08-27 20:57:35 | [diff] [blame] | 84 | #endif // WCHAR_T_IS_UTF32 |