avi@chromium.org | d827e3d | 2013-06-08 15:05:47 +0900 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
mmentovai@google.com | 6009c2c | 2008-08-28 05:57:35 +0900 | [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 | |
avi@chromium.org | d827e3d | 2013-06-08 15:05:47 +0900 | [diff] [blame] | 5 | #include "base/strings/string16.h" |
mmentovai@google.com | 6009c2c | 2008-08-28 05:57:35 +0900 | [diff] [blame] | 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 | |
mark@chromium.org | 00c1b54 | 2010-10-08 03:23:28 +0900 | [diff] [blame] | 16 | #include <ostream> |
| 17 | |
avi@chromium.org | 17f6062 | 2013-06-08 03:37:07 +0900 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
thestig@chromium.org | c0fb395 | 2009-03-03 07:40:58 +0900 | [diff] [blame] | 19 | |
mmentovai@google.com | 6009c2c | 2008-08-28 05:57:35 +0900 | [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) { |
dilmah@chromium.org | 4fb1706 | 2011-08-26 22:07:29 +0900 | [diff] [blame] | 54 | return static_cast<char16*>(memmove(s1, s2, n * sizeof(char16))); |
mmentovai@google.com | 6009c2c | 2008-08-28 05:57:35 +0900 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | char16* c16memcpy(char16* s1, const char16* s2, size_t n) { |
dilmah@chromium.org | 4fb1706 | 2011-08-26 22:07:29 +0900 | [diff] [blame] | 58 | return static_cast<char16*>(memcpy(s1, s2, n * sizeof(char16))); |
mmentovai@google.com | 6009c2c | 2008-08-28 05:57:35 +0900 | [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 | |
thestig@chromium.org | c0fb395 | 2009-03-03 07:40:58 +0900 | [diff] [blame] | 70 | std::ostream& operator<<(std::ostream& out, const string16& str) { |
| 71 | return out << UTF16ToUTF8(str); |
| 72 | } |
gab@chromium.org | ca1d523 | 2012-03-09 03:34:50 +0900 | [diff] [blame] | 73 | |
| 74 | void PrintTo(const string16& str, std::ostream* out) { |
| 75 | *out << str; |
| 76 | } |
brettw@chromium.org | 63777d8 | 2013-03-29 04:51:28 +0900 | [diff] [blame] | 77 | |
| 78 | } // namespace base |
| 79 | |
brettw@chromium.org | 5b04085 | 2013-12-03 09:39:26 +0900 | [diff] [blame] | 80 | template class std::basic_string<base::char16, base::string16_char_traits>; |
thestig@chromium.org | c0fb395 | 2009-03-03 07:40:58 +0900 | [diff] [blame] | 81 | |
mmentovai@google.com | 6009c2c | 2008-08-28 05:57:35 +0900 | [diff] [blame] | 82 | #endif // WCHAR_T_IS_UTF32 |