annotate scripts/strings/ostrsplit.m @ 29358:0a5b15007766 stable

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2021.
author John W. Eaton <jwe@octave.org>
date Wed, 10 Feb 2021 09:52:15 -0500
parents bd51beb6205e
children 7854d5752dd2
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 ##
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
3 ## Copyright (C) 2009-2021 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## This file is part of Octave.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24461
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.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: 24461
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.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: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
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: 24461
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 ########################################################################
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
25
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.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: 20164
diff changeset
27 ## @deftypefn {} {[@var{cstr}] =} ostrsplit (@var{s}, @var{sep})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20164
diff changeset
28 ## @deftypefnx {} {[@var{cstr}] =} ostrsplit (@var{s}, @var{sep}, @var{strip_empty})
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
29 ## Split the string @var{s} using one or more separators @var{sep} and return
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## a cell array of strings.
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ##
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ## Consecutive separators and separators at boundaries result in empty
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## strings, unless @var{strip_empty} is true. The default value of
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## @var{strip_empty} is false.
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
35 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
36 ## 2-D character arrays are split at separators and at the original column
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 ## boundaries.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
38 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
39 ## Example:
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
40 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
41 ## @example
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
42 ## @group
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 ## ostrsplit ("a,b,c", ",")
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
44 ## @result{}
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
45 ## @{
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
46 ## [1,1] = a
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
47 ## [1,2] = b
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 ## [1,3] = c
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49 ## @}
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
50 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
51 ## ostrsplit (["a,b" ; "cde"], ",")
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
52 ## @result{}
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53 ## @{
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
54 ## [1,1] = a
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
55 ## [1,2] = b
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 ## [1,3] = cde
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 ## @}
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
58 ## @end group
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
59 ## @end example
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
60 ## @seealso{strsplit, strtok}
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
61 ## @end deftypefn
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
62
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 function cstr = ostrsplit (s, sep, strip_empty = false)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
64
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65 if (nargin < 2 || nargin > 3)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
66 print_usage ();
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
67 elseif (! ischar (s) || ! ischar (sep))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
68 error ("ostrsplit: S and SEP must be string values");
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
69 elseif (! isscalar (strip_empty))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
70 error ("ostrsplit: STRIP_EMPTY must be a scalar value");
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
71 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 if (isempty (s))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
74 cstr = cell (size (s));
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 else
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76 if (rows (s) > 1)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 ## For 2-D arrays, add separator character at line boundaries
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78 ## and transform to single string
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79 s(:, end+1) = sep(1);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80 s = reshape (s.', 1, numel (s));
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
81 s(end) = [];
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
82 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
83
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
84 ## Split s according to delimiter
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
85 if (isscalar (sep))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
86 ## Single separator
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
87 idx = find (s == sep);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
88 else
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
89 ## Multiple separators
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
90 idx = strchr (s, sep);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
91 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
92
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
93 ## Get substring lengths.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
94 if (isempty (idx))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
95 strlens = length (s);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
96 else
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
97 strlens = [idx(1)-1, diff(idx)-1, numel(s)-idx(end)];
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
98 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
99 ## Remove separators.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
100 s(idx) = [];
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
101 if (strip_empty)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
102 ## Omit zero lengths.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
103 strlens = strlens(strlens != 0);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
104 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
105
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
106 ## Convert!
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
107 cstr = mat2cell (s, 1, strlens);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
110 endfunction
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
111
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
112
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
113 %!assert (ostrsplit ("road to hell", " "), {"road", "to", "hell"})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
114 %!assert (ostrsplit ("road to^hell", " ^"), {"road", "to", "hell"})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
115 %!assert (ostrsplit ("road to--hell", " -", true), {"road", "to", "hell"})
24461
0a034d34a686 Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents: 23220
diff changeset
116 %!assert (ostrsplit (char ("a,bc", ",de"), ","),
0a034d34a686 Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents: 23220
diff changeset
117 %! {"a", "bc", char(ones(1,0)), "de "})
0a034d34a686 Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents: 23220
diff changeset
118 %!assert (ostrsplit (char ("a,bc", ",de"), ",", true), {"a", "bc", "de "})
0a034d34a686 Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents: 23220
diff changeset
119 %!assert (ostrsplit (char ("a,bc", ",de"), ", ", true), {"a", "bc", "de"})
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
120
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
121 ## Test input validation
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
122 %!error ostrsplit ()
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
123 %!error ostrsplit ("abc")
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
124 %!error ostrsplit ("abc", "b", true, 4)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
125 %!error <S and SEP must be string values> ostrsplit (123, "b")
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
126 %!error <S and SEP must be string values> ostrsplit ("abc", 1)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
127 %!error <STRIP_EMPTY must be a scalar value> ostrsplit ("abc", "def", ones (3,3))