mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 00:11:55 +07:00
nothrow variants for strings format
This commit is contained in:
@@ -51,7 +51,18 @@ namespace e2d
|
||||
str_view fmt, Args&&... args);
|
||||
|
||||
template < typename... Args >
|
||||
str rformat(str_view fmt, Args&&... args);
|
||||
bool format_nothrow(
|
||||
char* dst, std::size_t dst_size, std::size_t* length,
|
||||
str_view fmt, Args&&... args) noexcept;
|
||||
|
||||
template < typename... Args >
|
||||
str rformat(
|
||||
str_view fmt, Args&&... args);
|
||||
|
||||
template < typename... Args >
|
||||
bool rformat_nothrow(
|
||||
str& dst,
|
||||
str_view fmt, Args&&... args) noexcept;
|
||||
|
||||
bool wildcard_match(str_view string, str_view pattern);
|
||||
}
|
||||
|
||||
@@ -385,6 +385,23 @@ namespace e2d { namespace strings
|
||||
std::make_tuple(impl::wrap_arg(std::forward<Args>(args))...));
|
||||
}
|
||||
|
||||
template < typename... Args >
|
||||
bool format_nothrow(
|
||||
char* dst, std::size_t dst_size, std::size_t* length,
|
||||
str_view fmt, Args&&... args) noexcept
|
||||
{
|
||||
try {
|
||||
std::size_t result = format(
|
||||
dst, dst_size, fmt, std::forward<Args>(args)...);
|
||||
if ( length ) {
|
||||
*length = result;
|
||||
}
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template < typename... Args >
|
||||
str rformat(str_view fmt, Args&&... args) {
|
||||
auto targs = std::make_tuple(
|
||||
@@ -397,6 +414,16 @@ namespace e2d { namespace strings
|
||||
E2D_ASSERT(expected_format_size == actual_format_size);
|
||||
return str(buffer.data(), buffer.data() + actual_format_size);
|
||||
}
|
||||
|
||||
template < typename... Args >
|
||||
bool rformat_nothrow(str& dst, str_view fmt, Args&&... args) noexcept {
|
||||
try {
|
||||
dst = rformat(fmt, std::forward<Args>(args)...);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -250,6 +250,18 @@ TEST_CASE("strings") {
|
||||
REQUIRE_THROWS_AS(strings::rformat("hell%y%"), strings::bad_format);
|
||||
REQUIRE_THROWS_AS(strings::rformat("%z%hell"), strings::bad_format);
|
||||
}
|
||||
{
|
||||
str s;
|
||||
REQUIRE(strings::rformat_nothrow(s, "%0", "hello"));
|
||||
REQUIRE(s == "hello");
|
||||
REQUIRE_FALSE(strings::rformat_nothrow(s, "%"));
|
||||
char buf[5] = {0};
|
||||
std::size_t length = 0;
|
||||
REQUIRE(strings::format_nothrow(buf, E2D_COUNTOF(buf), &length, "%0", "hell"));
|
||||
REQUIRE(length == 4);
|
||||
REQUIRE(str(buf) == str("hell"));
|
||||
REQUIRE_FALSE(strings::format_nothrow(buf, E2D_COUNTOF(buf), &length, "%0", "hello"));
|
||||
}
|
||||
{
|
||||
REQUIRE(strings::rformat(str_view("%0"), 42) == "42");
|
||||
REQUIRE(strings::rformat(str_view("%0%1",2), 42) == "42");
|
||||
|
||||
Reference in New Issue
Block a user