annotate scripts/deprecated/split.m @ 12728:a17269b1148f stable

maint: undo unintended change removing deprecated functions
author John W. Eaton <jwe@octave.org>
date Thu, 09 Jun 2011 13:35:10 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12728
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 ## Copyright (C) 1996-2011 Kurt Hornik
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 ##
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 ## This file is part of Octave.
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 ##
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 ## your option) any later version.
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 ##
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ## General Public License for more details.
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ##
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 ## -*- texinfo -*-
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 ## @deftypefn {Function File} {} split (@var{s}, @var{t}, @var{n})
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 ## This function has been deprecated. Use @code{char (strsplit (s, t))}
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 ## instead.
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 ## @end deftypefn
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 ## Divides the string @var{s} into pieces separated by @var{t}, returning
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 ## the result in a string array (padded with blanks to form a valid
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 ## matrix). If the optional input @var{n} is supplied, split @var{s}
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 ## into at most @var{n} different pieces.
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 ##
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 ## For example,
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 ##
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 ## @example
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 ## split ("Test string", "t")
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 ## @result{}
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 ## "Tes "
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 ## " s "
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 ## "ring"
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 ## @end example
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 ##
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 ## @example
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 ## split ("Test string", "t s", 2)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 ## @result{}
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 ## "Tes "
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 ## "tring"
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 ## @end example
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 ## @seealso{strtok, index}
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 ## @end deftypefn
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 ## Adapted-By: jwe
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 ## Deprecated in version 3.2
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 function m = split (s, t, n)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 persistent warned = false;
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 if (! warned)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 warned = true;
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 warning ("Octave:deprecated-function",
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 "split is obsolete and will be removed from a future version of Octave; please use strsplit instead");
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 endif
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 if (nargin == 2 || nargin == 3)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 if (nargin == 2)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 n = length (s);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 endif
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 if (ischar (s) && ischar (t))
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 l_s = length (s);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 l_t = length (t);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 if (l_s == 0)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 m = "";
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 return;
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 elseif (l_t == 0)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 m = s';
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 return;
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 elseif (l_s < l_t)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 error ("split: S must not be shorter than T");
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 endif
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 if (min (size (s)) != 1 || min (size (t)) != 1)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 error("split: multi-line strings are not supported");
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 endif
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 ind = findstr (s, t, 0);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 if (length (ind) == 0)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 m = s;
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 return;
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 elseif (n - 1 < length(ind))
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 ind = ind(1:n-1);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 endif
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 ind2 = [1, ind+l_t];
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 ind = [ind, l_s+1];
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 ind_diff = ind-ind2;
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 ## Create a matrix of the correct size that's filled with spaces.
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 m_rows = length (ind);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 m_cols = max (ind_diff);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102 m = repmat (" ", m_rows, m_cols);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 ## Copy the strings to the matrix.
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105 for i = 1:length (ind)
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 tmp = ind2(i):(ind(i)-1);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 m(i,1:length(tmp)) = s(tmp);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 endfor
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 else
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110 error ("split: both S and T must be strings");
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 endif
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 else
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113 print_usage ();
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
114 endif
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116 endfunction
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
117
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118 %!assert(all (all (split ("Test string", "t") == ["Tes "; " s "; "ring"])));
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120 %!error split ();
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
122 %!assert(all (strcmp (split ("foo bar baz", " ", 2), ["foo"; "bar baz"])));
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
123
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
124 %!error split ("foo", "bar", 3, 4);
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
125
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
126 %!assert (all (strcmp (split("road//to/hell","/"), ["road"; " "; "to "; "hell"])))
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
127
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
128 %!assert (all (strcmp (split("/road/to/hell/","/"), [" "; "road"; "to "; "hell"; " "])))
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
129
a17269b1148f maint: undo unintended change removing deprecated functions
John W. Eaton <jwe@octave.org>
parents:
diff changeset
130