1
## Copyright (C) 2009-2012 Jaroslav Hajek
3
## This file is part of Octave.
5
## Octave is free software; you can redistribute it and/or modify it
6
## under the terms of the GNU General Public License as published by
7
## the Free Software Foundation; either version 3 of the License, or (at
8
## your option) any later version.
10
## Octave is distributed in the hope that it will be useful, but
11
## WITHOUT ANY WARRANTY; without even the implied warranty of
12
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
## General Public License for more details.
15
## You should have received a copy of the GNU General Public License
16
## along with Octave; see the file COPYING. If not, see
17
## <http://www.gnu.org/licenses/>.
19
## FIXME: this file is here to avoid conflicts with new Octave versions. Matlab
20
## has recently added strplit function (used to exist in Octave only) but
21
## their syntax is not compatible with ours. Rather than timing the
22
## release of each octave forge package that used strsplit, a copy of the
23
## old version was placed as private. Once the new Octave version is
24
## released, this file can be removed, and the calls to strsplit fixed.
27
## @deftypefn {Function File} {[@var{cstr}] =} strsplit (@var{s}, @var{sep})
28
## @deftypefnx {Function File} {[@var{cstr}] =} strsplit (@var{s}, @var{sep}, @var{strip_empty})
29
## Split the string @var{s} using one or more separators @var{sep} and return
30
## a cell array of strings. Consecutive separators and separators at
31
## boundaries result in empty strings, unless @var{strip_empty} is true.
32
## The default value of @var{strip_empty} is false.
34
## 2-D character arrays are split at separators and at the original column
41
## strsplit ("a,b,c", ",")
49
## strsplit (["a,b" ; "cde"], ",")
61
function cstr = strsplit (s, sep, strip_empty = false)
63
if (nargin < 2 || nargin > 3)
65
elseif (! ischar (s) || ! ischar (sep))
66
error ("strsplit: S and SEP must be string values");
67
elseif (! isscalar (strip_empty))
68
error ("strsplit: STRIP_EMPTY must be a scalar value");
72
cstr = cell (size (s));
75
## For 2-D arrays, add separator character at line boundaries
76
## and transform to single string
78
s = reshape (s.', 1, numel (s));
82
## Split s according to delimiter
85
idx = find (s == sep);
87
## Multiple separators
88
idx = strchr (s, sep);
91
## Get substring lengths.
95
strlens = [idx(1)-1, diff(idx)-1, numel(s)-idx(end)];
100
## Omit zero lengths.
101
strlens = strlens(strlens != 0);
105
cstr = mat2cell (s, 1, strlens);
111
%!assert (strsplit ("road to hell", " "), {"road", "to", "hell"})
112
%!assert (strsplit ("road to^hell", " ^"), {"road", "to", "hell"})
113
%!assert (strsplit ("road to--hell", " -", true), {"road", "to", "hell"})
114
%!assert (strsplit (["a,bc";",de"], ","), {"a", "bc", char(ones(1,0)), "de "})
115
%!assert (strsplit (["a,bc";",de"], ",", true), {"a", "bc", "de "})
116
%!assert (strsplit (["a,bc";",de"], ", ", true), {"a", "bc", "de"})
118
%% Test input validation
120
%!error strsplit ("abc")
121
%!error strsplit ("abc", "b", true, 4)
122
%!error <S and SEP must be string values> strsplit (123, "b")
123
%!error <S and SEP must be string values> strsplit ("abc", 1)
124
%!error <STRIP_EMPTY must be a scalar value> strsplit ("abc", "def", ones (3,3))