aboutsummaryrefslogtreecommitdiff
path: root/libc/src/__support/wchar/mbrtowc.cpp
blob: 66cc68e791d9902e68c561c71db6707ca5da4eac (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
//===-- Implementation for mbrtowc function ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/wchar/mbrtowc.h"
#include "hdr/errno_macros.h"
#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
#include "src/__support/wchar/character_converter.h"
#include "src/__support/wchar/mbstate.h"

namespace LIBC_NAMESPACE_DECL {
namespace internal {

ErrorOr<size_t> mbrtowc(wchar_t *__restrict pwc, const char *__restrict s,
                        size_t n, mbstate *__restrict ps) {
  CharacterConverter char_conv(ps);
  if (!char_conv.isValidState())
    return Error(EINVAL);
  if (s == nullptr)
    return 0;
  size_t i = 0;
  // Reading in bytes until we have a complete wc or error
  for (; i < n && !char_conv.isFull(); ++i) {
    int err = char_conv.push(static_cast<char8_t>(s[i]));
    // Encoding error
    if (err == EILSEQ)
      return Error(err);
  }
  auto wc = char_conv.pop_utf32();
  if (wc.has_value()) {
    if (pwc != nullptr)
      *pwc = wc.value();
    // null terminator -> return 0
    if (wc.value() == L'\0')
      return 0;
    return i;
  }
  // Incomplete but potentially valid
  return -2;
}

} // namespace internal

} // namespace LIBC_NAMESPACE_DECL