brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · 31668a0 Raw
57 lines · cpp
1//===-- Implementation of strfroml ------------------------------*- C++ -*-===//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/strfroml.h"10#include "src/__support/CPP/limits.h"11#include "src/__support/macros/config.h"12#include "src/stdio/printf_core/core_structs.h"13#include "src/stdio/printf_core/error_mapper.h"14#include "src/stdlib/str_from_util.h"15 16namespace LIBC_NAMESPACE_DECL {17 18LLVM_LIBC_FUNCTION(int, strfroml,19                   (char *__restrict s, size_t n, const char *__restrict format,20                    long double fp)) {21  LIBC_ASSERT(s != nullptr);22 23  printf_core::FormatSection section =24      internal::parse_format_string(format, fp);25 26  // To ensure that the conversion function actually uses long double,27  // the length modifier has to be set to LenghtModifier::L28  section.length_modifier = printf_core::LengthModifier::L;29 30  printf_core::WriteBuffer<printf_core::Mode<31      printf_core::WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>::value>32      wb(s, (n > 0 ? n - 1 : 0));33  printf_core::Writer writer(wb);34 35  int result = 0;36  if (section.has_conv)37    result = internal::strfromfloat_convert<long double>(&writer, section);38  else39    result = writer.write(section.raw_string);40 41  if (result < 0)42    return result;43 44  if (n > 0)45    wb.buff[wb.buff_cur] = '\0';46 47  if (writer.get_chars_written() >48      static_cast<size_t>(cpp::numeric_limits<int>::max())) {49    libc_errno =50        printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);51    return -1;52  }53  return static_cast<int>(writer.get_chars_written());54}55 56} // namespace LIBC_NAMESPACE_DECL57