2 Copyright (C) 2000-2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <pbd/whitespace.h>
31 Tokenize string, this should work for standard
32 strings as well as Glib::ustring. This is a bit of a hack,
33 there are much better string tokenizing patterns out there.
34 If strip_whitespace is set to true, tokens will be checked to see
35 that they still have a length after stripping. If no length, they
38 template<typename StringType, typename Iter>
40 tokenize(const StringType& str,
41 const StringType& delims,
43 bool strip_whitespace=false)
45 typename StringType::size_type start_pos = 0;
46 typename StringType::size_type end_pos = 0;
47 unsigned int token_count = 0;
50 start_pos = str.find_first_not_of(delims, start_pos);
51 end_pos = str.find_first_of(delims, start_pos);
52 if (start_pos != end_pos) {
53 if (end_pos == str.npos) {
54 end_pos = str.length();
56 if (strip_whitespace) {
57 StringType stripped = str.substr(start_pos, end_pos - start_pos);
58 strip_whitespace_edges (stripped);
59 if (stripped.length()) {
63 *it++ = str.substr(start_pos, end_pos - start_pos);
66 start_pos = str.find_first_not_of(delims, end_pos + 1);
68 } while (start_pos != str.npos);
70 if (start_pos != str.npos) {
71 if (strip_whitespace) {
72 StringType stripped = str.substr(start_pos, str.length() - start_pos);
73 strip_whitespace_edges (stripped);
74 if (stripped.length()) {
78 *it++ = str.substr(start_pos, str.length() - start_pos);
88 #endif // PBD_TOKENIZER