blob: d1d09082ac1996656ef675b66b573fc36b2294ea [file] [log] [blame]
[email protected]047a03f2009-10-07 02:10:201// Copyright (c) 2009 The Chromium Authors. All rights reserved.
[email protected]d13701902008-08-27 20:57:352// 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"
[email protected]047a03f2009-10-07 02:10:2017#include "base/utf_string_conversions.h"
[email protected]a0ea1bc2009-03-02 22:40:5818
[email protected]d13701902008-08-27 20:57:3519namespace base {
20
21int c16memcmp(const char16* s1, const char16* s2, size_t n) {
22 // We cannot call memcmp because that changes the semantics.
23 while (n-- > 0) {
24 if (*s1 != *s2) {
25 // We cannot use (*s1 - *s2) because char16 is unsigned.
26 return ((*s1 < *s2) ? -1 : 1);
27 }
28 ++s1;
29 ++s2;
30 }
31 return 0;
32}
33
34size_t c16len(const char16* s) {
35 const char16 *s_orig = s;
36 while (*s) {
37 ++s;
38 }
39 return s - s_orig;
40}
41
42const char16* c16memchr(const char16* s, char16 c, size_t n) {
43 while (n-- > 0) {
44 if (*s == c) {
45 return s;
46 }
47 ++s;
48 }
49 return 0;
50}
51
52char16* c16memmove(char16* s1, const char16* s2, size_t n) {
53 return reinterpret_cast<char16*>(memmove(s1, s2, n * sizeof(char16)));
54}
55
56char16* c16memcpy(char16* s1, const char16* s2, size_t n) {
57 return reinterpret_cast<char16*>(memcpy(s1, s2, n * sizeof(char16)));
58}
59
60char16* c16memset(char16* s, char16 c, size_t n) {
61 char16 *s_orig = s;
62 while (n-- > 0) {
63 *s = c;
64 ++s;
65 }
66 return s_orig;
67}
68
69} // namespace base
70
[email protected]1bc2de42009-03-24 23:32:1371template class std::basic_string<char16, base::string16_char_traits>;
72
[email protected]a0ea1bc2009-03-02 22:40:5873std::ostream& operator<<(std::ostream& out, const string16& str) {
74 return out << UTF16ToUTF8(str);
75}
76
[email protected]d13701902008-08-27 20:57:3577#endif // WCHAR_T_IS_UTF32