Skip to content

Commit

Permalink
[break] rename format append overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Apr 10, 2023
1 parent ae31e95 commit 7e62662
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 50 deletions.
8 changes: 8 additions & 0 deletions changelog/current.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### Breaking changes

- [PR#111](https://github.com/biojppm/c4core/pull/111) - Rename formatting overloads accepting `c4::append`:
- `catrs(append_t, ...) -> catrs_append(...)`
- `catseprs(append_t, ...) -> catseprs_append(...)`
- `formatrs(append_t, ...) -> formatrs_append(...)`


### New features

- [PR#105](https://github.com/biojppm/c4core/pull/105): Add macros in `c4/language.hpp` for compile-time flow of exceptions:
Expand Down
53 changes: 16 additions & 37 deletions src/c4/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,16 @@ size_t uncat(csubstr buf, Arg & C4_RESTRICT a, Args & C4_RESTRICT ...more)
namespace detail {

template<class Sep>
inline size_t catsep_more(substr /*buf*/, Sep const& C4_RESTRICT /*sep*/)
C4_ALWAYS_INLINE size_t catsep_more(substr /*buf*/, Sep const& C4_RESTRICT /*sep*/)
{
return 0;
}

template<class Sep, class Arg, class... Args>
size_t catsep_more(substr buf, Sep const& C4_RESTRICT sep, Arg const& C4_RESTRICT a, Args const& C4_RESTRICT ...more)
{
size_t ret = to_chars(buf, sep), num = ret;
size_t ret = to_chars(buf, sep);
size_t num = ret;
buf = buf.len >= ret ? buf.sub(ret) : substr{};
ret = to_chars(buf, a);
num += ret;
Expand All @@ -548,6 +549,7 @@ size_t catsep_more(substr buf, Sep const& C4_RESTRICT sep, Arg const& C4_RESTRIC
return num;
}


template<class Sep>
inline size_t uncatsep_more(csubstr /*buf*/, Sep & /*sep*/)
{
Expand All @@ -557,7 +559,8 @@ inline size_t uncatsep_more(csubstr /*buf*/, Sep & /*sep*/)
template<class Sep, class Arg, class... Args>
size_t uncatsep_more(csubstr buf, Sep & C4_RESTRICT sep, Arg & C4_RESTRICT a, Args & C4_RESTRICT ...more)
{
size_t ret = from_chars_first(buf, &sep), num = ret;
size_t ret = from_chars_first(buf, &sep);
size_t num = ret;
if(C4_UNLIKELY(ret == csubstr::npos))
return csubstr::npos;
buf = buf.len >= ret ? buf.sub(ret) : substr{};
Expand All @@ -575,6 +578,13 @@ size_t uncatsep_more(csubstr buf, Sep & C4_RESTRICT sep, Arg & C4_RESTRICT a, Ar

} // namespace detail

/// @cond dev
template<class Sep>
size_t catsep(substr /*buf*/, Sep const& C4_RESTRICT /*sep*/)
{
return 0;
}
/// @endcond

/** serialize the arguments, concatenating them to the given fixed-size
* buffer, using a separator between each argument.
Expand Down Expand Up @@ -721,17 +731,6 @@ size_t unformat(csubstr buf, csubstr fmt, Arg & C4_RESTRICT a, Args & C4_RESTRIC
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

/** a tag type for marking append to container
* @see c4::catrs() */
struct append_t {};

/** a tag variable
* @see c4::catrs() */
constexpr const append_t append = {};


//-----------------------------------------------------------------------------

/** like c4::cat(), but receives a container, and resizes it as needed to contain
* the result. The container is overwritten. To append to it, use the append
* overload.
Expand Down Expand Up @@ -764,7 +763,7 @@ inline CharOwningContainer catrs(Args const& C4_RESTRICT ...args)
* @see c4::cat()
* @see c4::catrs() */
template<class CharOwningContainer, class... Args>
inline csubstr catrs(append_t, CharOwningContainer * C4_RESTRICT cont, Args const& C4_RESTRICT ...args)
inline csubstr catrs_append(CharOwningContainer * C4_RESTRICT cont, Args const& C4_RESTRICT ...args)
{
const size_t pos = cont->size();
retry:
Expand All @@ -779,16 +778,6 @@ inline csubstr catrs(append_t, CharOwningContainer * C4_RESTRICT cont, Args cons

//-----------------------------------------------------------------------------

/// @cond dev
// terminates the recursion
template<class CharOwningContainer, class Sep, class... Args>
inline void catseprs(CharOwningContainer * C4_RESTRICT, Sep const& C4_RESTRICT)
{
return;
}
/// @end cond


/** like c4::catsep(), but receives a container, and resizes it as needed to contain the result.
* The container is overwritten. To append to the container use the append overload.
* @see c4::catsep() */
Expand All @@ -814,22 +803,12 @@ inline CharOwningContainer catseprs(Sep const& C4_RESTRICT sep, Args const& C4_R
}


/// @cond dev
// terminates the recursion
template<class CharOwningContainer, class Sep, class... Args>
inline csubstr catseprs(append_t, CharOwningContainer * C4_RESTRICT, Sep const& C4_RESTRICT)
{
csubstr s;
return s;
}
/// @endcond

/** like catsep(), but receives a container, and appends the arguments, resizing the
* container as needed to contain the result. The buffer is appended to.
* @return a csubstr of the appended part
* @ingroup formatting_functions */
template<class CharOwningContainer, class Sep, class... Args>
inline csubstr catseprs(append_t, CharOwningContainer * C4_RESTRICT cont, Sep const& C4_RESTRICT sep, Args const& C4_RESTRICT ...args)
inline csubstr catseprs_append(CharOwningContainer * C4_RESTRICT cont, Sep const& C4_RESTRICT sep, Args const& C4_RESTRICT ...args)
{
const size_t pos = cont->size();
retry:
Expand Down Expand Up @@ -875,7 +854,7 @@ inline CharOwningContainer formatrs(csubstr fmt, Args const& C4_RESTRICT ...args
* @return the region newly appended to the original container
* @ingroup formatting_functions */
template<class CharOwningContainer, class... Args>
inline csubstr formatrs(append_t, CharOwningContainer * C4_RESTRICT cont, csubstr fmt, Args const& C4_RESTRICT ...args)
inline csubstr formatrs_append(CharOwningContainer * C4_RESTRICT cont, csubstr fmt, Args const& C4_RESTRICT ...args)
{
const size_t pos = cont->size();
retry:
Expand Down
91 changes: 78 additions & 13 deletions test/test_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,45 @@ TEST_CASE("cat.vars")
csubstr result;
size_t sz;

sz = cat({}, 1, 2, 3, 4);
CHECK_EQ(sz, 4);
sz = cat(buf, 1, 2, 3, 4);
CHECK_EQ(sz, 4);
result = sp.left_of(sz);
CHECK_EQ(result, "1234");

sz = cat({}, 1, 2, 3);
CHECK_EQ(sz, 3);
sz = cat(buf, 1, 2, 3);
CHECK_EQ(sz, 3);
result = sp.left_of(sz);
CHECK_EQ(result, "123");

sz = cat({}, 1, 2);
CHECK_EQ(sz, 2);
sz = cat(buf, 1, 2);
CHECK_EQ(sz, 2);
result = sp.left_of(sz);
CHECK_EQ(result, "12");

sz = cat({}, 1);
CHECK_EQ(sz, 1);
sz = cat(buf, 1);
CHECK_EQ(sz, 1);
result = sp.left_of(sz);
CHECK_EQ(result, "1");

sz = cat({});
CHECK_EQ(sz, 0);
sz = cat(buf);
CHECK_EQ(sz, 0);
result = sp.left_of(sz);
CHECK_EQ(result, "");

sz = cat({}, 1, ' ', 2, ' ', 3, ' ', 4);
CHECK_EQ(sz, 7);
sz = cat(buf, 1, ' ', 2, ' ', 3, ' ', 4);
CHECK_EQ(sz, 7);
result = sp.left_of(sz);
CHECK_EQ(result, "1 2 3 4");
}
Expand Down Expand Up @@ -441,6 +479,13 @@ TEST_CASE("catsep.vars")
csubstr result;
size_t sz;

sz = catsep(buf, ' ');
CHECK_EQ(sz, 0);

sz = catsep(buf, ' ', 1);
CHECK_EQ(sz, 1);
CHECK_EQ(sp.first(1), "1");

sz = catsep(buf, ' ', 1, 2);
CHECK_EQ(sz, 3);
result = sp.left_of(sz);
Expand Down Expand Up @@ -530,6 +575,26 @@ TEST_CASE("format.vars")
csubstr result;
size_t sz;

sz = format(buf, "{} and {} and {} and {}");
CHECK_EQ(sz, strlen("{} and {} and {} and {}"));
result = sp.left_of(sz);
CHECK_EQ(result, "{} and {} and {} and {}");

sz = format(buf, "{} and {} and {} and {}", 1);
CHECK_EQ(sz, strlen("1 and {} and {} and {}"));
result = sp.left_of(sz);
CHECK_EQ(result, "1 and {} and {} and {}");

sz = format(buf, "{} and {} and {} and {}", 1, 2);
CHECK_EQ(sz, strlen("1 and 2 and {} and {}"));
result = sp.left_of(sz);
CHECK_EQ(result, "1 and 2 and {} and {}");

sz = format(buf, "{} and {} and {} and {}", 1, 2, 3);
CHECK_EQ(sz, strlen("1 and 2 and 3 and {}"));
result = sp.left_of(sz);
CHECK_EQ(result, "1 and 2 and 3 and {}");

sz = format(buf, "{} and {} and {} and {}", 1, 2, 3, 4);
CHECK_EQ(sz, strlen("1 and 2 and 3 and 4"));
result = sp.left_of(sz);
Expand Down Expand Up @@ -703,14 +768,14 @@ TEST_CASE("catrs.basic_append")
{
std::vector<char> buf;

catrs(append, &buf);
catrs_append(&buf);
CHECK_EQ(to_csubstr(buf), "");

catrs(append, &buf, 1, 2, 3, 4);
catrs_append(&buf, 1, 2, 3, 4);
CHECK_EQ(to_csubstr(buf), "1234");
catrs(append, &buf, 5, 6, 7, 8);
catrs_append(&buf, 5, 6, 7, 8);
CHECK_EQ(to_csubstr(buf), "12345678");
catrs(append, &buf, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
catrs_append(&buf, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
CHECK_EQ(to_csubstr(buf), "123456789012345678");
}

Expand Down Expand Up @@ -795,21 +860,21 @@ TEST_CASE("catseprs.basic_append")
{
std::vector<char> buf;

auto ret = catseprs(append, &buf, ' ');
auto ret = catseprs_append(&buf, ' ');
CHECK_EQ(to_csubstr(buf), "");
CHECK_EQ(ret, "");

ret = catseprs(append, &buf, ' ', 1, 2, 3, 4);
ret = catseprs_append(&buf, ' ', 1, 2, 3, 4);
CHECK_EQ(to_csubstr(buf), "1 2 3 4");
CHECK_EQ(ret, "1 2 3 4");
ret = catseprs(append, &buf, ' ', 5, 6, 7, 8);
ret = catseprs_append(&buf, ' ', 5, 6, 7, 8);
CHECK_EQ(to_csubstr(buf), "1 2 3 45 6 7 8");
CHECK_EQ(ret, "5 6 7 8");
ret = catseprs(append, &buf, ' ', 9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
ret = catseprs_append(&buf, ' ', 9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
CHECK_EQ(to_csubstr(buf), "1 2 3 45 6 7 89 0 1 2 3 4 5 6 7 8");
CHECK_EQ(ret, "9 0 1 2 3 4 5 6 7 8");

ret = catseprs(append, &buf, ' ');
ret = catseprs_append(&buf, ' ');
CHECK_EQ(to_csubstr(buf), "1 2 3 45 6 7 89 0 1 2 3 4 5 6 7 8");
CHECK_EQ(ret, "");
}
Expand Down Expand Up @@ -876,14 +941,14 @@ TEST_CASE("formatrs.basic_append")
{
std::vector<char> buf;

formatrs(append, &buf, "{} goes with food", "wine");
formatrs_append(&buf, "{} goes with food", "wine");
CHECK_EQ(to_csubstr(buf), "wine goes with food");
formatrs(append, &buf, ", {} goes with heat", "beer");
formatrs_append(&buf, ", {} goes with heat", "beer");
CHECK_EQ(to_csubstr(buf), "wine goes with food, beer goes with heat");
formatrs(append, &buf, ", {} anytime", "coffee");
formatrs_append(&buf, ", {} anytime", "coffee");
CHECK_EQ(to_csubstr(buf), "wine goes with food, beer goes with heat, coffee anytime");

formatrs(append, &buf, ". And water. {} glass of {}cl in the morning clears you up for the day", 1, 40);
formatrs_append(&buf, ". And water. {} glass of {}cl in the morning clears you up for the day", 1, 40);
CHECK_EQ(to_csubstr(buf), "wine goes with food, beer goes with heat, coffee anytime. And water. 1 glass of 40cl in the morning clears you up for the day");
}

Expand Down

0 comments on commit 7e62662

Please sign in to comment.