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/libpbd_visibility.h"
27 #include "pbd/whitespace.h"
32 Tokenize string, this should work for standard
33 strings as well as Glib::ustring. This is a bit of a hack,
34 there are much better string tokenizing patterns out there.
35 If strip_whitespace is set to true, tokens will be checked to see
36 that they still have a length after stripping. If no length, they
39 template<typename StringType, typename Iter>
40 /*LIBPBD_API*/ unsigned int
41 tokenize(const StringType& str,
42 const StringType& delims,
44 bool strip_whitespace=false)
46 typename StringType::size_type start_pos = 0;
47 typename StringType::size_type end_pos = 0;
48 unsigned int token_count = 0;
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();
57 if (strip_whitespace) {
58 StringType stripped = str.substr(start_pos, end_pos - start_pos);
59 strip_whitespace_edges (stripped);
60 if (stripped.length()) {
64 *it++ = str.substr(start_pos, end_pos - start_pos);
67 start_pos = str.find_first_not_of(delims, end_pos + 1);
69 } while (start_pos != str.npos);
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()) {
79 *it++ = str.substr(start_pos, str.length() - start_pos);
89 #endif // PBD_TOKENIZER