rippled
join.h
1 //------------------------------------------------------------------------------
2 /*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2022 Ripple Labs Inc.
5 
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9 
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR 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 
24 namespace ripple {
25 
26 template <class Stream, class Iter>
27 Stream&
28 join(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 
38 template <class Collection>
40 {
41 public:
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,
57  std::end(cd.collection),
58  cd.delimiter);
59  }
60 };
61 
62 template <class Collection, std::size_t N>
63 class CollectionAndDelimiter<Collection[N]>
64 {
65 public:
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
83 template <std::size_t N>
84 class CollectionAndDelimiter<char[N]>
85 {
86 public:
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
std::string
STL class.
ripple::join
Stream & join(Stream &s, Iter iter, Iter end, std::string const &delimiter)
Definition: join.h:28
ripple::CollectionAndDelimiter< char[N]>::delimiter
const std::string delimiter
Definition: join.h:88
ripple::CollectionAndDelimiter< Collection[N]>::CollectionAndDelimiter
CollectionAndDelimiter(Collection const c[N], std::string delim)
Definition: join.h:69
ripple::CollectionAndDelimiter< char[N]>::collection
char const * collection
Definition: join.h:87
ripple::CollectionAndDelimiter
Definition: join.h:39
ripple::CollectionAndDelimiter::collection
Collection const & collection
Definition: join.h:42
ripple::CollectionAndDelimiter::CollectionAndDelimiter
CollectionAndDelimiter(Collection const &c, std::string delim)
Definition: join.h:45
ripple::CollectionAndDelimiter< Collection[N]>::delimiter
const std::string delimiter
Definition: join.h:67
ripple::CollectionAndDelimiter::delimiter
const std::string delimiter
Definition: join.h:43
ripple::CollectionAndDelimiter< char[N]>::CollectionAndDelimiter
CollectionAndDelimiter(char const c[N], std::string delim)
Definition: join.h:90
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::CollectionAndDelimiter::operator<<
friend Stream & operator<<(Stream &s, CollectionAndDelimiter const &cd)
Definition: join.h:52
std::begin
T begin(T... args)
std
STL namespace.
ripple::CollectionAndDelimiter< Collection[N]>::collection
Collection const * collection
Definition: join.h:66
std::end
T end(T... args)
string