llvm-project/libcxx/test/support/format_string.h
Gabriel Ravier c23e2c015f [libcxx] Fixed a number of typos
I went over the output of the following mess of a command:

`(ulimit -m 2000000; ulimit -v 2000000; git ls-files -z | parallel --xargs -0 cat | aspell list --mode=none --ignore-case | grep -E '^[A-Za-z][a-z]*$' | sort | uniq -c | sort -n | grep -vE '.{25}' | aspell pipe -W3 | grep : | cut -d' ' -f2 | less)`

and proceeded to spend a few days looking at it to find probable typos
and fixed a few hundred of them in all of the llvm project (note, the
ones I found are not anywhere near all of them, but it seems like a
good start).

Reviewed By: #libc, philnik

Spies: philnik, libcxx-commits, mgorny, arichardson

Differential Revision: https://reviews.llvm.org/D130905
2022-08-02 12:42:34 +02:00

72 lines
1.9 KiB
C++

#ifndef TEST_SUPPORT_FORMAT_STRING_H
#define TEST_SUPPORT_FORMAT_STRING_H
#include <cstdio>
#include <string>
#include <memory>
#include <array>
#include <cstdarg>
namespace format_string_detail {
inline std::string format_string_imp(const char* msg, ...) {
// we might need a second shot at this, so pre-emptively make a copy
struct GuardVAList {
va_list& xtarget;
bool active;
GuardVAList(va_list& val) : xtarget(val), active(true) {}
void clear() {
if (active)
va_end(xtarget);
active = false;
}
~GuardVAList() {
if (active)
va_end(xtarget);
}
};
va_list args;
va_start(args, msg);
GuardVAList args_guard(args);
va_list args_cp;
va_copy(args_cp, args);
GuardVAList args_copy_guard(args_cp);
std::array<char, 256> local_buff;
std::size_t size = local_buff.size();
auto ret = ::vsnprintf(local_buff.data(), size, msg, args_cp);
args_copy_guard.clear();
// handle empty expansion
if (ret == 0)
return std::string{};
if (static_cast<std::size_t>(ret) < size)
return std::string(local_buff.data());
// we did not provide a long enough buffer on our first attempt.
// add 1 to size to account for null-byte in size cast to prevent overflow
size = static_cast<std::size_t>(ret) + 1;
auto buff_ptr = std::unique_ptr<char[]>(new char[size]);
ret = ::vsnprintf(buff_ptr.get(), size, msg, args);
return std::string(buff_ptr.get());
}
const char* unwrap(std::string& s) { return s.c_str(); }
template <class Arg>
Arg const& unwrap(Arg& a) {
static_assert(!std::is_class<Arg>::value, "cannot pass class here");
return a;
}
} // namespace format_string_detail
template <class... Args>
std::string format_string(const char* fmt, Args const&... args) {
return format_string_detail::format_string_imp(
fmt, format_string_detail::unwrap(const_cast<Args&>(args))...);
}
#endif // TEST_SUPPORT_FORMAT_STRING_HPP