41 lines · cpp
1//===-- Implementation of mbstowcs ----------------------------------------===//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/mbstowcs.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/macros/null_check.h"17#include "src/__support/wchar/mbsnrtowcs.h"18#include "src/__support/wchar/mbstate.h"19 20namespace LIBC_NAMESPACE_DECL {21 22LLVM_LIBC_FUNCTION(size_t, mbstowcs,23 (wchar_t *__restrict pwcs, const char *__restrict s,24 size_t n)) {25 LIBC_CRASH_ON_NULLPTR(s);26 // If destination is null, ignore n27 n = pwcs == nullptr ? SIZE_MAX : n;28 static internal::mbstate internal_mbstate;29 const char *temp = s;30 auto ret = internal::mbsnrtowcs(pwcs, &temp, SIZE_MAX, n, &internal_mbstate);31 32 if (!ret.has_value()) {33 // Encoding failure34 libc_errno = ret.error();35 return -1;36 }37 return ret.value();38}39 40} // namespace LIBC_NAMESPACE_DECL41