annotate scripts/strings/strsplit.m @ 33623:4517f929c59d bytecode-interpreter tip

maint: Merge default to bytecode-interpreter
author Arun Giridhar <arungiridhar@gmail.com>
date Fri, 24 May 2024 16:22:41 -0400
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 32002
diff changeset
3 ## Copyright (C) 2009-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27069
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/>.
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 8884
diff changeset
8 ## This file is part of Octave.
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 8884
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
10 ## 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
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: 23573
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: 22489
diff changeset
13 ## (at your option) any later version.
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 8884
diff changeset
15 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
18 ## GNU General Public License for more details.
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ##
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 ## 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
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: 23573
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 ########################################################################
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
27 ## @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
28 ## @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
29 ## @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
30 ## @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
31 ## 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
32 ## 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
33 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## 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
35 ## @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
36 ## @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
37 ## 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
38 ## resulting in a single split.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
39 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
40 ## 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
41 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
42 ## @itemize
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
43 ## @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
44 ## (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
45 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ## @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
47 ## (default) or @nospell{@qcode{"regularexpression"}}. A simple delimiter
21dbaf135667 doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 19989
diff changeset
48 ## 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
49 ## 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
50 ## @end itemize
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
51 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
52 ## 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
53 ## matched in the original string.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
54 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
55 ## Examples with simple delimiters:
13929
9cae456085c2 Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13776
diff changeset
56 ##
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
57 ## @example
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
58 ## strsplit ("a b c")
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
59 ## @result{}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
60 ## @{
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
61 ## [1,1] = a
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
62 ## [1,2] = b
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
63 ## [1,3] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
64 ## @}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
65 ##
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
66 ## 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
67 ## @result{}
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
68 ## @{
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
69 ## [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
70 ## [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
71 ## [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
72 ## @}
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
73 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
74 ## strsplit ("a foo b,bar c", @{" ", ",", "foo", "bar"@})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
75 ## @result{}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
76 ## @{
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
77 ## [1,1] = a
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
78 ## [1,2] = b
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
79 ## [1,3] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
80 ## @}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
81 ##
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
82 ## strsplit ("a,,b, c", @{",", " "@}, "collapsedelimiters", false)
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
83 ## @result{}
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 ## [1,1] = a
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
86 ## [1,2] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
87 ## [1,3] = b
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
88 ## [1,4] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
89 ## [1,5] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
90 ## @}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
91 ##
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
92 ## @end example
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
93 ##
20098
21dbaf135667 doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 19989
diff changeset
94 ## Examples with @nospell{regularexpression} delimiters:
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
95 ##
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
96 ## @smallexample
26577
ae1542fb58ba documentation: Avoid some overfull lines in the PDF manual.
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
97 ## strsplit ("a foo b,bar c", ',|\s|foo|bar', ...
ae1542fb58ba documentation: Avoid some overfull lines in the PDF manual.
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
98 ## "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
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
102 ## [1,2] = b
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
103 ## [1,3] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
104 ## @}
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
105 ##
26577
ae1542fb58ba documentation: Avoid some overfull lines in the PDF manual.
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
106 ## strsplit ("a,,b, c", '[, ]', "collapsedelimiters", false, ...
ae1542fb58ba documentation: Avoid some overfull lines in the PDF manual.
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
107 ## "delimitertype", "regularexpression")
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
108 ## @result{}
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
109 ## @{
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
110 ## [1,1] = a
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
111 ## [1,2] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
112 ## [1,3] = b
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
113 ## [1,4] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
114 ## [1,5] = c
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
115 ## @}
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
116 ##
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
117 ## 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
118 ## @result{}
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
119 ## @{
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
120 ## [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
121 ## [1,2] = b
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
122 ## [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
123 ## @}
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
124 ##
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
125 ## 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
126 ## @result{}
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
127 ## @{
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
128 ## [1,1] = a
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
129 ## [1,2] =
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
130 ## [1,3] =
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
131 ## [1,4] = b
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
132 ## [1,5] =
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
133 ## [1,6] = c
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
134 ## @}
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
135 ## @end smallexample
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
136 ##
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
137 ## @seealso{ostrsplit, strjoin, strtok, regexp}
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
138 ## @end deftypefn
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
139
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
140 function [cstr, matches] = strsplit (str, del, varargin)
8884
579de77acd90 strsplit.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8883
diff changeset
141
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
142 args.collapsedelimiters = true;
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
143 args.delimitertype = "simple";
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
144
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
145 [reg, params] = parseparams (varargin);
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
146
16712
9ce08a1efc29 fix strsplit input validation
Mike Miller <mtmiller@ieee.org>
parents: 16557
diff changeset
147 if (nargin < 1)
9ce08a1efc29 fix strsplit input validation
Mike Miller <mtmiller@ieee.org>
parents: 16557
diff changeset
148 print_usage ();
9ce08a1efc29 fix strsplit input validation
Mike Miller <mtmiller@ieee.org>
parents: 16557
diff changeset
149 elseif (numel (reg) > 1)
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
150 print_usage ();
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
151 elseif (numel (reg) == 1)
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
152 ## 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
153 if (islogical (reg{1}))
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
154 args.collapsedelimiters = reg{1};
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
155 else
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
156 print_usage ();
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
157 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
158 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
159 fields = fieldnames (args);
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
160 for n = 1:2:numel (params)
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
161 if (any (strcmpi (params{n}, fields)))
32002
939e5d952675 Use upper/lower in preference to toupper/tolower to match Matlab.
Rik <rik@octave.org>
parents: 31706
diff changeset
162 args.(lower (params{n})) = params{n+1};
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
163 elseif (ischar (varargin{n}))
27069
0a62d9a6aa2d Place Octave's warning and error IDs in to the "Octave" namespace (bug #56213).
Rik <rik@octave.org>
parents: 26577
diff changeset
164 error ("Octave: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
165 "strsplit: invalid parameter name, '%s'", varargin{n});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
166 else
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
167 print_usage ();
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
168 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
169 endfor
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
170
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 16826
diff changeset
171 ## Save the length of the "delimitertype" parameter
17405
3f0ed69d21c6 Replace unnecessary instances of strncmp with strcmp.
Rik <rik@octave.org>
parents: 17386
diff changeset
172 length_deltype = length (args.delimitertype);
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
173
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
174 if (nargin == 1 || (nargin > 1 && islogical (del)))
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
175 if (nargin > 1)
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
176 ## Second input is the "collapsedelimiters" parameter
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
177 args.collapsedelimiters = del;
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
178 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
179 ## 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
180 if (strncmpi (args.delimitertype, "simple", length_deltype))
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
181 del = {" ", "\f", "\n", "\r", "\t", "\v"};
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
182 else
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
183 del = '\s';
16403
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 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
186
16712
9ce08a1efc29 fix strsplit input validation
Mike Miller <mtmiller@ieee.org>
parents: 16557
diff changeset
187 if (! ischar (str) || (! ischar (del) && ! iscellstr (del)))
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
188 error ("strsplit: S and DEL must be string values");
26076
421ea6654fa4 strsplit.m: use isrow() to check for row vector input.
Rik <rik@octave.org>
parents: 26073
diff changeset
189 elseif (! isempty (str) && ! isrow (str))
28928
ae7ce8358953 maint: Add semicolon to end of all warning() and error() invocations.
Rik <rik@octave.org>
parents: 28896
diff changeset
190 error ("strsplit: S must be a char row vector");
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
191 elseif (! isscalar (args.collapsedelimiters))
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
192 error ("strsplit: COLLAPSEDELIMITERS must be a scalar value");
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
193 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
194
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
195 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
196 is_simple = true;
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
197 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
198 is_simple = false;
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
199 else
27069
0a62d9a6aa2d Place Octave's warning and error IDs in to the "Octave" namespace (bug #56213).
Rik <rik@octave.org>
parents: 26577
diff changeset
200 error ("Octave:strsplit:invalid_delimitertype", "strsplit: Invalid DELIMITERTYPE");
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
201 endif
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
202
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
203 if (is_simple)
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
204 if (iscellstr (del))
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
205 del = cellfun (@do_string_escapes, del, "uniformoutput", false);
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
206 else
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
207 del = do_string_escapes (del);
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
208 endif
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
209 ## 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
210 del = regexprep (del, '([{}()[\]^$.*?+|\\])', '\\$1');
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
211 endif
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
212
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
213 if (isempty (str))
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
214 cstr = {str};
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
215 else
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
216 if (iscellstr (del))
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
217 del = sprintf ("%s|", del{:});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
218 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
219 endif
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
220 if (args.collapsedelimiters)
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
221 del = [ "(" del ")+" ];
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
222 endif
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
223 [cstr, matches] = regexp (str, del, "split", "match");
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
224 endif
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
225
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
226 endfunction
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
227
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17336
diff changeset
228
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
229 %!shared str
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
230 %! 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
231
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
232 ## Split on all whitespace.
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
233 %!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
234 %! "mainly", "in", "the", "plain."})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
235 ## Split on "ain".
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
236 %!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
237 %! "ly in the pl", "."})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
238 ## 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
239 %!test
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
240 %! 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
241 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
242 %! "m", "ly", "in", "the", "pl", "."});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
243 %!test
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
244 %! 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
245 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
246 %! "m", "ly", "in", "the", "pl", "."});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
247 %!test
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
248 %! [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
249 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
250 %! "m", "ly", "in", "the", "pl", "."});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
251 %! assert (m, {" ", "ain ", " ", "ain ", " ", "ain", " ", " ", " ", "ain"});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
252 ## Split on " " and "ain", and treat multiple delimiters separately.
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
253 %!test
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
254 %! [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
255 %! assert (s, {"The", "r", "", "in", "Sp", "", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
256 %! "m", "ly", "in", "the", "pl", "."});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
257 %! assert (m, {" ", "ain", " ", " ", "ain", " ", " ", "ain", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
258 %! " ", " ", " ", "ain"});
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
259
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
260 %!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
261 %!assert (strsplit ("road to hell", " "), {"road", "to", "hell"})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
262 %!assert (strsplit ("road to^hell", {" ","^"}), {"road", "to", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
263 %!assert (strsplit ("road to--hell", {" ","-"}, true), {"road", "to", "hell"})
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
264 %!assert (strsplit (["a,bc,,de"], ",", false, "delimitertype", "s"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
265 %! {"a", "bc", "", "de"})
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
266 %!assert (strsplit (["a,bc,,de"], ",", false), {"a", "bc", "", "de"})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
267 %!assert (strsplit (["a,bc,de"], ",", true), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
268 %!assert (strsplit (["a,bc,de"], {","," "}, true), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
269
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
270 %!assert (strsplit ("road to hell", " ", "delimitertype", "r"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
271 %! {"road", "to", "hell"})
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
272 %!assert (strsplit ("road to^hell", '\^| ', "delimitertype", "r"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
273 %! {"road", "to", "hell"})
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
274 %!assert (strsplit ("road to^hell", "[ ^]", "delimitertype", "r"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
275 %! {"road", "to", "hell"})
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
276 %!assert (strsplit ("road to--hell", "[ -]", false, "delimitertype", "r"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
277 %! {"road", "", "", "to", "", "hell"})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
278 %!assert (strsplit (["a,bc,de"], ",", "delimitertype", "r"), {"a", "bc", "de"})
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
279 %!assert (strsplit (["a,bc,,de"], ",", false, "delimitertype", "r"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
280 %! {"a", "bc", "", "de"})
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
281 %!assert (strsplit (["a,bc,de"], ",", true, "delimitertype", "r"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
282 %! {"a", "bc", "de"})
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
283 %!assert (strsplit (["a,bc,de"], "[, ]", true, "delimitertype", "r"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
284 %! {"a", "bc", "de"})
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
285 %!assert (strsplit ("hello \t world", true, "delimitertype", "r"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
286 %! {"hello", "world"})
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
287
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
288 %!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
289 %!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
290
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
291 ## Test "match" for consecutive delmiters
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
292 %!test
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
293 %! [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
294 %! "collapsedelimiters", false);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
295 %! assert (a, {"a", "", "", "b"});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
296 %! assert (m, {"\t", " ", "\n"});
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
297 %!test
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
298 %! [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
299 %! assert (a, {"a", "", "", "b"});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
300 %! assert (m, {"\t", " ", "\n"});
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
301 %!test
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
302 %! [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
303 %! assert (a, {"a", "b"});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
304 %! assert (m, {"\t \n"});
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
305 %!test
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
306 %! [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
307 %! assert (a, {"a", "b"});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
308 %! assert (m, {"\t \n"});
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
309 %!test
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
310 %! [s, m] = strsplit ("hello \t world", true);
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
311 %! assert (s, {"hello", "world"});
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
312 %! assert (m, {" \t "});
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
313
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
314 ## Compatibility
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
315 %! assert (strsplit ("", "a"), {""});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
316 %! assert (strsplit ("a", "a"), {"", ""});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
317 %! assert (strsplit ("aa", "a"), {"", ""});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21454
diff changeset
318 %! assert (strsplit ("aaa", "a"), {"", ""});
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
319
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
320 %!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
321 %!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
322
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
323 %!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
324
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
325 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 27923
diff changeset
326 %!error <Invalid call> strsplit ()
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
327 %!error strsplit ("abc", "b", true, 4)
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
328 %!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
329 %!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
330 %!error <S must be a char row vector> strsplit (["abc"; "xyz"])
26073
1ee1ddeee5ac strsplit: don't check only rows because of nd arrays (backout 01da3729016a)
Carnë Draug <carandraug@octave.org>
parents: 26070
diff changeset
331 %!error <S must be a char row vector> strsplit (reshape ("axbycz", [1 3 2]))
19989
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19833
diff changeset
332 %!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
333 %!error <Invalid DELIMITERTYPE> strsplit ("abc", "b", "delimitertype", "foobar")