Path: blob/main/contrib/llvm-project/libcxx/src/filesystem/format_string.h
35231 views
//===----------------------------------------------------------------------===////1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===////78#ifndef FILESYSTEM_FORMAT_STRING_H9#define FILESYSTEM_FORMAT_STRING_H1011#include <__assert>12#include <__config>13#include <array>14#include <cstdarg>15#include <cstddef>16#include <cstdio>17#include <string>1819#if defined(_LIBCPP_WIN32API)20# define PATHSTR(x) (L##x)21# define PATH_CSTR_FMT "\"%ls\""22#else23# define PATHSTR(x) (x)24# define PATH_CSTR_FMT "\"%s\""25#endif2627_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM2829namespace detail {3031inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 1, 0) string vformat_string(const char* msg, va_list ap) {32array<char, 256> buf;3334va_list apcopy;35va_copy(apcopy, ap);36int ret = ::vsnprintf(buf.data(), buf.size(), msg, apcopy);37va_end(apcopy);3839string result;40if (static_cast<size_t>(ret) < buf.size()) {41result.assign(buf.data(), static_cast<size_t>(ret));42} else {43// we did not provide a long enough buffer on our first attempt. The44// return value is the number of bytes (excluding the null byte) that are45// needed for formatting.46size_t size_with_null = static_cast<size_t>(ret) + 1;47result.__resize_default_init(size_with_null - 1);48ret = ::vsnprintf(&result[0], size_with_null, msg, ap);49_LIBCPP_ASSERT_INTERNAL(static_cast<size_t>(ret) == (size_with_null - 1), "TODO");50}51return result;52}5354inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 1, 2) string format_string(const char* msg, ...) {55string ret;56va_list ap;57va_start(ap, msg);58#ifndef _LIBCPP_HAS_NO_EXCEPTIONS59try {60#endif // _LIBCPP_HAS_NO_EXCEPTIONS61ret = detail::vformat_string(msg, ap);62#ifndef _LIBCPP_HAS_NO_EXCEPTIONS63} catch (...) {64va_end(ap);65throw;66}67#endif // _LIBCPP_HAS_NO_EXCEPTIONS68va_end(ap);69return ret;70}7172} // end namespace detail7374_LIBCPP_END_NAMESPACE_FILESYSTEM7576#endif // FILESYSTEM_FORMAT_STRING_H777879