YARP
Yet Another Robot Platform
string.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_CONF_STRING_H
10 #define YARP_CONF_STRING_H
11 
12 #include <regex>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 
17 namespace yarp {
18 namespace conf {
19 namespace string {
20 
29 template <typename ContainerT = std::vector<std::string>>
30 inline ContainerT split(const typename ContainerT::value_type& s, std::basic_regex<typename ContainerT::value_type::value_type> regex)
31 {
32  using string_type = typename ContainerT::value_type;
33  using const_iterator_type = typename string_type::const_iterator;
34  if (s.empty()) {
35  return {};
36  }
37 
38  std::vector<string_type> ret;
39  std::copy(std::regex_token_iterator<const_iterator_type>{s.begin(), s.end(), regex, -1},
40  std::regex_token_iterator<const_iterator_type>{},
41  std::back_inserter(ret));
42  return ret;
43 }
44 
53 template <typename ContainerT = std::vector<std::string>>
54 inline ContainerT split(const typename ContainerT::value_type& s, typename ContainerT::value_type separator)
55 {
56  using string_type = typename ContainerT::value_type;
57  using char_type = typename string_type::value_type;
58 
59  // Escape all characters in the string
60  string_type sep_esc(separator.size()*2, char_type{'\\'});
61  for (size_t i = 0; i < s.size(); ++i) {
62  sep_esc[i*2 + 1] = separator[i];
63  }
64 
65  return split<ContainerT>(s, std::basic_regex<char_type>{string_type{"("} + sep_esc + string_type{")|$"}});
66 }
67 
76 template <typename ContainerT = std::vector<std::string>>
77 inline ContainerT split(const typename ContainerT::value_type& s, typename ContainerT::value_type::value_type separator)
78 {
79  using string_type = typename ContainerT::value_type;
80  using char_type = typename string_type::value_type;
81 
82  return split<ContainerT>(s, std::basic_regex<char_type>{string_type{"\\"} + string_type{separator} + string_type{"|$"}});
83 }
84 
95 template <typename ContainerT = std::vector<std::string>>
96 inline typename ContainerT::value_type join(typename ContainerT::const_iterator begin,
97  typename ContainerT::const_iterator end,
98  const typename ContainerT::value_type& separator = ", ")
99 {
100  using string_type = typename ContainerT::value_type;
101  using char_type = typename string_type::value_type;
102  using traits_type = typename string_type::traits_type;
103  using allocator_type = typename string_type::allocator_type;
104 
105  if (begin == end) {
106  return {};
107  }
108 
109  std::basic_stringstream<char_type, traits_type, allocator_type> s;
110  std::copy(begin, end - 1, std::ostream_iterator<string_type, char_type, traits_type>(s, separator.c_str()));
111  s << *(end - 1);
112  return s.str();
113 }
114 
125 template <typename ContainerT = std::vector<std::string>>
126 inline typename ContainerT::value_type join(typename ContainerT::const_iterator begin,
127  typename ContainerT::const_iterator end,
128  const typename ContainerT::value_type::value_type& separator)
129 {
130  return join<ContainerT>(begin, end, typename ContainerT::value_type{separator});
131 }
132 
133 } // namespace string
134 } // namespace conf
135 } // namespace yarp
136 
137 #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:30
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:96
The main, catch-all namespace for YARP.
Definition: environment.h:25