merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[ardour.git] / libs / pbd / strsplit.cc
1 #include <pbd/strsplit.h>
2
3 using namespace std;
4 using namespace Glib;
5
6 void
7 split (string str, vector<string>& result, char splitchar)
8 {       
9         string::size_type pos;
10         string remaining;
11         string::size_type len = str.length();
12         int cnt;
13
14         cnt = 0;
15
16         if (str.empty()) {
17                 return;
18         }
19
20         for (string::size_type n = 0; n < len; ++n) {
21                 if (str[n] == splitchar) {
22                         cnt++;
23                 }
24         }
25
26         if (cnt == 0) {
27                 result.push_back (str);
28                 return;
29         }
30
31         remaining = str;
32
33         while ((pos = remaining.find_first_of (':')) != string::npos) {
34                 result.push_back (remaining.substr (0, pos));
35                 remaining = remaining.substr (pos+1);
36         }
37
38         if (remaining.length()) {
39
40                 result.push_back (remaining);
41         }
42 }
43
44 void
45 split (ustring str, vector<ustring>& result, char splitchar)
46 {       
47         ustring::size_type pos;
48         ustring remaining;
49         ustring::size_type len = str.length();
50         int cnt;
51
52         cnt = 0;
53
54         if (str.empty()) {
55                 return;
56         }
57
58         for (ustring::size_type n = 0; n < len; ++n) {
59                 if (str[n] == gunichar(splitchar)) {
60                         cnt++;
61                 }
62         }
63
64         if (cnt == 0) {
65                 result.push_back (str);
66                 return;
67         }
68
69         remaining = str;
70
71         while ((pos = remaining.find_first_of (':')) != ustring::npos) {
72                 result.push_back (remaining.substr (0, pos));
73                 remaining = remaining.substr (pos+1);
74         }
75
76         if (remaining.length()) {
77
78                 result.push_back (remaining);
79         }
80 }