blob: 2a7ec9c850cb1426430c24d81d28609f85601174 [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
16namespace base {
17
18int c16memcmp(const char16* s1, const char16* s2, size_t n) {
19 // We cannot call memcmp because that changes the semantics.
20 while (n-- > 0) {
21 if (*s1 != *s2) {
22 // We cannot use (*s1 - *s2) because char16 is unsigned.
23 return ((*s1 < *s2) ? -1 : 1);
24 }
25 ++s1;
26 ++s2;
27 }
28 return 0;
29}
30
31size_t c16len(const char16* s) {
32 const char16 *s_orig = s;
33 while (*s) {
34 ++s;
35 }
36 return s - s_orig;
37}
38
39const char16* c16memchr(const char16* s, char16 c, size_t n) {
40 while (n-- > 0) {
41 if (*s == c) {
42 return s;
43 }
44 ++s;
45 }
46 return 0;
47}
48
49char16* c16memmove(char16* s1, const char16* s2, size_t n) {
50 return reinterpret_cast<char16*>(memmove(s1, s2, n * sizeof(char16)));
51}
52
53char16* c16memcpy(char16* s1, const char16* s2, size_t n) {
54 return reinterpret_cast<char16*>(memcpy(s1, s2, n * sizeof(char16)));
55}
56
57char16* c16memset(char16* s, char16 c, size_t n) {
58 char16 *s_orig = s;
59 while (n-- > 0) {
60 *s = c;
61 ++s;
62 }
63 return s_orig;
64}
65
66} // namespace base
67
68#endif // WCHAR_T_IS_UTF32