xref: /llvm-project/libc/src/stdio/vsprintf.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
116d5c242SMichael Jones //===-- Implementation of vsprintf ------------------------------*- C++ -*-===//
216d5c242SMichael Jones //
316d5c242SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
416d5c242SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
516d5c242SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
616d5c242SMichael Jones //
716d5c242SMichael Jones //===----------------------------------------------------------------------===//
816d5c242SMichael Jones 
916d5c242SMichael Jones #include "src/stdio/vsprintf.h"
1016d5c242SMichael Jones 
1116d5c242SMichael Jones #include "src/__support/CPP/limits.h"
1216d5c242SMichael Jones #include "src/__support/arg_list.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1416d5c242SMichael Jones #include "src/stdio/printf_core/printf_main.h"
1516d5c242SMichael Jones #include "src/stdio/printf_core/writer.h"
1616d5c242SMichael Jones 
1716d5c242SMichael Jones #include <stdarg.h>
1816d5c242SMichael Jones 
19*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
2016d5c242SMichael Jones 
2116d5c242SMichael Jones LLVM_LIBC_FUNCTION(int, vsprintf,
2216d5c242SMichael Jones                    (char *__restrict buffer, const char *__restrict format,
2316d5c242SMichael Jones                     va_list vlist)) {
2416d5c242SMichael Jones   internal::ArgList args(vlist); // This holder class allows for easier copying
2516d5c242SMichael Jones                                  // and pointer semantics, as well as handling
2616d5c242SMichael Jones                                  // destruction automatically.
2716d5c242SMichael Jones 
2816d5c242SMichael Jones   printf_core::WriteBuffer wb(buffer, cpp::numeric_limits<size_t>::max());
2916d5c242SMichael Jones   printf_core::Writer writer(&wb);
3016d5c242SMichael Jones 
3116d5c242SMichael Jones   int ret_val = printf_core::printf_main(&writer, format, args);
3216d5c242SMichael Jones   wb.buff[wb.buff_cur] = '\0';
3316d5c242SMichael Jones   return ret_val;
3416d5c242SMichael Jones }
3516d5c242SMichael Jones 
36*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
37