rippled
Loading...
Searching...
No Matches
join.h
1//------------------------------------------------------------------------------
2/*
3This file is part of rippled: https://github.com/ripple/rippled
4Copyright (c) 2022 Ripple Labs Inc.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18
19#ifndef JOIN_H_INCLUDED
20#define JOIN_H_INCLUDED
21
22#include <string>
23
24namespace ripple {
25
26template <class Stream, class Iter>
27Stream&
28join(Stream& s, Iter iter, Iter end, std::string const& delimiter)
29{
30 if (iter == end)
31 return s;
32 s << *iter;
33 for (++iter; iter != end; ++iter)
34 s << delimiter << *iter;
35 return s;
36}
37
38template <class Collection>
40{
41public:
42 Collection const& collection;
44
45 explicit CollectionAndDelimiter(Collection const& c, std::string delim)
46 : collection(c), delimiter(std::move(delim))
47 {
48 }
49
50 template <class Stream>
51 friend Stream&
52 operator<<(Stream& s, CollectionAndDelimiter const& cd)
53 {
54 return join(
55 s,
58 cd.delimiter);
59 }
60};
61
62template <class Collection, std::size_t N>
63class CollectionAndDelimiter<Collection[N]>
64{
65public:
66 Collection const* collection;
68
69 explicit CollectionAndDelimiter(Collection const c[N], std::string delim)
70 : collection(c), delimiter(std::move(delim))
71 {
72 }
73
74 template <class Stream>
75 friend Stream&
76 operator<<(Stream& s, CollectionAndDelimiter const& cd)
77 {
78 return join(s, cd.collection, cd.collection + N, cd.delimiter);
79 }
80};
81
82// Specialization for const char* strings
83template <std::size_t N>
85{
86public:
87 char const* collection;
89
90 explicit CollectionAndDelimiter(char const c[N], std::string delim)
91 : collection(c), delimiter(std::move(delim))
92 {
93 }
94
95 template <class Stream>
96 friend Stream&
97 operator<<(Stream& s, CollectionAndDelimiter const& cd)
98 {
99 auto end = cd.collection + N;
100 if (N > 0 && *(end - 1) == '\0')
101 --end;
102 return join(s, cd.collection, end, cd.delimiter);
103 }
104};
105
106} // namespace ripple
107
108#endif
T begin(T... args)
CollectionAndDelimiter(Collection const c[N], std::string delim)
Definition join.h:69
CollectionAndDelimiter(char const c[N], std::string delim)
Definition join.h:90
Collection const & collection
Definition join.h:42
friend Stream & operator<<(Stream &s, CollectionAndDelimiter const &cd)
Definition join.h:52
CollectionAndDelimiter(Collection const &c, std::string delim)
Definition join.h:45
std::string const delimiter
Definition join.h:43
T end(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Stream & join(Stream &s, Iter iter, Iter end, std::string const &delimiter)
Definition join.h:28
STL namespace.