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