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