Point Cloud Library (PCL)  1.14.1-dev
split.h
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2014-, Open Perception Inc.
6 *
7 * All rights reserved
8 */
9 
10 #pragma once
11 #include <string>
12 
13 namespace pcl {
14 
15 /** \brief Lightweight tokenization function
16  * This function can be used as a boost::split substitute. When benchmarked against
17  * boost, this function will create much less allocations and hence it is much better
18  * suited for quick line tokenization.
19  *
20  * Cool thing is this function will work with SequenceSequenceT =
21  * std::vector<std::string> and std::vector<std::string_view>
22  */
23 template <typename SequenceSequenceT>
24 void
25 split(SequenceSequenceT& result, std::string const& in, const char* const delimiters)
26 {
27  using StringSizeT = std::string::size_type;
28 
29  const auto len = in.length();
30  StringSizeT token_start = 0;
31 
32  result.clear();
33  while (token_start < len) {
34  // eat leading whitespace
35  token_start = in.find_first_not_of(delimiters, token_start);
36  if (token_start == std::string::npos) {
37  return; // nothing left but white space
38  }
39 
40  // find the end of the token
41  const auto token_end = in.find_first_of(delimiters, token_start);
42 
43  // push token
44  if (token_end == std::string::npos) {
45  result.emplace_back(in.data() + token_start, len - token_start);
46  return;
47  }
48  else {
49  result.emplace_back(in.data() + token_start, token_end - token_start);
50  }
51 
52  // set up for next loop
53  token_start = token_end + 1;
54  }
55 }
56 } // namespace pcl
void split(SequenceSequenceT &result, std::string const &in, const char *const delimiters)
Lightweight tokenization function This function can be used as a boost::split substitute.
Definition: split.h:25