Mercurial > hg > octave-nkf
annotate scripts/deprecated/split.m @ 8877:2c8b2399247b
implement strsplit; deprecate split
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 26 Feb 2009 10:29:59 +0100 |
parents | scripts/strings/split.m@502e58a0d44f |
children | ebb8c1dcf4d3 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1996, 1999, 2000, 2005, 2006, 2007 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}) |
8877
2c8b2399247b
implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents:
8442
diff
changeset
|
21 ## This function has been deprecated. Use @code{char (strsplit (s, t))} |
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 |
5462 | 52 function m = split (s, t, n) |
2325 | 53 |
5462 | 54 if (nargin == 2 || nargin == 3) |
55 if (nargin == 2) | |
56 n = length (s); | |
57 endif | |
58 | |
5443 | 59 if (ischar (s) && ischar (t)) |
5219 | 60 |
61 l_s = length (s); | |
62 l_t = length (t); | |
63 | |
64 if (l_s == 0) | |
65 m = ""; | |
66 return; | |
67 elseif (l_t == 0) | |
68 m = s'; | |
69 return; | |
70 elseif (l_s < l_t) | |
71 error ("split: s must not be shorter than t"); | |
72 endif | |
73 | |
74 if (min (size (s)) != 1 || min (size (t)) != 1) | |
75 error("split: multi-line strings are not supported"); | |
76 endif | |
77 | |
78 ind = findstr (s, t, 0); | |
79 if (length (ind) == 0) | |
80 m = s; | |
81 return; | |
5462 | 82 elseif (n - 1 < length(ind)) |
5473 | 83 ind = ind(1:n-1); |
5219 | 84 endif |
85 ind2 = [1, ind+l_t]; | |
86 ind = [ind, l_s+1]; | |
87 | |
88 ind_diff = ind-ind2; | |
89 | |
90 ## Create a matrix of the correct size that's filled with spaces. | |
91 m_rows = length (ind); | |
92 m_cols = max (ind_diff); | |
93 m = repmat (" ", m_rows, m_cols); | |
94 | |
95 ## Copy the strings to the matrix. | |
96 for i = 1:length (ind) | |
97 tmp = ind2(i):(ind(i)-1); | |
98 m(i,1:length(tmp)) = s(tmp); | |
99 endfor | |
100 else | |
101 error ("split: both s and t must be strings"); | |
102 endif | |
103 else | |
6046 | 104 print_usage (); |
2274 | 105 endif |
2325 | 106 |
2274 | 107 endfunction |
7411 | 108 |
109 %!assert(all (all (split ("Test string", "t") == ["Tes "; " s "; "ring"]))); | |
110 | |
111 %!error split (); | |
112 | |
113 %!assert(all (strcmp (split ("foo bar baz", " ", 2), ["foo"; "bar baz"]))); | |
114 | |
115 %!error split ("foo", "bar", 3, 4); | |
116 | |
8168
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
117 %!assert (all (strcmp (split("road//to/hell","/"), ["road"; " "; "to "; "hell"]))) |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
118 |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
119 %!assert (all (strcmp (split("/road/to/hell/","/"), [" "; "road"; "to "; "hell"; " "]))) |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
120 |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
121 |