From b2c4489592cc96a431e623fb58002435dff46484 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 6 Oct 2018 19:39:24 +0700 Subject: [PATCH] nothrow variants for strings format --- headers/enduro2d/utils/strings.hpp | 13 ++++++++++- headers/enduro2d/utils/strings.inl | 27 +++++++++++++++++++++++ untests/sources/untests_utils/strings.cpp | 12 ++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/headers/enduro2d/utils/strings.hpp b/headers/enduro2d/utils/strings.hpp index e8ad29ed..877cf7ed 100644 --- a/headers/enduro2d/utils/strings.hpp +++ b/headers/enduro2d/utils/strings.hpp @@ -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); } diff --git a/headers/enduro2d/utils/strings.inl b/headers/enduro2d/utils/strings.inl index ba091195..3f942315 100644 --- a/headers/enduro2d/utils/strings.inl +++ b/headers/enduro2d/utils/strings.inl @@ -385,6 +385,23 @@ namespace e2d { namespace strings std::make_tuple(impl::wrap_arg(std::forward(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)...); + 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)...); + return true; + } catch (...) { + return false; + } + } }} #endif diff --git a/untests/sources/untests_utils/strings.cpp b/untests/sources/untests_utils/strings.cpp index 634397d0..3e1cdb3d 100644 --- a/untests/sources/untests_utils/strings.cpp +++ b/untests/sources/untests_utils/strings.cpp @@ -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");