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 <regex>
10 #include <sstream>
11 #include <string>
12 #include <vector>
13 
14 namespace yarp {
15 namespace conf {
16 namespace string {
17 
26 template <typename ContainerT = std::vector<std::string>>
27 inline ContainerT split(const typename ContainerT::value_type& s, std::basic_regex<typename ContainerT::value_type::value_type> regex)
28 {
29  using string_type = typename ContainerT::value_type;
30  using const_iterator_type = typename string_type::const_iterator;
31  if (s.empty()) {
32  return {};
33  }
34 
35  std::vector<string_type> ret;
36  std::copy(std::regex_token_iterator<const_iterator_type>{s.begin(), s.end(), regex, -1},
37  std::regex_token_iterator<const_iterator_type>{},
38  std::back_inserter(ret));
39  return ret;
40 }
41 
50 template <typename ContainerT = std::vector<std::string>>
51 inline ContainerT split(const typename ContainerT::value_type& s, typename ContainerT::value_type separator)
52 {
53  using string_type = typename ContainerT::value_type;
54  using char_type = typename string_type::value_type;
55 
56  // Escape all characters in the string
57  string_type sep_esc(separator.size()*2, char_type{'\\'});
58  for (size_t i = 0; i < s.size(); ++i) {
59  sep_esc[i*2 + 1] = separator[i];
60  }
61 
62  return split<ContainerT>(s, std::basic_regex<char_type>{string_type{"("} + sep_esc + string_type{")|$"}});
63 }
64 
73 template <typename ContainerT = std::vector<std::string>>
74 inline ContainerT split(const typename ContainerT::value_type& s, typename ContainerT::value_type::value_type separator)
75 {
76  using string_type = typename ContainerT::value_type;
77  using char_type = typename string_type::value_type;
78 
79  return split<ContainerT>(s, std::basic_regex<char_type>{string_type{"\\"} + string_type{separator} + string_type{"|$"}});
80 }
81 
92 template <typename ContainerT = std::vector<std::string>>
93 inline typename ContainerT::value_type join(typename ContainerT::const_iterator begin,
94  typename ContainerT::const_iterator end,
95  const typename ContainerT::value_type& separator = ", ")
96 {
97  using string_type = typename ContainerT::value_type;
98  using char_type = typename string_type::value_type;
99  using traits_type = typename string_type::traits_type;
100  using allocator_type = typename string_type::allocator_type;
101 
102  if (begin == end) {
103  return {};
104  }
105 
106  std::basic_stringstream<char_type, traits_type, allocator_type> s;
107  std::copy(begin, end - 1, std::ostream_iterator<string_type, char_type, traits_type>(s, separator.c_str()));
108  s << *(end - 1);
109  return s.str();
110 }
111 
122 template <typename ContainerT = std::vector<std::string>>
123 inline typename ContainerT::value_type join(typename ContainerT::const_iterator begin,
124  typename ContainerT::const_iterator end,
125  const typename ContainerT::value_type::value_type& separator)
126 {
127  return join<ContainerT>(begin, end, typename ContainerT::value_type{separator});
128 }
129 
130 } // namespace string
131 } // namespace conf
132 } // namespace yarp
133 
134 #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:27
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:93
The main, catch-all namespace for YARP.
Definition: dirs.h:16