[win/asan] GetInstructionSize: Fix `83 E4 XX` to return 3. (#119644)
[llvm-project.git] / libc / src / stdlib / strfroml.cpp
blobd5bee7609f69caee0ac427ea47f11caff32b62ec
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 //===----------------------------------------------------------------------===//
9 #include "src/stdlib/strfroml.h"
10 #include "src/__support/macros/config.h"
11 #include "src/stdlib/str_from_util.h"
13 namespace LIBC_NAMESPACE_DECL {
15 LLVM_LIBC_FUNCTION(int, strfroml,
16 (char *__restrict s, size_t n, const char *__restrict format,
17 long double fp)) {
18 LIBC_ASSERT(s != nullptr);
20 printf_core::FormatSection section =
21 internal::parse_format_string(format, fp);
23 // To ensure that the conversion function actually uses long double,
24 // the length modifier has to be set to LenghtModifier::L
25 section.length_modifier = printf_core::LengthModifier::L;
27 printf_core::WriteBuffer wb(s, (n > 0 ? n - 1 : 0));
28 printf_core::Writer writer(&wb);
30 int result = 0;
31 if (section.has_conv)
32 result = internal::strfromfloat_convert<long double>(&writer, section);
33 else
34 result = writer.write(section.raw_string);
36 if (result < 0)
37 return result;
39 if (n > 0)
40 wb.buff[wb.buff_cur] = '\0';
42 return writer.get_chars_written();
45 } // namespace LIBC_NAMESPACE_DECL