brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · f593a0e Raw
70 lines · c
1//===-- Implementation header for wcsnrtombs ------------------------------===//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#ifndef LLVM_LIBC_SRC__SUPPORT_WCHAR_WCSNRTOMBS_H10#define LLVM_LIBC_SRC__SUPPORT_WCHAR_WCSNRTOMBS_H11 12#include "hdr/types/char32_t.h"13#include "hdr/types/size_t.h"14#include "hdr/types/wchar_t.h"15#include "src/__support/common.h"16#include "src/__support/libc_errno.h"17#include "src/__support/macros/config.h"18#include "src/__support/macros/null_check.h"19#include "src/__support/wchar/mbstate.h"20#include "src/__support/wchar/string_converter.h"21 22namespace LIBC_NAMESPACE_DECL {23namespace internal {24 25LIBC_INLINE static ErrorOr<size_t>26wcsnrtombs(char *__restrict dest, const wchar_t **__restrict ptr_to_src,27           size_t num_src_widechars, size_t dest_len, mbstate *ps) {28  LIBC_CRASH_ON_NULLPTR(ptr_to_src);29  LIBC_CRASH_ON_NULLPTR(ps);30 31  CharacterConverter cr(ps);32  if (!cr.isValidState())33    return Error(EINVAL);34 35  if (dest == nullptr)36    dest_len = SIZE_MAX;37 38  StringConverter<char32_t> str_conv(39      reinterpret_cast<const char32_t *>(*ptr_to_src), ps, dest_len,40      num_src_widechars);41  size_t dst_idx = 0;42  ErrorOr<char8_t> converted = str_conv.pop<char8_t>();43  while (converted.has_value()) {44    if (dest != nullptr)45      dest[dst_idx] = converted.value();46 47    if (converted.value() == '\0') {48      if (dest != nullptr)49        *ptr_to_src = nullptr;50      return dst_idx;51    }52 53    dst_idx++;54    converted = str_conv.pop<char8_t>();55  }56 57  if (dest != nullptr)58    *ptr_to_src += str_conv.getSourceIndex();59 60  if (converted.error() == -1) // if we hit conversion limit61    return dst_idx;62 63  return Error(converted.error());64}65 66} // namespace internal67} // namespace LIBC_NAMESPACE_DECL68 69#endif // LLVM_LIBC_SRC__SUPPORT_WCHAR_WCSNRTOMBS_H70