rcpputils  master
C++ API providing common utilities and data structures.
split.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019, Open Source Robotics Foundation, Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 //
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 //
13 // * Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 
29 // This file is originally from:
30 // https://github.com/ros/pluginlib/blob/1a4de29fa55173e9b897ca8ff57ebc88c047e0b3/pluginlib/include/pluginlib/impl/split.hpp
31 
36 #ifndef RCPPUTILS__SPLIT_HPP_
37 #define RCPPUTILS__SPLIT_HPP_
38 
39 #include <iterator>
40 #include <sstream>
41 #include <string>
42 #include <vector>
43 
44 namespace rcpputils
45 {
46 
48 
56 template<
57  class InsertIterator,
58  typename std::enable_if<
60  InsertIterator &,
61  decltype(std::declval<InsertIterator>().operator=(std::declval<std::string>()))>::value
62  >::type * = nullptr>
63 void
64 split(const std::string & input, char delim, InsertIterator & it, bool skip_empty = false)
65 {
67  ss.str(input);
68  std::string item;
69  while (std::getline(ss, item, delim)) {
70  if (skip_empty && item == "") {
71  continue;
72  }
73  it = item;
74  }
75 }
76 
78 
87 split(const std::string & input, char delim, bool skip_empty = false)
88 {
90  auto it = std::back_inserter(result);
91  split(input, delim, it, skip_empty);
92  return result;
93 }
94 } // namespace rcpputils
95 
96 #endif // RCPPUTILS__SPLIT_HPP_
std::is_same
std::string
rcpputils
Definition: asserts.hpp:37
std::vector< std::string >
std::back_inserter
T back_inserter(T... args)
std::stringstream
std::enable_if
rcpputils::split
void split(const std::string &input, char delim, InsertIterator &it, bool skip_empty=false)
Split a specified input into tokens using a delimiter and a type erased insert iterator.
Definition: split.hpp:64
std::getline
T getline(T... args)
std::stringstream::str
T str(T... args)