38 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/stdlib/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 19namespace LIBC_NAMESPACE_DECL {20 21LLVM_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-dependent25 if (s == nullptr)26 return 0;27 internal::mbstate internal_mbstate;28 auto ret = internal::mbrtowc(pwc, s, n, &internal_mbstate);29 if (!ret.has_value() || static_cast<int>(ret.value()) == -2) {30 // Encoding failure31 libc_errno = EILSEQ;32 return -1;33 }34 return static_cast<int>(ret.value());35}36 37} // namespace LIBC_NAMESPACE_DECL38