Mercurial > hg > octave-nkf
comparison scripts/strings/strtok.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | fa94d6a93d45 |
comparison
equal
deleted
inserted
replaced
11586:12df7854fa7c | 11587:c792872f8942 |
---|---|
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {[@var{tok}, @var{rem}] =} strtok (@var{str}, @var{delim}) | 20 ## @deftypefn {Function File} {[@var{tok}, @var{rem}] =} strtok (@var{str}, @var{delim}) |
21 ## | 21 ## |
22 ## Find all characters up to but not including the first character which | 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 | 23 ## is in the string delim. If @var{rem} is requested, it contains the |
24 ## remainder of the string, starting at the first delimiter. Leading | 24 ## remainder of the string, starting at the first delimiter. Leading |
25 ## delimiters are ignored. If @var{delim} is not specified, space is | 25 ## delimiters are ignored. If @var{delim} is not specified, space is |
26 ## assumed. For example: | 26 ## assumed. For example: |
27 ## | 27 ## |
28 ## @example | 28 ## @example |
29 ## @group | 29 ## @group |
30 ## strtok ("this is the life") | 30 ## strtok ("this is the life") |
31 ## @result{} "this" | 31 ## @result{} "this" |
46 if (nargin<1 || nargin > 2) | 46 if (nargin<1 || nargin > 2) |
47 print_usage (); | 47 print_usage (); |
48 endif | 48 endif |
49 | 49 |
50 if (nargin < 2 || isempty (delim)) | 50 if (nargin < 2 || isempty (delim)) |
51 delim = "\t\n\v\f\r "; | 51 delim = "\t\n\v\f\r "; |
52 endif | 52 endif |
53 | 53 |
54 if (isempty (str)) | 54 if (isempty (str)) |
55 tok = rem = ""; | 55 tok = rem = ""; |
56 elseif (length (delim) > 3) | 56 elseif (length (delim) > 3) |
57 start = 1; | 57 start = 1; |
58 len = length (str); | 58 len = length (str); |
59 while (start <= len) | 59 while (start <= len) |
60 if (all (str(start) != delim)) | 60 if (all (str(start) != delim)) |
61 break; | 61 break; |
62 endif | 62 endif |
63 start++; | 63 start++; |
64 endwhile | 64 endwhile |
65 stop = start; | 65 stop = start; |
66 while (stop <= len) | 66 while (stop <= len) |
67 if (any (str(stop) == delim)) | 67 if (any (str(stop) == delim)) |
68 break; | 68 break; |
69 endif | 69 endif |
70 stop++; | 70 stop++; |
71 endwhile | 71 endwhile |
72 tok = str(start:stop-1); | 72 tok = str(start:stop-1); |
73 rem = str(stop:len); | 73 rem = str(stop:len); |
110 %! printf("<%s>", s(1)); | 110 %! printf("<%s>", s(1)); |
111 %! endwhile | 111 %! endwhile |
112 %! printf("\n"); | 112 %! printf("\n"); |
113 %! % ---------------------------------------------------- | 113 %! % ---------------------------------------------------- |
114 %! % Demonstrates processing of an entire string split on | 114 %! % Demonstrates processing of an entire string split on |
115 %! % a variety of delimiters. Tokens and delimiters are | 115 %! % a variety of delimiters. Tokens and delimiters are |
116 %! % printed one after another in angle brackets. The | 116 %! % printed one after another in angle brackets. The |
117 %! % string is: | 117 %! % string is: |
118 | 118 |
119 %!# test the tokens for all cases | 119 %!# test the tokens for all cases |
120 %!assert(strtok(""), ""); # no string | 120 %!assert(strtok(""), ""); # no string |
136 | 136 |
137 %!# simple check with 2 and 3 delimeters | 137 %!# simple check with 2 and 3 delimeters |
138 %!assert(strtok("this is", "i "), "th"); | 138 %!assert(strtok("this is", "i "), "th"); |
139 %!assert(strtok("this is", "ij "), "th"); | 139 %!assert(strtok("this is", "ij "), "th"); |
140 | 140 |
141 %!# test all cases for 4 delimiters since a different | 141 %!# test all cases for 4 delimiters since a different |
142 %!# algorithm is used when more than 3 delimiters | 142 %!# algorithm is used when more than 3 delimiters |
143 %!assert(strtok("","jkl "), ""); | 143 %!assert(strtok("","jkl "), ""); |
144 %!assert(strtok("this","jkl "), "this"); | 144 %!assert(strtok("this","jkl "), "this"); |
145 %!assert(strtok("this ","jkl "), "this"); | 145 %!assert(strtok("this ","jkl "), "this"); |
146 %!assert(strtok("this is","jkl "), "this"); | 146 %!assert(strtok("this is","jkl "), "this"); |