Ardour  9.0-pre0-350-gf17a656217
tokenizer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2007 Taybin Rutkin <taybin@taybin.com>
3  * Copyright (C) 2007-2015 Paul Davis <paul@linuxaudiosystems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef PBD_TOKENIZER
21 #define PBD_TOKENIZER
22 
23 #include <iterator>
24 #include <string>
25 
26 #include "pbd/libpbd_visibility.h"
27 #include "pbd/whitespace.h"
28 
29 namespace PBD {
30 
39 template<typename StringType, typename Iter>
40 /*LIBPBD_API*/ unsigned int
41 tokenize(const StringType& str,
42  const StringType& delims,
43  Iter it,
44  bool strip_whitespace=false)
45 {
46  typename StringType::size_type start_pos = 0;
47  typename StringType::size_type end_pos = 0;
48  unsigned int token_count = 0;
49 
50  do {
51  start_pos = str.find_first_not_of(delims, start_pos);
52  end_pos = str.find_first_of(delims, start_pos);
53  if (start_pos != end_pos) {
54  if (end_pos == str.npos) {
55  end_pos = str.length();
56  }
57  if (strip_whitespace) {
58  StringType stripped = str.substr(start_pos, end_pos - start_pos);
59  strip_whitespace_edges (stripped);
60  if (stripped.length()) {
61  *it++ = stripped;
62  }
63  } else {
64  *it++ = str.substr(start_pos, end_pos - start_pos);
65  }
66  ++token_count;
67  start_pos = str.find_first_not_of(delims, end_pos + 1);
68  }
69  } while (start_pos != str.npos);
70 
71  if (start_pos != str.npos) {
72  if (strip_whitespace) {
73  StringType stripped = str.substr(start_pos, str.length() - start_pos);
74  strip_whitespace_edges (stripped);
75  if (stripped.length()) {
76  *it++ = stripped;
77  }
78  } else {
79  *it++ = str.substr(start_pos, str.length() - start_pos);
80  }
81  ++token_count;
82  }
83 
84  return token_count;
85 }
86 
87 } // namespace PBD
88 
89 #endif // PBD_TOKENIZER
Definition: axis_view.h:42
unsigned int tokenize(const StringType &str, const StringType &delims, Iter it, bool strip_whitespace=false)
Definition: tokenizer.h:41
void strip_whitespace_edges(std::string &str)