~ubuntu-branches/ubuntu/wily/octave-miscellaneous/wily

« back to all changes in this revision

Viewing changes to inst/private/strsplit.m

  • Committer: Package Import Robot
  • Author(s): Thomas Weber
  • Date: 2014-06-09 15:43:35 UTC
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20140609154335-qtyms40gdz6lrvui
Tags: upstream-1.2.1
ImportĀ upstreamĀ versionĀ 1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 2009-2012 Jaroslav Hajek
 
2
##
 
3
## This file is part of Octave.
 
4
##
 
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.
 
9
##
 
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.
 
14
##
 
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/>.
 
18
##
 
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.
 
25
 
 
26
## -*- texinfo -*-
 
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.
 
33
##
 
34
## 2-D character arrays are split at separators and at the original column
 
35
## boundaries.
 
36
##
 
37
## Example:
 
38
##
 
39
## @example
 
40
## @group
 
41
## strsplit ("a,b,c", ",")
 
42
##       @result{}
 
43
##           @{
 
44
##             [1,1] = a
 
45
##             [1,2] = b
 
46
##             [1,3] = c
 
47
##           @}
 
48
##
 
49
## strsplit (["a,b" ; "cde"], ",")
 
50
##       @result{}
 
51
##           @{
 
52
##             [1,1] = a
 
53
##             [1,2] = b
 
54
##             [1,3] = cde
 
55
##           @}
 
56
## @end group
 
57
## @end example
 
58
## @seealso{strtok}
 
59
## @end deftypefn
 
60
 
 
61
function cstr = strsplit (s, sep, strip_empty = false)
 
62
 
 
63
  if (nargin < 2 || nargin > 3)
 
64
    print_usage ();
 
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");
 
69
  endif
 
70
 
 
71
  if (isempty (s))
 
72
    cstr = cell (size (s));
 
73
  else
 
74
    if (rows (s) > 1)
 
75
      ## For 2-D arrays, add separator character at line boundaries
 
76
      ## and transform to single string
 
77
      s(:, end+1) = sep(1);
 
78
      s = reshape (s.', 1, numel (s));
 
79
      s(end) = []; 
 
80
    endif
 
81
 
 
82
    ## Split s according to delimiter
 
83
    if (isscalar (sep))
 
84
      ## Single separator
 
85
      idx = find (s == sep);
 
86
    else
 
87
      ## Multiple separators
 
88
      idx = strchr (s, sep);
 
89
    endif
 
90
 
 
91
    ## Get substring lengths.
 
92
    if (isempty (idx))
 
93
      strlens = length (s);
 
94
    else
 
95
      strlens = [idx(1)-1, diff(idx)-1, numel(s)-idx(end)];
 
96
    endif
 
97
    ## Remove separators.
 
98
    s(idx) = [];
 
99
    if (strip_empty)
 
100
      ## Omit zero lengths.
 
101
      strlens = strlens(strlens != 0);
 
102
    endif
 
103
 
 
104
    ## Convert!
 
105
    cstr = mat2cell (s, 1, strlens);
 
106
  endif
 
107
 
 
108
endfunction
 
109
 
 
110
 
 
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"})
 
117
 
 
118
%% Test input validation
 
119
%!error strsplit ()
 
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))
 
125