-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FOLD] Write a collection to a stream with delimiters
- Loading branch information
Showing
4 changed files
with
198 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of rippled: https://github.com/ripple/rippled | ||
Copyright (c) 2022 Ripple Labs Inc. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#ifndef JOIN_H_INCLUDED | ||
#define JOIN_H_INCLUDED | ||
|
||
namespace ripple { | ||
|
||
template <class Stream, class Iter, class Str> | ||
Stream& | ||
join(Stream& s, Iter iter, Iter end, Str const& delimiter) | ||
{ | ||
if (iter == end) | ||
return s; | ||
s << *iter; | ||
for (++iter; iter != end; ++iter) | ||
s << delimiter << *iter; | ||
return s; | ||
} | ||
|
||
template <class Collection, class Str> | ||
class CollectionAndDelimiter | ||
{ | ||
public: | ||
Collection const& collection; | ||
Str const& delimiter; | ||
|
||
explicit CollectionAndDelimiter(Collection const& c, Str const& delim) | ||
: collection(c), delimiter(delim) | ||
{ | ||
} | ||
|
||
template <class Stream> | ||
friend Stream& | ||
operator<<(Stream& s, CollectionAndDelimiter const& cd) | ||
{ | ||
return join( | ||
s, | ||
std::begin(cd.collection), | ||
std::end(cd.collection), | ||
cd.delimiter); | ||
} | ||
}; | ||
|
||
template <class Collection, size_t N, class Str> | ||
class CollectionAndDelimiter<Collection[N], Str> | ||
{ | ||
public: | ||
Collection const* collection; | ||
Str const& delimiter; | ||
|
||
explicit CollectionAndDelimiter(Collection const c[N], Str const& delim) | ||
: collection(c), delimiter(delim) | ||
{ | ||
} | ||
|
||
template <class Stream> | ||
friend Stream& | ||
operator<<(Stream& s, CollectionAndDelimiter const& cd) | ||
{ | ||
return join(s, cd.collection, cd.collection + N, cd.delimiter); | ||
} | ||
}; | ||
|
||
// Specialization for const char* strings | ||
template <size_t N, class Str> | ||
class CollectionAndDelimiter<char[N], Str> | ||
{ | ||
public: | ||
char const* collection; | ||
Str const& delimiter; | ||
|
||
explicit CollectionAndDelimiter(char const c[N], Str const& delim) | ||
: collection(c), delimiter(delim) | ||
{ | ||
} | ||
|
||
template <class Stream> | ||
friend Stream& | ||
operator<<(Stream& s, CollectionAndDelimiter const& cd) | ||
{ | ||
auto end = cd.collection + N; | ||
if (N > 0 && *(end - 1) == '\0') | ||
--end; | ||
return join(s, cd.collection, end, cd.delimiter); | ||
} | ||
}; | ||
|
||
} // namespace ripple | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of rippled: https://github.com/ripple/rippled | ||
Copyright (c) 2022 Ripple Labs Inc. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
//============================================================================== | ||
|
||
#include <ripple/basics/join.h> | ||
|
||
#include <ripple/beast/unit_test.h> | ||
#include <test/jtx/Account.h> | ||
|
||
namespace ripple { | ||
namespace test { | ||
|
||
struct join_test : beast::unit_test::suite | ||
{ | ||
void | ||
run() override | ||
{ | ||
auto test = [this](auto collectionanddelimiter, std::string expected) { | ||
std::stringstream ss; | ||
// Put something else in the buffer before and after to ensure that | ||
// the << operator returns the stream correctly. | ||
ss << "(" << collectionanddelimiter << ")"; | ||
auto const str = ss.str(); | ||
BEAST_EXPECT(str.substr(1, str.length() - 2) == expected); | ||
BEAST_EXPECT(str.front() == '('); | ||
BEAST_EXPECT(str.back() == ')'); | ||
}; | ||
|
||
// C++ array | ||
test( | ||
CollectionAndDelimiter(std::array<int, 4>{2, -1, 5, 10}, '/'), | ||
"2/-1/5/10"); | ||
// Empty C++ array edge case | ||
test(CollectionAndDelimiter(std::array<int, 0>{}, uint256{5}), ""); | ||
{ | ||
// C-style array | ||
char letters[4]{'w', 'a', 's', 'd'}; | ||
test(CollectionAndDelimiter(letters, 0), "w0a0s0d"); | ||
} | ||
{ | ||
// Auto sized C-style array | ||
std::string words[]{"one", "two", "three", "four"}; | ||
test(CollectionAndDelimiter(words, "\n"), "one\ntwo\nthree\nfour"); | ||
} | ||
// Initializer list | ||
test( | ||
CollectionAndDelimiter(std::initializer_list<size_t>{19, 25}, "+"), | ||
"19+25"); | ||
{ | ||
using namespace jtx; | ||
// vector | ||
test(CollectionAndDelimiter(std::vector<int>{0, 42}, 99), "09942"); | ||
// vector with one item edge case | ||
test( | ||
CollectionAndDelimiter( | ||
std::vector<Account>{Account::master}, "xxx"), | ||
Account::master.human()); | ||
} | ||
// C-style string | ||
test(CollectionAndDelimiter("string", " "), "s t r i n g"); | ||
// Empty C-style string edge case | ||
test(CollectionAndDelimiter("", "*"), ""); | ||
// C-style string | ||
test(CollectionAndDelimiter(std::string{"string"}, " "), "s t r i n g"); | ||
// Empty C-style string edge case | ||
test(CollectionAndDelimiter(std::string{""}, "*"), ""); | ||
} | ||
}; // namespace test | ||
|
||
BEAST_DEFINE_TESTSUITE(join, ripple_basics, ripple); | ||
|
||
} // namespace test | ||
} // namespace ripple |