1// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are
5// met:
6//
7//    * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9//    * Redistributions in binary form must reproduce the above
10// copyright notice, this list of conditions and the following disclaimer
11// in the documentation and/or other materials provided with the
12// distribution.
13//    * Neither the name of Google Inc. nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "base/string16.h"
30
31#ifdef WIN32
32
33#error This file should not be used on 2-byte wchar_t systems
34// If this winds up being needed on 2-byte wchar_t systems, either the
35// definitions below can be used, or the host system's wide character
36// functions like wmemcmp can be wrapped.
37
38#else  // !WIN32
39
40namespace base {
41
42int c16memcmp(const char16* s1, const char16* s2, size_t n) {
43  // We cannot call memcmp because that changes the semantics.
44  while (n-- > 0) {
45    if (*s1 != *s2) {
46      // We cannot use (*s1 - *s2) because char16 is unsigned.
47      return ((*s1 < *s2) ? -1 : 1);
48    }
49    ++s1;
50    ++s2;
51  }
52  return 0;
53}
54
55size_t c16len(const char16* s) {
56  const char16 *s_orig = s;
57  while (*s) {
58    ++s;
59  }
60  return s - s_orig;
61}
62
63const char16* c16memchr(const char16* s, char16 c, size_t n) {
64  while (n-- > 0) {
65    if (*s == c) {
66      return s;
67    }
68    ++s;
69  }
70  return 0;
71}
72
73char16* c16memmove(char16* s1, const char16* s2, size_t n) {
74  return reinterpret_cast<char16*>(memmove(s1, s2, n * sizeof(char16)));
75}
76
77char16* c16memcpy(char16* s1, const char16* s2, size_t n) {
78  return reinterpret_cast<char16*>(memcpy(s1, s2, n * sizeof(char16)));
79}
80
81char16* c16memset(char16* s, char16 c, size_t n) {
82  char16 *s_orig = s;
83  while (n-- > 0) {
84    *s = c;
85    ++s;
86  }
87  return s_orig;
88}
89
90}  // namespace base
91
92template class std::basic_string<char16, base::string16_char_traits>;
93
94#endif  // WIN32
95