Path: blob/main/system/lib/llvm-libc/src/wchar/mbtowc.cpp
6174 views
//===-- Implementation of mbtowc -----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "src/wchar/mbtowc.h"910#include "hdr/types/size_t.h"11#include "hdr/types/wchar_t.h"12#include "src/__support/common.h"13#include "src/__support/libc_errno.h"14#include "src/__support/macros/config.h"15#include "src/__support/wchar/mbrtowc.h"16#include "src/__support/wchar/mbstate.h"1718namespace LIBC_NAMESPACE_DECL {1920LLVM_LIBC_FUNCTION(int, mbtowc,21(wchar_t *__restrict pwc, const char *__restrict s,22size_t n)) {23// returns 0 since UTF-8 encoding is not state-dependent24if (s == nullptr)25return 0;26internal::mbstate internal_mbstate;27// temp ptr to use if pwc is nullptr28wchar_t buf[1];29auto ret =30internal::mbrtowc(pwc == nullptr ? buf : pwc, s, n, &internal_mbstate);31if (!ret.has_value() || static_cast<int>(ret.value()) == -2) {32// Encoding failure33libc_errno = EILSEQ;34return -1;35}36return static_cast<int>(ret.value());37}3839} // namespace LIBC_NAMESPACE_DECL404142