annotate scripts/strings/strsplit.m @ 20024:7569e880d56c

strsplit.m: Overhaul function and fix bug #44641. * strsplit.m: Rewrite docstring. Match variables in documentation to those in function. Change undocumented 3rd argument to only accept logical args. Use single quotes around regexp patterns to avoid lots of backslash escaping. Define is_simple variable to avoid repeated strncmpi calls. Escape special regexp characters in "simple" split expressions (bug #44641). Wrap some BIST tests to less than 80 columns. Add BIST tests for bug #44641.
author Rik <rik@octave.org>
date Sat, 28 Mar 2015 19:37:34 -0700
parents 9fc020886ae9
children 21dbaf135667
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19627
diff changeset
1 ## Copyright (C) 2009-2015 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 ##
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 8884
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
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## your option) any later version.
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
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ## General Public License for more details.
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
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
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 -*-
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
20 ## @deftypefn {Function File} {[@var{cstr}] =} strsplit (@var{str})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
21 ## @deftypefnx {Function File} {[@var{cstr}] =} strsplit (@var{str}, @var{del})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
22 ## @deftypefnx {Function File} {[@var{cstr}] =} strsplit (@dots{}, @var{name}, @var{value})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
23 ## @deftypefnx {Function File} {[@var{cstr}, @var{matches}] =} strsplit (@dots{})
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
24 ## Split the string @var{str} using the delimiters specified by @var{del}
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
25 ## and return a cell string array of substrings.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
26 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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: 19867
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: 19867
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: 19867
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: 19867
diff changeset
31 ## resulting in a single split.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
32 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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 ##
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
35 ## @itemize
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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: 19867
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 ##
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
39 ## @item @var{delimitertype} which may take the value of @qcode{"simple"}
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
40 ## (default) or @qcode{"regularexpression"}. A simple delimiter matches the
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
41 ## text exactly as written. Otherwise, the syntax for regular expressions
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
42 ## outlined in @code{regexp} is used.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
43 ## @end itemize
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
44 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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: 19867
diff changeset
46 ## matched in the original string.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
47 ##
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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 ##
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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 ##
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
diff changeset
79 ## [1,2] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
80 ## [1,3] = b
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
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 ##
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
87 ## Examples with regularexpression delimiters:
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
88 ##
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
89 ## @smallexample
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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 ## @}
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
diff changeset
97 ##
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
diff changeset
102 ## [1,2] =
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
103 ## [1,3] = b
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
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 ## @}
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
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 ## @}
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
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
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
diff changeset
120 ## [1,2] =
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
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
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
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 ## @}
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
126 ## @end smallexample
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18185
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
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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;
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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)
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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: 19867
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);
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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)))
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
171 if (strncmpi (args.delimitertype, "simple", length_deltype))
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
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");
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
180 elseif (! isscalar (args.collapsedelimiters))
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
181 error ("strsplit: COLLAPSEDELIMITERS must be a scalar value");
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
182 endif
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
183
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
184 if (strncmpi (args.delimitertype, "simple", length_deltype))
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
185 is_simple = true;
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
186 elseif (strncmpi (args.delimitertype, "regularexpression", length_deltype))
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
187 is_simple = false;
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
188 else
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
189 error ("strsplit:invalid_delimitertype", "strsplit: Invalid DELIMITERTYPE");
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
190 endif
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
191
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
192 if (is_simple)
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
193 if (iscellstr (del))
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
194 del = cellfun (@do_string_escapes, del, "uniformoutput", false);
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
195 else
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
196 del = do_string_escapes (del);
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
197 endif
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
198 ## Escape characters which have a special meaning in regexp.
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
199 del = regexprep (del, '([{}()[\]^$.*?|\\])', '\\$1');
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
200 endif
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
201
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
202 if (isempty (str))
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
203 cstr = {str};
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
204 else
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
205 if (iscellstr (del))
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
206 del = sprintf ("%s|", del{:});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
207 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
208 endif
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
209 if (args.collapsedelimiters)
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
210 del = [ "(" del ")+" ];
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
211 endif
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
212 [cstr, matches] = regexp (str, del, "split", "match");
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
213 endif
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
214
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
215 endfunction
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
216
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17336
diff changeset
217
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
218 %!shared str
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
219 %! str = "The rain in Spain stays mainly in the plain.";
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
220
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
221 ## Split on all whitespace.
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
222 %!assert (strsplit (str), {"The", "rain", "in", "Spain", "stays", ...
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
223 %! "mainly", "in", "the", "plain."})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
224 ## Split on "ain".
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
225 %!assert (strsplit (str, "ain"), {"The r", " in Sp", " stays m", ...
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
226 %! "ly in the pl", "."})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
227 ## Split on " " and "ain" (treating multiple delimiters as one).
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
228 %!test
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
229 %! s = strsplit (str, '\s|ain', true, "delimitertype", "r");
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
230 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
231 %! "m", "ly", "in", "the", "pl", "."});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
232 %!test
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
233 %! s = strsplit (str, '\s|ain', true, "delimitertype", "r");
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
234 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
235 %! "m", "ly", "in", "the", "pl", "."});
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
236 %!test
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
237 %! [s, m] = strsplit (str, {'\s', 'ain'}, true, "delimitertype", "r");
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
238 %! assert (s, {"The", "r", "in", "Sp", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
239 %! "m", "ly", "in", "the", "pl", "."});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
240 %! assert (m, {" ", "ain ", " ", "ain ", " ", "ain", " ", " ", " ", "ain"});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
241 ## Split on " " and "ain", and treat multiple delimiters separately.
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
242 %!test
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
243 %! [s, m] = strsplit (str, {" ", "ain"}, "collapsedelimiters", false);
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
244 %! assert (s, {"The", "r", "", "in", "Sp", "", "stays", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
245 %! "m", "ly", "in", "the", "pl", "."});
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
246 %! assert (m, {" ", "ain", " ", " ", "ain", " ", " ", "ain", ...
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
247 %! " ", " ", " ", "ain"});
13701
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
248
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
249 %!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
250 %!assert (strsplit ("road to hell", " "), {"road", "to", "hell"})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
251 %!assert (strsplit ("road to^hell", {" ","^"}), {"road", "to", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
252 %!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
253 %!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
254 %!assert (strsplit (["a,bc,,de"], ",", false), {"a", "bc", "", "de"})
16403
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
255 %!assert (strsplit (["a,bc,de"], ",", true), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
256 %!assert (strsplit (["a,bc,de"], {","," "}, true), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
257
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
258 %!assert (strsplit ("road to hell", " ", "delimitertype", "r"), {"road", "to", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
259 %!assert (strsplit ("road to^hell", '\^| ', "delimitertype", "r"), {"road", "to", "hell"})
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", "[ -]", false, "delimitertype", "r"), {"road", "", "", "to", "", "hell"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
262 %!assert (strsplit (["a,bc,de"], ",", "delimitertype", "r"), {"a", "bc", "de"})
1de4ec2a856d Matlab compatibility for strsplit()
Ben Abbott <bpabbott@mac.com>
parents: 15521
diff changeset
263 %!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
264 %!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
265 %!assert (strsplit (["a,bc,de"], "[, ]", true, "delimitertype", "r"), {"a", "bc", "de"})
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
266 %!assert (strsplit ("hello \t world", true, "delimitertype", "r"), {"hello", "world"});
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
267
16557
d50bca1cdc22 Support escaped characters in sq_strings for strsplit.m.
Ben Abbott <bpabbott@mac.com>
parents: 16554
diff changeset
268 %!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
269 %!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
270
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
271 ## Test "match" for consecutive delmiters
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
272 %!test
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
273 %! [a, m] = strsplit ("a\t \nb", '\s', "delimitertype", "regularexpression",
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
274 %! "collapsedelimiters", false);
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
275 %! assert (a, {"a", "", "", "b"})
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
276 %! assert (m, {"\t", " ", "\n"})
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
277 %!test
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
278 %! [a, m] = strsplit ("a\t \nb", '\s', false, "delimitertype", "regularexpression");
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
279 %! assert (a, {"a", "", "", "b"})
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
280 %! assert (m, {"\t", " ", "\n"})
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
281 %!test
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
282 %! [a, m] = strsplit ("a\t \nb", '\s', "delimitertype", "regularexpression");
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
283 %! assert (a, {"a", "b"})
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
284 %! assert (m, {"\t \n"})
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
285 %!test
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
286 %! [a, m] = strsplit ("a\t \nb", {"\t", " ", "\n"}, "delimitertype", "simple");
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
287 %! assert (a, {"a", "b"})
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
288 %! assert (m, {"\t \n"})
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
289 %!test
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
290 %! [s, m] = strsplit ("hello \t world", true);
16554
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
291 %! assert (s, {"hello", "world"});
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
292 %! assert (m, {" \t "});
03a28487fa9d Collect collapsed delimiters in strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16411
diff changeset
293
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
294 ## Compatibility
16727
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
295 %! assert (strsplit ("", "a"), {""})
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
296 %! assert (strsplit ("a", "a"), {"", ""})
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
297 %! assert (strsplit ("aa", "a"), {"", ""})
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
298 %! assert (strsplit ("aaa", "a"), {"", ""})
3ce3579405f8 Remove the "legacy" delimiter type from strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16712
diff changeset
299
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
300 ## Bug #44641
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
301 %!assert (strsplit ("xxx<yyy", "<"), {"xxx", "yyy"})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
302 %!assert (strsplit ('xxx\yyy', '\'), {"xxx", "yyy"})
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
303
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
304 ## 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
305 %!error strsplit ()
46e68badedb8 strsplit.m: Expand to accept 2-D character arrays. Improve input validation.
Rik <octave@nomad.inbox5.com>
parents: 12915
diff changeset
306 %!error strsplit ("abc", "b", true, 4)
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
307 %!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
308 %!error <S and DEL must be string values> strsplit (123, "b")
20024
7569e880d56c strsplit.m: Overhaul function and fix bug #44641.
Rik <rik@octave.org>
parents: 19867
diff changeset
309 %!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: 19867
diff changeset
310 %!error <Invalid DELIMITERTYPE> strsplit ("abc", "b", "delimitertype", "foobar")
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
311