52 lines · cpp
1//===-- Implementation of strfromd ------------------------------*- 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/strfromd.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, strfromd,19 (char *__restrict s, size_t n, const char *__restrict format,20 double fp)) {21 LIBC_ASSERT(s != nullptr);22 23 printf_core::FormatSection section =24 internal::parse_format_string(format, fp);25 printf_core::WriteBuffer<printf_core::Mode<26 printf_core::WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>::value>27 wb(s, (n > 0 ? n - 1 : 0));28 printf_core::Writer writer(wb);29 30 int result = 0;31 if (section.has_conv)32 result = internal::strfromfloat_convert<double>(&writer, section);33 else34 result = writer.write(section.raw_string);35 36 if (result < 0)37 return result;38 39 if (n > 0)40 wb.buff[wb.buff_cur] = '\0';41 42 if (writer.get_chars_written() >43 static_cast<size_t>(cpp::numeric_limits<int>::max())) {44 libc_errno =45 printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);46 return -1;47 }48 return static_cast<int>(writer.get_chars_written());49}50 51} // namespace LIBC_NAMESPACE_DECL52