annotate scripts/strings/strsplit.m @ 26070:01da3729016a

strsplit.m: Simplify input validation test for multi-line inputs in cset edc02d5eeac6. * strsplit.m: Use rows (str) > 1 to test for multi-line char vectors.
author Rik <rik@octave.org>
date Wed, 14 Nov 2018 08:04:40 -0800
parents edc02d5eeac6
children 1ee1ddeee5ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25054
6652d3823428 maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1 ## Copyright (C) 2009-2018 Jaroslav Hajek
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 8884
diff changeset
3 ## This file is part of Octave.
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 8884
diff changeset
4 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
5 ## Octave is free software: you can redistribute it and/or modify it
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 ## 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: 23573
diff changeset
7 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
8 ## (at your option) any later version.
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 8884
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
13 ## GNU General Public License for more details.
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 8884
diff changeset
16 ## 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: 23573
diff changeset
17 ## <https://www.gnu.org/licenses/>.
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
20 ## @deftypefn {} {[@var{cstr}] =} strsplit (@var{str})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
21 ## @deftypefnx {} {[@var{cstr}] =} strsplit (@var{str}, @var{del})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
22 ## @deftypefnx {} {[@var{cstr}] =} strsplit (@dots{}, @var{name}, @var{value})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
23 ## @deftypefnx {} {[@var{cstr}, @var{matches}] =} strsplit (@dots{})
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20098
diff changeset
24 ## Split the string @var{str} using the delimiters specified by @var{del} and
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20098
diff changeset
25 ## return a cell string array of substrings.
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
26 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
27 ## If a delimiter is not specified the string is split at whitespace
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
28 ## @code{@{" ", "\f", "\n", "\r", "\t", "\v"@}}. Otherwise, the delimiter,
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
29 ## @var{del} must be a string or cell array of strings. By default,
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## consecutive delimiters in the input string @var{s} are collapsed into one
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ## resulting in a single split.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## Supported @var{name}/@var{value} pair arguments are:
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
34 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## @itemize
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
36 ## @item @var{collapsedelimiters} which may take the value of @code{true}
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
37 ## (default) or @code{false}.
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
38 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
39 ## @item @var{delimitertype} which may take the value of @qcode{"simple"}
20098
21dbaf135667 doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 19989
diff changeset
40 ## (default) or @nospell{@qcode{"regularexpression"}}. A simple delimiter
21dbaf135667 doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 19989
diff changeset
41 ## matches the text exactly as written. Otherwise, the syntax for regular
21dbaf135667 doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 19989
diff changeset
42 ## expressions outlined in @code{regexp} is used.
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
43 ## @end itemize
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
44 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
45 ## The optional second output, @var{matches}, returns the delimiters which were
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ## matched in the original string.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
47 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
48 ## Examples with simple delimiters:
13929
9cae456085c2 Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13776
diff changeset
49 ##
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
50 ## @example
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
51 ## strsplit ("a b c")
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
52 ## @result{}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
53 ## @{
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
54 ## [1,1] = a
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
55 ## [1,2] = b
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
56 ## [1,3] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
57 ## @}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
58 ##
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
59 ## strsplit ("a,b,c", ",")
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
60 ## @result{}
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
61 ## @{
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
62 ## [1,1] = a
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
63 ## [1,2] = b
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
64 ## [1,3] = c
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
65 ## @}
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
66 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
67 ## strsplit ("a foo b,bar c", @{" ", ",", "foo", "bar"@})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
68 ## @result{}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
69 ## @{
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
70 ## [1,1] = a
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
71 ## [1,2] = b
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
72 ## [1,3] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
73 ## @}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
74 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
75 ## strsplit ("a,,b, c", @{",", " "@}, "collapsedelimiters", false)
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
76 ## @result{}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
77 ## @{
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
78 ## [1,1] = a
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
79 ## [1,2] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
80 ## [1,3] = b
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
81 ## [1,4] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
82 ## [1,5] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
83 ## @}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
84 ##
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
85 ## @end example
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
86 ##
20098
21dbaf135667 doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 19989
diff changeset
87 ## Examples with @nospell{regularexpression} delimiters:
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
88 ##
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
89 ## @smallexample
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
90 ## strsplit ("a foo b,bar c", ',|\s|foo|bar', "delimitertype", "regularexpression")
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
91 ## @result{}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
92 ## @{
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
93 ## [1,1] = a
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
94 ## [1,2] = b
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
95 ## [1,3] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
96 ## @}
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
97 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
98 ## strsplit ("a,,b, c", '[, ]', "collapsedelimiters", false, "delimitertype", "regularexpression")
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
99 ## @result{}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
100 ## @{
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
101 ## [1,1] = a
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
102 ## [1,2] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
103 ## [1,3] = b
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
104 ## [1,4] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
105 ## [1,5] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
106 ## @}
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
107 ##
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
108 ## strsplit ("a,\t,b, c", @{',', '\s'@}, "delimitertype", "regularexpression")
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
109 ## @result{}
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
110 ## @{
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
111 ## [1,1] = a
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
112 ## [1,2] = b
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
113 ## [1,3] = c
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
114 ## @}
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
115 ##
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
116 ## strsplit ("a,\t,b, c", @{',', ' ', '\t'@}, "collapsedelimiters", false)
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
117 ## @result{}
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
118 ## @{
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
119 ## [1,1] = a
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
120 ## [1,2] =
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
121 ## [1,3] =
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
122 ## [1,4] = b
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
123 ## [1,5] =
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
124 ## [1,6] = c
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
125 ## @}
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
126 ## @end smallexample
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
127 ##
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
128 ## @seealso{ostrsplit, strjoin, strtok, regexp}
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 ## @end deftypefn
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
130
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
131 function [cstr, matches] = strsplit (str, del, varargin)
8884
579de77acd90 strsplit.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8883
diff changeset
132
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
133 args.collapsedelimiters = true;
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
134 args.delimitertype = "simple";
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
135
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
136 [reg, params] = parseparams (varargin);
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
137
16712
9ce08a1efc29 fix strsplit input validation
Mike Miller <mtmiller@ieee.org>
parents: 16557
diff changeset
138 if (nargin < 1)
9ce08a1efc29 fix strsplit input validation
Mike Miller <mtmiller@ieee.org>
parents: 16557
diff changeset
139 print_usage ();
9ce08a1efc29 fix strsplit input validation
Mike Miller <mtmiller@ieee.org>
parents: 16557
diff changeset
140 elseif (numel (reg) > 1)
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
141 print_usage ();
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
142 elseif (numel (reg) == 1)
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
143 ## This is undocumented behavior to accept a logical 3rd arg.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
144 if (islogical (reg{1}))
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
145 args.collapsedelimiters = reg{1};
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
146 else
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
147 print_usage ();
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
148 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
149 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
150 fields = fieldnames (args);
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
151 for n = 1:2:numel (params)
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
152 if (any (strcmpi (params{n}, fields)))
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
153 args.(tolower (params{n})) = params{n+1};
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
154 elseif (ischar (varargin{n}))
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
155 error ("strsplit:invalid_parameter_name",
16766
7268845c0a1e avoid backquote in error messages, some uses in doc strings
John W. Eaton <jwe@octave.org>
parents: 16727
diff changeset
156 "strsplit: invalid parameter name, '%s'", varargin{n});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
157 else
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
158 print_usage ();
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
159 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
160 endfor
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
161
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 16826
diff changeset
162 ## Save the length of the "delimitertype" parameter
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17386
diff changeset
163 length_deltype = length (args.delimitertype);
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
164
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
165 if (nargin == 1 || (nargin > 1 && islogical (del)))
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
166 if (nargin > 1)
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
167 ## Second input is the "collapsedelimiters" parameter
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
168 args.collapsedelimiters = del;
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
169 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
170 ## Set proper default for the delimiter type
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
171 if (strncmpi (args.delimitertype, "simple", length_deltype))
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
172 del = {" ", "\f", "\n", "\r", "\t", "\v"};
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
173 else
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
174 del = '\s';
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
175 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
176 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
177
16712
9ce08a1efc29 fix strsplit input validation
Mike Miller <mtmiller@ieee.org>
parents: 16557
diff changeset
178 if (! ischar (str) || (! ischar (del) && ! iscellstr (del)))
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
179 error ("strsplit: S and DEL must be string values");
26070
01da3729016a strsplit.m: Simplify input validation test for multi-line inputs in cset edc02d5eeac6.
Rik <rik@octave.org>
parents: 26069
diff changeset
180 elseif (rows (str) > 1)
26069
edc02d5eeac6 strsplit: error if str has multiple rows instead of silently ignore (bug #55021)
Carnë Draug <carandraug@octave.org>
parents: 25054
diff changeset
181 error ("strsplit: S must be a char row vector")
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
182 elseif (! isscalar (args.collapsedelimiters))
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
183 error ("strsplit: COLLAPSEDELIMITERS must be a scalar value");
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
184 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
185
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
186 if (strncmpi (args.delimitertype, "simple", length_deltype))
20181
aa36fb998a4d maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents: 20164
diff changeset
187 is_simple = true;
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
188 elseif (strncmpi (args.delimitertype, "regularexpression", length_deltype))
20181
aa36fb998a4d maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents: 20164
diff changeset
189 is_simple = false;
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
190 else
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
191 error ("strsplit:invalid_delimitertype", "strsplit: Invalid DELIMITERTYPE");
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
192 endif
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
193
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
194 if (is_simple)
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
195 if (iscellstr (del))
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
196 del = cellfun (@do_string_escapes, del, "uniformoutput", false);
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
197 else
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
198 del = do_string_escapes (del);
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
199 endif
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
200 ## Escape characters which have a special meaning in regexp.
21453
04e0598f8b94 Allow strsplit to use '+' as a delimiter (bug #47403).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20181
diff changeset
201 del = regexprep (del, '([{}()[\]^$.*?+|\\])', '\\$1');
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
202 endif
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
203
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
204 if (isempty (str))
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
205 cstr = {str};
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
206 else
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
207 if (iscellstr (del))
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
208 del = sprintf ("%s|", del{:});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
209 del(end) = [];
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
210 endif
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
211 if (args.collapsedelimiters)
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
212 del = [ "(" del ")+" ];
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
213 endif
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
214 [cstr, matches] = regexp (str, del, "split", "match");
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
215 endif
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
216
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
217 endfunction
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
218
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17336
diff changeset
219
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
220 %!shared str
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
221 %! str = "The rain in Spain stays mainly in the plain.";
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
222
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
223 ## Split on all whitespace.
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
224 %!assert (strsplit (str), {"The", "rain", "in", "Spain", "stays", ...
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
225 %! "mainly", "in", "the", "plain."})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
226 ## Split on "ain".
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
227 %!assert (strsplit (str, "ain"), {"The r", " in Sp", " stays m", ...
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
228 %! "ly in the pl", "."})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
229 ## Split on " " and "ain" (treating multiple delimiters as one).
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
230 %!test
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
231 %! s = strsplit (str, '\s|ain', true, "delimitertype", "r");
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
232 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
233 %! "m", "ly", "in", "the", "pl", "."});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
234 %!test
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
235 %! s = strsplit (str, '\s|ain', true, "delimitertype", "r");
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
236 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
237 %! "m", "ly", "in", "the", "pl", "."});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
238 %!test
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
239 %! [s, m] = strsplit (str, {'\s', 'ain'}, true, "delimitertype", "r");
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
240 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
241 %! "m", "ly", "in", "the", "pl", "."});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
242 %! assert (m, {" ", "ain ", " ", "ain ", " ", "ain", " ", " ", " ", "ain"});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
243 ## Split on " " and "ain", and treat multiple delimiters separately.
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
244 %!test
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
245 %! [s, m] = strsplit (str, {" ", "ain"}, "collapsedelimiters", false);
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
246 %! assert (s, {"The", "r", "", "in", "Sp", "", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
247 %! "m", "ly", "in", "the", "pl", "."});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
248 %! assert (m, {" ", "ain", " ", " ", "ain", " ", " ", "ain", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
249 %! " ", " ", " ", "ain"});
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
250
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
251 %!assert (strsplit ("road to hell"), {"road", "to", "hell"})
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
252 %!assert (strsplit ("road to hell", " "), {"road", "to", "hell"})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
253 %!assert (strsplit ("road to^hell", {" ","^"}), {"road", "to", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
254 %!assert (strsplit ("road to--hell", {" ","-"}, true), {"road", "to", "hell"})
16411
5be43435bd5b Improve speed and backward compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 16403
diff changeset
255 %!assert (strsplit (["a,bc,,de"], ",", false, "delimitertype", "s"), {"a", "bc", "", "de"})
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
256 %!assert (strsplit (["a,bc,,de"], ",", false), {"a", "bc", "", "de"})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
257 %!assert (strsplit (["a,bc,de"], ",", true), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
258 %!assert (strsplit (["a,bc,de"], {","," "}, true), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
259
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
260 %!assert (strsplit ("road to hell", " ", "delimitertype", "r"), {"road", "to", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
261 %!assert (strsplit ("road to^hell", '\^| ', "delimitertype", "r"), {"road", "to", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
262 %!assert (strsplit ("road to^hell", "[ ^]", "delimitertype", "r"), {"road", "to", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
263 %!assert (strsplit ("road to--hell", "[ -]", false, "delimitertype", "r"), {"road", "", "", "to", "", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
264 %!assert (strsplit (["a,bc,de"], ",", "delimitertype", "r"), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
265 %!assert (strsplit (["a,bc,,de"], ",", false, "delimitertype", "r"), {"a", "bc", "", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
266 %!assert (strsplit (["a,bc,de"], ",", true, "delimitertype", "r"), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
267 %!assert (strsplit (["a,bc,de"], "[, ]", true, "delimitertype", "r"), {"a", "bc", "de"})
21317
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20852
diff changeset
268 %!assert (strsplit ("hello \t world", true, "delimitertype", "r"), {"hello", "world"})
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
269
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
270 %!assert (strsplit ("foo\tbar", '\t', "delimitertype", "r"), {"foo", "bar"})
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
271 %!assert (strsplit ("foo\tbar", '\t', "delimitertype", "s"), {"foo", "bar"})
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
272
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
273 ## Test "match" for consecutive delmiters
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
274 %!test
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
275 %! [a, m] = strsplit ("a\t \nb", '\s', "delimitertype", "regularexpression",
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
276 %! "collapsedelimiters", false);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
277 %! assert (a, {"a", "", "", "b"});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
278 %! assert (m, {"\t", " ", "\n"});
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
279 %!test
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
280 %! [a, m] = strsplit ("a\t \nb", '\s', false, "delimitertype", "regularexpression");
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
281 %! assert (a, {"a", "", "", "b"});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
282 %! assert (m, {"\t", " ", "\n"});
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
283 %!test
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
284 %! [a, m] = strsplit ("a\t \nb", '\s', "delimitertype", "regularexpression");
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
285 %! assert (a, {"a", "b"});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
286 %! assert (m, {"\t \n"});
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
287 %!test
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
288 %! [a, m] = strsplit ("a\t \nb", {"\t", " ", "\n"}, "delimitertype", "simple");
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
289 %! assert (a, {"a", "b"});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
290 %! assert (m, {"\t \n"});
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
291 %!test
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
292 %! [s, m] = strsplit ("hello \t world", true);
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
293 %! assert (s, {"hello", "world"});
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
294 %! assert (m, {" \t "});
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
295
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
296 ## Compatibility
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
297 %! assert (strsplit ("", "a"), {""});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
298 %! assert (strsplit ("a", "a"), {"", ""});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
299 %! assert (strsplit ("aa", "a"), {"", ""});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
300 %! assert (strsplit ("aaa", "a"), {"", ""});
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
301
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
302 %!assert <*44641> (strsplit ("xxx<yyy", "<"), {"xxx", "yyy"})
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
303 %!assert <*44641> (strsplit ('xxx\yyy', '\'), {"xxx", "yyy"})
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
304
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
305 %!assert <*47403> (strsplit ('xxx+yyy', '+'), {"xxx", "yyy"})
21453
04e0598f8b94 Allow strsplit to use '+' as a delimiter (bug #47403).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20181
diff changeset
306
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
307 ## Test input validation
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
308 %!error strsplit ()
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
309 %!error strsplit ("abc", "b", true, 4)
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
310 %!error <invalid parameter name, 'foo'> strsplit ("abc", "b", "foo", "true")
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
311 %!error <S and DEL must be string values> strsplit (123, "b")
26070
01da3729016a strsplit.m: Simplify input validation test for multi-line inputs in cset edc02d5eeac6.
Rik <rik@octave.org>
parents: 26069
diff changeset
312 %!error <S must be a char row vector> strsplit (["abc"; "xyz"])
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
313 %!error <COLLAPSEDELIMITERS must be a scalar value> strsplit ("abc", "def", "collapsedelimiters", ones (3,3))
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
314 %!error <Invalid DELIMITERTYPE> strsplit ("abc", "b", "delimitertype", "foobar")