Browse Source
1. Use `fmt::format_int` directly instead of parsing a format string. 2. Use `AppendStrView`. 3. Define the varargs versions using fold expressions when available. 4. Add tests.pull/5884/head
5 changed files with 52 additions and 20 deletions
@ -1,28 +1,20 @@
|
||||
#include "utils/str_cat.hpp" |
||||
|
||||
#include <cstddef> |
||||
|
||||
#include <fmt/compile.h> |
||||
#include <fmt/core.h> |
||||
#include <fmt/format.h> |
||||
|
||||
namespace devilution { |
||||
|
||||
namespace { |
||||
|
||||
template <typename T> |
||||
constexpr size_t MaxDecimalDigits = 241 * sizeof(T) / 100 + 1; |
||||
|
||||
} // namespace
|
||||
|
||||
char *BufCopy(char *out, int value) |
||||
{ |
||||
return fmt::format_to(out, FMT_COMPILE("{}"), value); |
||||
const fmt::format_int formatted { value }; |
||||
std::memcpy(out, formatted.data(), formatted.size()); |
||||
return out + formatted.size(); |
||||
} |
||||
|
||||
void StrAppend(std::string &out, int value) |
||||
{ |
||||
char buf[MaxDecimalDigits<int> + 1]; |
||||
out.append(&buf[0], BufCopy(buf, value) - &buf[0]); |
||||
const fmt::format_int formatted { value }; |
||||
out.append(formatted.data(), formatted.size()); |
||||
} |
||||
|
||||
} // namespace devilution
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
#include <gtest/gtest.h> |
||||
|
||||
#include "utils/str_cat.hpp" |
||||
|
||||
namespace devilution { |
||||
namespace { |
||||
|
||||
TEST(StrCatTest, StrCatBasicTest) |
||||
{ |
||||
EXPECT_EQ(StrCat("a", "b", "c", 5), "abc5"); |
||||
} |
||||
|
||||
TEST(StrCatTest, BufCopyBasicTest) |
||||
{ |
||||
char buf[5]; |
||||
char *end = BufCopy(buf, "a", "b", "c", 5); |
||||
EXPECT_EQ(string_view(buf, end - buf), "abc5"); |
||||
} |
||||
|
||||
} // namespace
|
||||
} // namespace devilution
|
||||
Loading…
Reference in new issue