Mercurial > octave
annotate scripts/strings/strtok.m @ 28240:2fb684dc2ec2
axis.m: Implement "fill" option for Matlab compatibility.
* axis.m: Document that "fill" is a synonym for "normal". Place "vis3d" option
in documentation table for modes which affect aspect ratio. Add
strcmpi (opt, "fill") to decode opt and executed the same behavior as "normal".
author | Rik <rik@octave.org> |
---|---|
date | Fri, 24 Apr 2020 13:16:09 -0700 |
parents | c20b7290c778 |
children | 28de41192f3c 0a5b15007766 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 ## Copyright (C) 2000-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5827 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5827 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
5827 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
5827 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5827 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
27 ## @deftypefn {} {[@var{tok}, @var{rem}] =} strtok (@var{str}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
28 ## @deftypefnx {} {[@var{tok}, @var{rem}] =} strtok (@var{str}, @var{delim}) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
29 ## |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
30 ## Find all characters in the string @var{str} up to, but not including, the |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## first character which is in the string @var{delim}. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## @var{str} may also be a cell array of strings in which case the function |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## executes on every individual string and returns a cell array of tokens and |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## remainders. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
36 ## |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
37 ## Leading delimiters are ignored. If @var{delim} is not specified, |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
38 ## whitespace is assumed. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
39 ## |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
40 ## If @var{rem} is requested, it contains the remainder of the string, starting |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
41 ## at the first delimiter. |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
42 ## |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
43 ## Examples: |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
44 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
45 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
46 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
47 ## strtok ("this is the life") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
48 ## @result{} "this" |
5827 | 49 ## |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
50 ## [tok, rem] = strtok ("14*27+31", "+-*/") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
51 ## @result{} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
52 ## tok = 14 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
53 ## rem = *27+31 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
54 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8202
diff
changeset
|
55 ## @end example |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 ## @seealso{index, strsplit, strchr, isspace} |
5827 | 57 ## @end deftypefn |
58 | |
59 function [tok, rem] = strtok (str, delim) | |
60 | |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 if (nargin < 1 || nargin > 2) |
5827 | 62 print_usage (); |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 elseif (! (ischar (str) || iscellstr (str))) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 error ("strtok: STR must be a string or cell array of strings."); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
65 elseif (ischar (str) && ! isvector (str) &&! isempty (str)) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 error ("strtok: STR cannot be a 2-D character array."); |
5827 | 67 endif |
68 | |
69 if (nargin < 2 || isempty (delim)) | |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 ws_delim = true; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
71 else |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
72 ws_delim = false; |
5827 | 73 endif |
74 | |
75 if (isempty (str)) | |
76 tok = rem = ""; | |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 elseif (ischar (str)) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 if (ws_delim) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
79 idx = isspace (str); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
80 elseif (length (delim) <= 7) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
81 ## Build index of delimiters incrementally for low N. |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
82 idx = str == delim(1); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
83 for i = 2:length (delim) |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
84 idx |= str == delim(i); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
85 endfor |
5827 | 86 else |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 ## Index the str into a mask of valid values. Faster for large N. |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 f = false (256, 1); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 ## This is slower than it could be because of the +1 issue. |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
90 f(uint8 (delim)+1) = true; |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 ## Default goes via double -- unnecessarily long. |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 si = uint32 (str); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 ## in-place is faster than str+1 |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 ++si; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
95 idx = f(si); |
5827 | 96 endif |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
97 |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
98 idx_dlim = find (idx, 1); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
99 idx_nodlim = find (! idx, 1); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 if (isempty (idx_dlim)) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 ## No delimiter. Return whole string. |
5827 | 102 tok = str; |
103 rem = ""; | |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 elseif (idx_dlim > idx_nodlim) |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
105 ## Normal case. No leading delimiters and at least 1 delimiter in STR. |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 tok = str(1:idx_dlim-1); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
107 rem = str(idx_dlim:end); |
5827 | 108 else |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
109 ## Leading delimiter found. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
110 idx_dlim = find (idx(idx_nodlim+1:end), 1); |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
111 if (isempty (idx_dlim)) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
112 ## No further delimiters. Return STR stripped of delimiter prefix. |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
113 tok = str(idx_nodlim:end); |
10549 | 114 rem = ""; |
5827 | 115 else |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
116 ## Strip delimiter prefix. Return STR up to 1st delimiter |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
117 tok = str(idx_nodlim:(idx_dlim + idx_nodlim -1)); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
118 rem = str((idx_dlim + idx_nodlim):end); |
5827 | 119 endif |
120 endif | |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
121 else # Cell array of strings |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
122 if (ws_delim) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
123 delim = '\s'; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
124 endif |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
125 ptn = [ '^[' delim ']*','([^' delim ']+)','([' delim '].*)$' ]; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
126 matches = regexp (str, ptn, "tokens"); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
127 eidx = cellfun ("isempty", matches); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
128 midx = ! eidx; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
129 tok = cell (size (str)); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
130 tok(eidx) = regexprep (str(eidx), [ '^[' delim ']+' ], ''); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
131 ## Unwrap doubly nested cell array from regexp |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
132 tmp = [matches{midx}]; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
133 if (! isempty (tmp)) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
134 tmp = [tmp{:}]; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
135 endif |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
136 tok(midx) = tmp(1:2:end); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
137 if (isargout (2)) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
138 rem = cell (size (str)); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
139 rem(eidx) = {""}; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
140 rem(midx) = tmp(2:2:end); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
141 endif |
5827 | 142 endif |
143 | |
144 endfunction | |
145 | |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
146 |
5827 | 147 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
148 %! strtok ("this is the life") |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21317
diff
changeset
|
149 %! # split at the first space, returning "this" |
5827 | 150 |
151 %!demo | |
152 %! s = "14*27+31" | |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
153 %! while (1) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
154 %! [t, s] = strtok (s, "+-*/"); |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
155 %! printf ("<%s>", t); |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
156 %! if (isempty (s)) |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
157 %! break; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
158 %! endif |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
159 %! printf ("<%s>", s(1)); |
5827 | 160 %! endwhile |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
161 %! printf ("\n"); |
5827 | 162 %! % ---------------------------------------------------- |
163 %! % Demonstrates processing of an entire string split on | |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
164 %! % a variety of delimiters. Tokens and delimiters are |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
165 %! % printed one after another in angle brackets. |
5827 | 166 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
167 ## Test the tokens for all cases |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
168 %!assert (strtok (""), ""); # no string |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
169 %!assert (strtok ("this"), "this"); # no delimiter in string |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
170 %!assert (strtok ("this "), "this"); # delimiter at end |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
171 %!assert (strtok ("this is"), "this"); # delimiter in middle |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
172 %!assert (strtok (" this"), "this"); # delimiter at start |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
173 %!assert (strtok (" this "), "this"); # delimiter at start and end |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
174 %!assert (strtok (" "), ""(1:0)); # delimiter only |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
175 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
176 ## Test the remainder for all cases |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
177 %!test [t,r] = strtok (""); assert (r, ""); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
178 %!test [t,r] = strtok ("this"); assert (r, ""); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
179 %!test [t,r] = strtok ("this "); assert (r, " "); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
180 %!test [t,r] = strtok ("this is"); assert (r, " is"); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
181 %!test [t,r] = strtok (" this"); assert (r, ""); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
182 %!test [t,r] = strtok (" this "); assert (r, " "); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
183 %!test [t,r] = strtok (" "); assert (r, ""); |
5827 | 184 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
185 ## Test all tokens and remainders with cell array input |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
186 %!test |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
187 %! str = {"", "this", "this ", "this is", " this", " this ", " "}; |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
188 %! [t, r] = strtok (str); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
189 %! assert (t{1}, ""); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
190 %! assert (r{1}, ""); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
191 %! assert (t{2}, "this"); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
192 %! assert (r{2}, ""); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
193 %! assert (t{3}, "this"); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
194 %! assert (r{3}, " "); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
195 %! assert (t{4}, "this"); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
196 %! assert (r{4}, " is"); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
197 %! assert (t{5}, "this"); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
198 %! assert (r{5}, ""); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
199 %! assert (t{6}, "this"); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
200 %! assert (r{6}, " "); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
201 %! assert (t{7}, ""); |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
202 %! assert (r{7}, ""); |
5827 | 203 |
27956
2310164737b3
fix many spelling errors (bug #57613)
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
204 ## Simple check for 2, 3, and 4 delimiters |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
205 %!assert (strtok ("this is", "i "), "th") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
206 %!assert (strtok ("this is", "ij "), "th") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
207 %!assert (strtok ("this is", "ijk "), "th") |
5827 | 208 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
209 ## Test all cases for 8 delimiters since a different |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
210 %!# algorithm is used when more than 7 delimiters |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
211 %!assert (strtok ("","jklmnop "), "") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
212 %!assert (strtok ("this","jklmnop "), "this") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
213 %!assert (strtok ("this ","jklmnop "), "this") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
214 %!assert (strtok ("this is","jklmnop "), "this") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
215 %!assert (strtok (" this","jklmnop "), "this") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
216 %!assert (strtok (" this ","jklmnop "), "this") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
217 %!assert (strtok (" ","jklmnop "), ""(1:0)) |
8002
30f560a5fbc3
strtok.m: include TAB, LF, VT, FF, and CR in default set of delim characters
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
218 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
219 ## Test 'bad' string orientations |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
220 %!assert (strtok (" this ".'), "this".'); # delimiter at start and end |
21317
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
221 %!assert (strtok (" this ".',"jkl "), "this".') |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
222 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
223 ## Test with TAB, LF, VT, FF, and CR |
8002
30f560a5fbc3
strtok.m: include TAB, LF, VT, FF, and CR in default set of delim characters
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
224 %!test |
30f560a5fbc3
strtok.m: include TAB, LF, VT, FF, and CR in default set of delim characters
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
225 %! for ch = "\t\n\v\f\r" |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
226 %! [t, r] = strtok (["beg", ch, "end"]); |
8002
30f560a5fbc3
strtok.m: include TAB, LF, VT, FF, and CR in default set of delim characters
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
227 %! assert (t, "beg"); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
228 %! assert (r, [ch, "end"]); |
8002
30f560a5fbc3
strtok.m: include TAB, LF, VT, FF, and CR in default set of delim characters
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
229 %! endfor |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
230 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
231 ## Test input validation |
13790
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
232 %!error strtok () |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
233 %!error strtok ("a", "b", "c") |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
234 %!error <STR must be a string> strtok (1, "b") |
fa94d6a93d45
strtok.m: Revamp code for performance. Add cellstr input functionality.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
235 %!error <STR cannot be a 2-D> strtok (char ("hello", "world"), "l") |