[libc++][Android] Allow testing libc++ with clang-r536225 (#116149)
[llvm-project.git] / libc / src / stdlib / strfromd.cpp
blob4c51e4c5c8a01104f0f81b4bb88ecc73b4c362f3
1 //===-- Implementation of strfromd ------------------------------*- 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/strfromd.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, strfromd,
16 (char *__restrict s, size_t n, const char *__restrict format,
17 double fp)) {
18 LIBC_ASSERT(s != nullptr);
20 printf_core::FormatSection section =
21 internal::parse_format_string(format, fp);
22 printf_core::WriteBuffer wb(s, (n > 0 ? n - 1 : 0));
23 printf_core::Writer writer(&wb);
25 int result = 0;
26 if (section.has_conv)
27 result = internal::strfromfloat_convert<double>(&writer, section);
28 else
29 result = writer.write(section.raw_string);
31 if (result < 0)
32 return result;
34 if (n > 0)
35 wb.buff[wb.buff_cur] = '\0';
37 return writer.get_chars_written();
40 } // namespace LIBC_NAMESPACE_DECL