Mercurial > hg > octave-nkf
annotate scripts/deprecated/split.m @ 12107:1fc9fd052f0c release-3-2-x
fix typo in expm
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 25 Nov 2009 12:05:03 +0100 |
parents | 923c7cb7f13f |
children | 95c3e38098bf |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1996, 1999, 2000, 2005, 2006, 2007, 2009 Kurt Hornik |
2325 | 2 ## |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2274 | 18 |
3361 | 19 ## -*- texinfo -*- |
5462 | 20 ## @deftypefn {Function File} {} split (@var{s}, @var{t}, @var{n}) |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
21 ## This function has been deprecated. Use @code{char (strsplit (s, t))} |
8877
2c8b2399247b
implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
8442
diff
changeset
|
22 ## instead. |
2c8b2399247b
implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
8442
diff
changeset
|
23 ## @end deftypefn |
2c8b2399247b
implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
8442
diff
changeset
|
24 |
3361 | 25 ## Divides the string @var{s} into pieces separated by @var{t}, returning |
26 ## the result in a string array (padded with blanks to form a valid | |
5462 | 27 ## matrix). If the optional input @var{n} is supplied, split @var{s} |
28 ## into at most @var{n} different pieces. | |
29 ## | |
30 ## For example, | |
3426 | 31 ## |
3361 | 32 ## @example |
33 ## split ("Test string", "t") | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
34 ## @result{} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
35 ## "Tes " |
3361 | 36 ## " s " |
37 ## "ring" | |
38 ## @end example | |
5462 | 39 ## |
40 ## @example | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
41 ## split ("Test string", "t s", 2) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
42 ## @result{} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
43 ## "Tes " |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
44 ## "tring" |
5462 | 45 ## @end example |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
46 ## @seealso{strtok, index} |
3361 | 47 ## @end deftypefn |
2311 | 48 |
5428 | 49 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355 | 50 ## Adapted-By: jwe |
2314 | 51 |
8878
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
52 ## Deprecated in version 3.2 |
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
53 |
5462 | 54 function m = split (s, t, n) |
2325 | 55 |
8878
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
56 persistent warned = false; |
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
57 if (! warned) |
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
58 warned = true; |
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
59 warning ("Octave:deprecated-function", |
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
60 "split is obsolete and will be removed from a future version of Octave; please use strsplit instead"); |
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
61 endif |
ebb8c1dcf4d3
split.m: add warning, note version
John W. Eaton <jwe@octave.org>
parents:
8877
diff
changeset
|
62 |
5462 | 63 if (nargin == 2 || nargin == 3) |
64 if (nargin == 2) | |
65 n = length (s); | |
66 endif | |
67 | |
5443 | 68 if (ischar (s) && ischar (t)) |
5219 | 69 |
70 l_s = length (s); | |
71 l_t = length (t); | |
72 | |
73 if (l_s == 0) | |
74 m = ""; | |
75 return; | |
76 elseif (l_t == 0) | |
77 m = s'; | |
78 return; | |
79 elseif (l_s < l_t) | |
80 error ("split: s must not be shorter than t"); | |
81 endif | |
82 | |
83 if (min (size (s)) != 1 || min (size (t)) != 1) | |
84 error("split: multi-line strings are not supported"); | |
85 endif | |
86 | |
87 ind = findstr (s, t, 0); | |
88 if (length (ind) == 0) | |
89 m = s; | |
90 return; | |
5462 | 91 elseif (n - 1 < length(ind)) |
5473 | 92 ind = ind(1:n-1); |
5219 | 93 endif |
94 ind2 = [1, ind+l_t]; | |
95 ind = [ind, l_s+1]; | |
96 | |
97 ind_diff = ind-ind2; | |
98 | |
99 ## Create a matrix of the correct size that's filled with spaces. | |
100 m_rows = length (ind); | |
101 m_cols = max (ind_diff); | |
102 m = repmat (" ", m_rows, m_cols); | |
103 | |
104 ## Copy the strings to the matrix. | |
105 for i = 1:length (ind) | |
106 tmp = ind2(i):(ind(i)-1); | |
107 m(i,1:length(tmp)) = s(tmp); | |
108 endfor | |
109 else | |
110 error ("split: both s and t must be strings"); | |
111 endif | |
112 else | |
6046 | 113 print_usage (); |
2274 | 114 endif |
2325 | 115 |
2274 | 116 endfunction |
7411 | 117 |
118 %!assert(all (all (split ("Test string", "t") == ["Tes "; " s "; "ring"]))); | |
119 | |
120 %!error split (); | |
121 | |
122 %!assert(all (strcmp (split ("foo bar baz", " ", 2), ["foo"; "bar baz"]))); | |
123 | |
124 %!error split ("foo", "bar", 3, 4); | |
125 | |
8168
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
126 %!assert (all (strcmp (split("road//to/hell","/"), ["road"; " "; "to "; "hell"]))) |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
127 |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
128 %!assert (all (strcmp (split("/road/to/hell/","/"), [" "; "road"; "to "; "hell"; " "]))) |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
129 |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
130 |