Mercurial > octave-nkf
annotate scripts/strings/ostrsplit.m @ 16723:45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
The original was modified for compatibility in changeset 1de4ec2a856d.
Bug #39010.
* strings/ostrsplit.m: New file.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Wed, 05 Jun 2013 20:45:41 +0800 |
parents | |
children | d63878346099 |
rev | line source |
---|---|
16723
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
1 ## Copyright (C) 2009-2012 Jaroslav Hajek |
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 |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
23 ## a cell array of strings. Consecutive separators and separators at |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
24 ## boundaries result in empty strings, unless @var{strip_empty} is true. |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
25 ## The default value of @var{strip_empty} is false. |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
26 ## |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
27 ## 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
|
28 ## boundaries. |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
29 ## |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
30 ## Example: |
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 ## @group |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
34 ## ostrsplit ("a,b,c", ",") |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
35 ## @result{} |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
36 ## @{ |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
37 ## [1,1] = a |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
38 ## [1,2] = b |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
39 ## [1,3] = c |
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 ## |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
42 ## ostrsplit (["a,b" ; "cde"], ",") |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
43 ## @result{} |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
44 ## @{ |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
45 ## [1,1] = a |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
46 ## [1,2] = b |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
47 ## [1,3] = cde |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
48 ## @} |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
49 ## @end group |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
50 ## @end example |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
51 ## @seealso{strsplit, strtok} |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
52 ## @end deftypefn |
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 function cstr = ostrsplit (s, sep, strip_empty = false) |
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 if (nargin < 2 || nargin > 3) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
57 print_usage (); |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
58 elseif (! ischar (s) || ! ischar (sep)) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
59 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
|
60 elseif (! isscalar (strip_empty)) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
61 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
|
62 endif |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
63 |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
64 if (isempty (s)) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
65 cstr = cell (size (s)); |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
66 else |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
67 if (rows (s) > 1) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
68 ## 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
|
69 ## and transform to single string |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
70 s(:, end+1) = sep(1); |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
71 s = reshape (s.', 1, numel (s)); |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
72 s(end) = []; |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
73 endif |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
74 |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
75 ## Split s according to delimiter |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
76 if (isscalar (sep)) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
77 ## Single separator |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
78 idx = find (s == sep); |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
79 else |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
80 ## Multiple separators |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
81 idx = strchr (s, sep); |
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 ## Get substring lengths. |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
85 if (isempty (idx)) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
86 strlens = length (s); |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
87 else |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
88 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
|
89 endif |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
90 ## Remove separators. |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
91 s(idx) = []; |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
92 if (strip_empty) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
93 ## Omit zero lengths. |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
94 strlens = strlens(strlens != 0); |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
95 endif |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
96 |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
97 ## Convert! |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
98 cstr = mat2cell (s, 1, strlens); |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
99 endif |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
100 |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
101 endfunction |
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 |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
104 %!assert (ostrsplit ("road to hell", " "), {"road", "to", "hell"}) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
105 %!assert (ostrsplit ("road to^hell", " ^"), {"road", "to", "hell"}) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
106 %!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
|
107 %!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
|
108 %!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
|
109 %!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
|
110 |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
111 %% Test input validation |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
112 %!error ostrsplit () |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
113 %!error ostrsplit ("abc") |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
114 %!error ostrsplit ("abc", "b", true, 4) |
45b57ac44854
Re-introduce the original strsplit() as ostrsplit().
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
115 %!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
|
116 %!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
|
117 %!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
|
118 |