YARP
Yet Another Robot Platform
string.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef YARP_CONF_STRING_H
7#define YARP_CONF_STRING_H
8
9#include <iterator>
10#include <regex>
11#include <sstream>
12#include <string>
13#include <vector>
14
16
25template <typename ContainerT = std::vector<std::string>>
26inline ContainerT split(const typename ContainerT::value_type& s, std::basic_regex<typename ContainerT::value_type::value_type> regex)
27{
28 using string_type = typename ContainerT::value_type;
29 using const_iterator_type = typename string_type::const_iterator;
30 if (s.empty()) {
31 return {};
32 }
33
34 std::vector<string_type> ret;
35 std::copy(std::regex_token_iterator<const_iterator_type>{s.begin(), s.end(), regex, -1},
36 std::regex_token_iterator<const_iterator_type>{},
37 std::back_inserter(ret));
38 return ret;
39}
40
49template <typename ContainerT = std::vector<std::string>>
50inline ContainerT split(const typename ContainerT::value_type& s, typename ContainerT::value_type separator)
51{
52 using string_type = typename ContainerT::value_type;
53 using char_type = typename string_type::value_type;
54
55 // Escape all characters in the string
56 string_type sep_esc(separator.size()*2, char_type{'\\'});
57 for (size_t i = 0; i < s.size(); ++i) {
58 sep_esc[i*2 + 1] = separator[i];
59 }
60
61 return split<ContainerT>(s, std::basic_regex<char_type>{string_type{"("} + sep_esc + string_type{")|$"}});
62}
63
72template <typename ContainerT = std::vector<std::string>>
73inline ContainerT split(const typename ContainerT::value_type& s, typename ContainerT::value_type::value_type separator)
74{
75 using string_type = typename ContainerT::value_type;
76 using char_type = typename string_type::value_type;
77
78 return split<ContainerT>(s, std::basic_regex<char_type>{string_type{"\\"} + string_type{separator} + string_type{"|$"}});
79}
80
91template <typename ContainerT = std::vector<std::string>>
92inline typename ContainerT::value_type join(typename ContainerT::const_iterator begin,
93 typename ContainerT::const_iterator end,
94 const typename ContainerT::value_type& separator = ", ")
95{
96 using string_type = typename ContainerT::value_type;
97 using char_type = typename string_type::value_type;
98 using traits_type = typename string_type::traits_type;
99 using allocator_type = typename string_type::allocator_type;
100
101 if (begin == end) {
102 return {};
103 }
104
105 std::basic_stringstream<char_type, traits_type, allocator_type> s;
106 std::copy(begin, end - 1, std::ostream_iterator<string_type, char_type, traits_type>(s, separator.c_str()));
107 s << *(end - 1);
108 return s.str();
109}
110
121template <typename ContainerT = std::vector<std::string>>
122inline typename ContainerT::value_type join(typename ContainerT::const_iterator begin,
123 typename ContainerT::const_iterator end,
124 const typename ContainerT::value_type::value_type& separator)
125{
126 return join<ContainerT>(begin, end, typename ContainerT::value_type{separator});
127}
128
129} // namespace yarp::conf::string
130
131#endif // YARP_CONF_STRING_H
bool ret
ContainerT split(const typename ContainerT::value_type &s, std::basic_regex< typename ContainerT::value_type::value_type > regex)
Utility to split a string by a separator, into a vector of strings.
Definition: string.h:26
ContainerT::value_type join(typename ContainerT::const_iterator begin, typename ContainerT::const_iterator end, const typename ContainerT::value_type &separator=", ")
Utility to join the elements in a container to a single string separated by a separator.
Definition: string.h:92