1//===-- Implementation of strfromf ------------------------------*- 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/strfromf.h"
10#include "src/stdlib/str_from_util.h"
11
12namespace LIBC_NAMESPACE {
13
14LLVM_LIBC_FUNCTION(int, strfromf,
15 (char *__restrict s, size_t n, const char *__restrict format,
16 float fp)) {
17 LIBC_ASSERT(s != nullptr);
18
19 printf_core::FormatSection section =
20 internal::parse_format_string(format, fp);
21 printf_core::WriteBuffer wb(s, (n > 0 ? n - 1 : 0));
22 printf_core::Writer writer(&wb);
23
24 int result = 0;
25 if (section.has_conv)
26 result = internal::strfromfloat_convert<float>(writer: &writer, section);
27 else
28 result = writer.write(new_string: section.raw_string);
29
30 if (result < 0)
31 return result;
32
33 if (n > 0)
34 wb.buff[wb.buff_cur] = '\0';
35
36 return writer.get_chars_written();
37}
38
39} // namespace LIBC_NAMESPACE
40

source code of libc/src/stdlib/strfromf.cpp