annotate scripts/strings/ostrsplit.m @ 20654:b65888ec820e draft default tip gccjit

dmalcom gcc jit import
author Stefan Mahr <dac922@gmx.de>
date Fri, 27 Feb 2015 16:59:36 +0100
parents df437a52bcaf
children
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
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
2 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
3 ## This file is part of Octave.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
4 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## your option) any later version.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
10 ## 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
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
13 ## General Public License for more details.
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 ## 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
16 ## along with Octave; see the file COPYING. If not, see
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
18
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19 ## -*- texinfo -*-
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
20 ## @deftypefn {Function File} {[@var{cstr}] =} ostrsplit (@var{s}, @var{sep})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
21 ## @deftypefnx {Function File} {[@var{cstr}] =} ostrsplit (@var{s}, @var{sep}, @var{strip_empty})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
22 ## Split the string @var{s} using one or more separators @var{sep} and return
20199
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
23 ## a cell array of strings.
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
24 ##
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
25 ## 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: 19867
diff changeset
26 ## 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: 19867
diff changeset
27 ## @var{strip_empty} is false.
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
28 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
29 ## 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
30 ## boundaries.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
31 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
32 ## Example:
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
33 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
34 ## @example
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
35 ## @group
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
36 ## ostrsplit ("a,b,c", ",")
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 ## @result{}
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 ## [1,1] = a
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
40 ## [1,2] = b
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
41 ## [1,3] = c
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
42 ## @}
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 ##
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
44 ## ostrsplit (["a,b" ; "cde"], ",")
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
45 ## @result{}
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
46 ## @{
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
47 ## [1,1] = a
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 ## [1,2] = b
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49 ## [1,3] = cde
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 ## @end group
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
52 ## @end example
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53 ## @seealso{strsplit, strtok}
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
54 ## @end deftypefn
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
55
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 function cstr = ostrsplit (s, sep, strip_empty = false)
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 if (nargin < 2 || nargin > 3)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
59 print_usage ();
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
60 elseif (! ischar (s) || ! ischar (sep))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
61 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
62 elseif (! isscalar (strip_empty))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 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
64 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
66 if (isempty (s))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
67 cstr = cell (size (s));
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
68 else
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
69 if (rows (s) > 1)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
70 ## 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
71 ## and transform to single string
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72 s(:, end+1) = sep(1);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 s = reshape (s.', 1, numel (s));
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
74 s(end) = [];
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 ## Split s according to delimiter
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78 if (isscalar (sep))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79 ## Single separator
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80 idx = find (s == sep);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
81 else
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
82 ## Multiple separators
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
83 idx = strchr (s, sep);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
84 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
85
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
86 ## Get substring lengths.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
87 if (isempty (idx))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
88 strlens = length (s);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
89 else
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
90 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
91 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
92 ## Remove separators.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
93 s(idx) = [];
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
94 if (strip_empty)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
95 ## Omit zero lengths.
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
96 strlens = strlens(strlens != 0);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
97 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
98
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
99 ## Convert!
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
100 cstr = mat2cell (s, 1, strlens);
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
101 endif
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
102
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
103 endfunction
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
104
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 %!assert (ostrsplit ("road to hell", " "), {"road", "to", "hell"})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
107 %!assert (ostrsplit ("road to^hell", " ^"), {"road", "to", "hell"})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 %!assert (ostrsplit ("road to--hell", " -", true), {"road", "to", "hell"})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109 %!assert (ostrsplit (["a,bc";",de"], ","), {"a", "bc", char(ones(1,0)), "de "})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
110 %!assert (ostrsplit (["a,bc";",de"], ",", true), {"a", "bc", "de "})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
111 %!assert (ostrsplit (["a,bc";",de"], ", ", true), {"a", "bc", "de"})
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
112
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
113 ## Test input validation
16723
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
114 %!error ostrsplit ()
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
115 %!error ostrsplit ("abc")
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
116 %!error ostrsplit ("abc", "b", true, 4)
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
117 %!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
118 %!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
119 %!error <STRIP_EMPTY must be a scalar value> ostrsplit ("abc", "def", ones (3,3))
45b57ac44854 Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
120