libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
join.hpp
1#pragma once
2
3#include "iterable.hpp"
4#include "string.hpp"
5#include <vector>
6
7namespace z {
8namespace core {
19template <typename T, encoding E>
20string<E> join(const iterable<T> &list, const string<E> &delim) noexcept {
22
23 for (auto &item : list) {
24 if (result.length()) {
25 result += delim;
26 }
27 result += item;
28 }
29
30 return result;
31}
32
43template <typename T>
44zstring join(const iterable<T> &list, const zstring &delim) noexcept {
46
47 for (auto &item : list) {
48 if (result.length()) {
49 result += delim;
50 }
51 result += item;
52 }
53
54 return result;
55}
56
69zstring join(const std::vector<std::string> &list, const zstring &delim) noexcept;
70
81template <typename T, encoding E>
82string<E> joinDeref(const iterable<T> &list, const string<E> &delim) noexcept {
83 static_assert(std::is_pointer<T>::value, "Array template must be of pointer type.");
84
86
87 for (auto &item : list) {
88 if (result.length()) {
89 result += delim;
90 }
91 result += *item;
92 }
93
94 return result;
95}
96} // namespace core
97} // namespace z
A wrapper for std::vector.
Definition array.hpp:72
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1011
A template class for character strings.
Definition string.hpp:62
string< E > joinDeref(const iterable< T > &list, const string< E > &delim) noexcept
Dereference elements in an array and concatenate them into a string, separated by a delimiter.
Definition join.hpp:82
string< E > join(const iterable< T > &list, const string< E > &delim) noexcept
Concatenate elements in an array into a string, separated by a delimiter.
Definition join.hpp:20