summaryrefslogtreecommitdiff
path: root/lib/ktl/include/cwchar
blob: 4f24c73fc0687d2ba7ac668dd9c4b801c4b2df4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2020 The Fuchsia Authors
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT

#ifndef ZIRCON_KERNEL_LIB_KTL_INCLUDE_CWCHAR
#define ZIRCON_KERNEL_LIB_KTL_INCLUDE_CWCHAR

// The kernel doesn't want this file but some libc++ headers we need
// wind up including it and they need these declarations.

#include <wchar.h>

#include <__config>

_LIBCPP_BEGIN_NAMESPACE_STD

using ::wmemcpy;
using ::wmemmove;
using ::wmemset;

// These are called by libc++ header code.  The first two should never actually
// be called by anything that really gets instantiated in kernel code since
// wchar_t is never used.  However, __constexpr_wmemchr is used by places like
// std::find not just for wchar_t per se but for any trivial type of the same
// size, e.g. uint32_t.  So its code must be correct.  The other two are filled
// out just in case of an unexpected use.

constexpr size_t __constexpr_wcslen(const wchar_t* str) {
  size_t len = 0;
  while (*str++ != L'\0') {
    ++len;
  }
  return len;
}

constexpr int __constexpr_wmemcmp(const wchar_t* lhs, const wchar_t* rhs, size_t count) {
  while (count-- > 0) {
    if (*lhs != *rhs) {
      return static_cast<int>(*lhs - *rhs);
    }
    ++lhs;
    ++rhs;
  }
  return 0;
}

template <class Tp, class Up>
constexpr Tp* __constexpr_wmemchr(Tp* str, Up wc, size_t count) {
  while (count-- > 0) {
    if (*str == wc) {
      return str;
    }
    ++str;
  }
  return nullptr;
}

_LIBCPP_END_NAMESPACE_STD

#endif  // ZIRCON_KERNEL_LIB_KTL_INCLUDE_CWCHAR