52 lines · cpp
1//===-- Implementation of vsprintf ------------------------------*- 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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/stdio/vsprintf.h"10 11#include "src/__support/CPP/limits.h"12#include "src/__support/arg_list.h"13#include "src/__support/libc_errno.h"14#include "src/__support/macros/config.h"15#include "src/stdio/printf_core/core_structs.h"16#include "src/stdio/printf_core/error_mapper.h"17#include "src/stdio/printf_core/printf_main.h"18#include "src/stdio/printf_core/writer.h"19 20#include <stdarg.h>21 22namespace LIBC_NAMESPACE_DECL {23 24LLVM_LIBC_FUNCTION(int, vsprintf,25 (char *__restrict buffer, const char *__restrict format,26 va_list vlist)) {27 internal::ArgList args(vlist); // This holder class allows for easier copying28 // and pointer semantics, as well as handling29 // destruction automatically.30 31 printf_core::WriteBuffer<printf_core::Mode<32 printf_core::WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>::value>33 wb(buffer, cpp::numeric_limits<size_t>::max());34 printf_core::Writer writer(wb);35 36 auto ret_val = printf_core::printf_main(&writer, format, args);37 if (!ret_val.has_value()) {38 libc_errno = printf_core::internal_error_to_errno(ret_val.error());39 return -1;40 }41 wb.buff[wb.buff_cur] = '\0';42 43 if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {44 libc_errno =45 printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);46 return -1;47 }48 return static_cast<int>(ret_val.value());49}50 51} // namespace LIBC_NAMESPACE_DECL52