llvm-project
44 строки · 1.3 Кб
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/stdlib/strfroml.h"10#include "src/stdlib/str_from_util.h"11
12namespace LIBC_NAMESPACE {13
14LLVM_LIBC_FUNCTION(int, strfroml,15(char *__restrict s, size_t n, const char *__restrict format,16long double fp)) {17LIBC_ASSERT(s != nullptr);18
19printf_core::FormatSection section =20internal::parse_format_string(format, fp);21
22// To ensure that the conversion function actually uses long double,23// the length modifier has to be set to LenghtModifier::L24section.length_modifier = printf_core::LengthModifier::L;25
26printf_core::WriteBuffer wb(s, (n > 0 ? n - 1 : 0));27printf_core::Writer writer(&wb);28
29int result = 0;30if (section.has_conv)31result = internal::strfromfloat_convert<long double>(&writer, section);32else33result = writer.write(section.raw_string);34
35if (result < 0)36return result;37
38if (n > 0)39wb.buff[wb.buff_cur] = '\0';40
41return writer.get_chars_written();42}
43
44} // namespace LIBC_NAMESPACE45