| //===-- Implementation of strfromd ------------------------------*- C++ -*-===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #include "src/stdlib/strfromd.h" | |
| #include "src/__support/CPP/limits.h" | |
| #include "src/__support/macros/config.h" | |
| #include "src/__support/printf_core/core_structs.h" | |
| #include "src/__support/printf_core/error_mapper.h" | |
| #include "src/stdlib/str_from_util.h" | |
| namespace LIBC_NAMESPACE_DECL { | |
| LLVM_LIBC_FUNCTION(int, strfromd, | |
| (char *__restrict s, size_t n, const char *__restrict format, | |
| double fp)) { | |
| LIBC_ASSERT(s != nullptr); | |
| printf_core::Writer writer = | |
| printf_core::make_drop_overflow_writer(s, (n > 0 ? n - 1 : 0)); | |
| int result = internal::strfromfloat_convert(&writer, format, fp); | |
| if (result < 0) | |
| return result; | |
| if (n > 0) { | |
| printf_core::WriteBuffer<char> &wb = writer.get_write_buffer(); | |
| wb.buff[wb.buff_cur] = '\0'; | |
| } | |
| if (writer.get_chars_written() > | |
| static_cast<size_t>(cpp::numeric_limits<int>::max())) { | |
| libc_errno = | |
| printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR); | |
| return -1; | |
| } | |
| return static_cast<int>(writer.get_chars_written()); | |
| } | |
| } // namespace LIBC_NAMESPACE_DECL |