Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/llvm-libc/src/wchar/mbtowc.cpp
6174 views
1
//===-- Implementation of mbtowc -----------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "src/wchar/mbtowc.h"
10
11
#include "hdr/types/size_t.h"
12
#include "hdr/types/wchar_t.h"
13
#include "src/__support/common.h"
14
#include "src/__support/libc_errno.h"
15
#include "src/__support/macros/config.h"
16
#include "src/__support/wchar/mbrtowc.h"
17
#include "src/__support/wchar/mbstate.h"
18
19
namespace LIBC_NAMESPACE_DECL {
20
21
LLVM_LIBC_FUNCTION(int, mbtowc,
22
(wchar_t *__restrict pwc, const char *__restrict s,
23
size_t n)) {
24
// returns 0 since UTF-8 encoding is not state-dependent
25
if (s == nullptr)
26
return 0;
27
internal::mbstate internal_mbstate;
28
// temp ptr to use if pwc is nullptr
29
wchar_t buf[1];
30
auto ret =
31
internal::mbrtowc(pwc == nullptr ? buf : pwc, s, n, &internal_mbstate);
32
if (!ret.has_value() || static_cast<int>(ret.value()) == -2) {
33
// Encoding failure
34
libc_errno = EILSEQ;
35
return -1;
36
}
37
return static_cast<int>(ret.value());
38
}
39
40
} // namespace LIBC_NAMESPACE_DECL
41
42