blob: ca45fba6be7ec270cf2551693bbc33c97104f6ef [file] [log] [blame]
[email protected]d13701902008-08-27 20:57:351// 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
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]a0ea1bc2009-03-02 22:40:5816#include "base/string_util.h"
17
[email protected]d13701902008-08-27 20:57:3518namespace base {
19
20int c16memcmp(const char16* s1, const char16* s2, size_t n) {
21 // We cannot call memcmp because that changes the semantics.
22 while (n-- > 0) {
23 if (*s1 != *s2) {
24 // We cannot use (*s1 - *s2) because char16 is unsigned.
25 return ((*s1 < *s2) ? -1 : 1);
26 }
27 ++s1;
28 ++s2;
29 }
30 return 0;
31}
32
33size_t c16len(const char16* s) {
34 const char16 *s_orig = s;
35 while (*s) {
36 ++s;
37 }
38 return s - s_orig;
39}
40
41const char16* c16memchr(const char16* s, char16 c, size_t n) {
42 while (n-- > 0) {
43 if (*s == c) {
44 return s;
45 }
46 ++s;
47 }
48 return 0;
49}
50
51char16* c16memmove(char16* s1, const char16* s2, size_t n) {
52 return reinterpret_cast<char16*>(memmove(s1, s2, n * sizeof(char16)));
53}
54
55char16* c16memcpy(char16* s1, const char16* s2, size_t n) {
56 return reinterpret_cast<char16*>(memcpy(s1, s2, n * sizeof(char16)));
57}
58
59char16* c16memset(char16* s, char16 c, size_t n) {
60 char16 *s_orig = s;
61 while (n-- > 0) {
62 *s = c;
63 ++s;
64 }
65 return s_orig;
66}
67
68} // namespace base
69
[email protected]1bc2de42009-03-24 23:32:1370template class std::basic_string<char16, base::string16_char_traits>;
71
[email protected]a0ea1bc2009-03-02 22:40:5872std::ostream& operator<<(std::ostream& out, const string16& str) {
73 return out << UTF16ToUTF8(str);
74}
75
[email protected]d13701902008-08-27 20:57:3576#endif // WCHAR_T_IS_UTF32