0
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## usage: [tok, rem] = strtok(str, delim) |
|
18 ## |
|
19 ## Find all characters up to but not including the first character which |
|
20 ## is in the string delim. If rem is requested, it contains the |
|
21 ## remainder of the string, starting at the first deliminator. Leading |
|
22 ## delimiters are ignored. If delim is not specified, space is assumed. |
|
23 |
|
24 ## TODO: check what to do for a null delimiter |
|
25 function [tok, rem] = strtok(str, delim) |
|
26 |
|
27 if nargin<1 || nargin > 2 |
|
28 usage("[tok, rem] = strtok(str, delim)"); |
|
29 endif |
|
30 if nargin < 2 || isempty(delim), delim = " "; endif |
|
31 |
|
32 if isempty(str) |
|
33 tok = rem = ""; |
|
34 elseif length(delim) > 3 |
|
35 start = 1; |
|
36 len = length(str); |
|
37 while start<=len |
|
38 if all(str(start) != delim), break; endif |
|
39 start++; |
|
40 endwhile |
|
41 stop = start; |
|
42 while stop<=len |
|
43 if any(str(stop) == delim), break; endif |
|
44 stop++; |
|
45 endwhile |
|
46 tok = str(start:stop-1); |
|
47 rem = str(stop:len); |
|
48 else |
|
49 if length(delim)==1 |
|
50 idx = find(str == delim); |
|
51 elseif length(delim)==2 |
|
52 idx = find(str == delim(1) | str==delim(2)); |
|
53 else |
|
54 idx = find(str == delim(1) | str==delim(2) | str==delim(3)); |
|
55 endif |
|
56 if isempty(idx) |
|
57 tok = str; |
|
58 rem = ""; |
|
59 else |
|
60 skip = find(idx != 1:length(idx)); # find first non-leading delimiter |
|
61 if isempty(skip) |
|
62 tok = str(idx(length(idx))+1:length(str)); |
|
63 rem = ""; |
|
64 else |
|
65 tok = str(skip(1):idx(skip(1))-1); |
|
66 rem = str(idx(skip(1)):length(str)); |
|
67 endif |
|
68 endif |
|
69 endif |
|
70 |
|
71 endfunction |
|
72 |
|
73 %!demo |
|
74 %! strtok("this is the life") |
|
75 %! % split at the first space, returning "this" |
|
76 |
|
77 %!demo |
|
78 %! s = "14*27+31" |
|
79 %! while 1 |
|
80 %! [t,s] = strtok(s, "+-*/"); |
|
81 %! printf("<%s>", t); |
|
82 %! if isempty(s), break; endif |
|
83 %! printf("<%s>", s(1)); |
|
84 %! endwhile |
|
85 %! printf("\n"); |
|
86 %! % ---------------------------------------------------- |
|
87 %! % Demonstrates processing of an entire string split on |
|
88 %! % a variety of delimiters. Tokens and delimiters are |
|
89 %! % printed one after another in angle brackets. The |
|
90 %! % string is: |
|
91 |
|
92 %!# test the tokens for all cases |
|
93 %!assert(strtok(""), ""); # no string |
|
94 %!assert(strtok("this"), "this"); # no delimiter in string |
|
95 %!assert(strtok("this "), "this"); # delimiter at end |
|
96 %!assert(strtok("this is"), "this"); # delimiter in middle |
|
97 %!assert(strtok(" this"), "this"); # delimiter at start |
|
98 %!assert(strtok(" this "), "this"); # delimiter at start and end |
|
99 %!assert(strtok(" "), ""); # delimiter only |
|
100 |
|
101 %!# test the remainder for all cases |
|
102 %!test [t,r] = strtok(""); assert(r, ""); |
|
103 %!test [t,r] = strtok("this"); assert(r, ""); |
|
104 %!test [t,r] = strtok("this "); assert(r, " "); |
|
105 %!test [t,r] = strtok("this is"); assert(r, " is"); |
|
106 %!test [t,r] = strtok(" this"); assert(r, ""); |
|
107 %!test [t,r] = strtok(" this "); assert(r, " "); |
|
108 %!test [t,r] = strtok(" "); assert(r, ""); |
|
109 |
|
110 %!# simple check with 2 and 3 delimeters |
|
111 %!assert(strtok("this is", "i "), "th"); |
|
112 %!assert(strtok("this is", "ij "), "th"); |
|
113 |
|
114 %!# test all cases for 4 delimiters since a different |
|
115 %!# algorithm is used when more than 3 delimiters |
|
116 %!assert(strtok("","jkl "), ""); |
|
117 %!assert(strtok("this","jkl "), "this"); |
|
118 %!assert(strtok("this ","jkl "), "this"); |
|
119 %!assert(strtok("this is","jkl "), "this"); |
|
120 %!assert(strtok(" this","jkl "), "this"); |
|
121 %!assert(strtok(" this ","jkl "), "this"); |
|
122 %!assert(strtok(" ","jkl "), ""); |