Mercurial > hg > octave-lyh
annotate scripts/strings/split.m @ 8442:502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Mon, 05 Jan 2009 08:11:03 +0100 |
parents | dadf478ddc42 |
children |
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}) |
3361 | 21 ## Divides the string @var{s} into pieces separated by @var{t}, returning |
22 ## the result in a string array (padded with blanks to form a valid | |
5462 | 23 ## matrix). If the optional input @var{n} is supplied, split @var{s} |
24 ## into at most @var{n} different pieces. | |
25 ## | |
26 ## For example, | |
3426 | 27 ## |
3361 | 28 ## @example |
29 ## 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
|
30 ## @result{} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
31 ## "Tes " |
3361 | 32 ## " s " |
33 ## "ring" | |
34 ## @end example | |
5462 | 35 ## |
36 ## @example | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
37 ## 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
|
38 ## @result{} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
39 ## "Tes " |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
40 ## "tring" |
5462 | 41 ## @end example |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8168
diff
changeset
|
42 ## @seealso{strtok, index} |
3361 | 43 ## @end deftypefn |
2311 | 44 |
5428 | 45 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355 | 46 ## Adapted-By: jwe |
2314 | 47 |
5462 | 48 function m = split (s, t, n) |
2325 | 49 |
5462 | 50 if (nargin == 2 || nargin == 3) |
51 if (nargin == 2) | |
52 n = length (s); | |
53 endif | |
54 | |
5443 | 55 if (ischar (s) && ischar (t)) |
5219 | 56 |
57 l_s = length (s); | |
58 l_t = length (t); | |
59 | |
60 if (l_s == 0) | |
61 m = ""; | |
62 return; | |
63 elseif (l_t == 0) | |
64 m = s'; | |
65 return; | |
66 elseif (l_s < l_t) | |
67 error ("split: s must not be shorter than t"); | |
68 endif | |
69 | |
70 if (min (size (s)) != 1 || min (size (t)) != 1) | |
71 error("split: multi-line strings are not supported"); | |
72 endif | |
73 | |
74 ind = findstr (s, t, 0); | |
75 if (length (ind) == 0) | |
76 m = s; | |
77 return; | |
5462 | 78 elseif (n - 1 < length(ind)) |
5473 | 79 ind = ind(1:n-1); |
5219 | 80 endif |
81 ind2 = [1, ind+l_t]; | |
82 ind = [ind, l_s+1]; | |
83 | |
84 ind_diff = ind-ind2; | |
85 | |
86 ## Create a matrix of the correct size that's filled with spaces. | |
87 m_rows = length (ind); | |
88 m_cols = max (ind_diff); | |
89 m = repmat (" ", m_rows, m_cols); | |
90 | |
91 ## Copy the strings to the matrix. | |
92 for i = 1:length (ind) | |
93 tmp = ind2(i):(ind(i)-1); | |
94 m(i,1:length(tmp)) = s(tmp); | |
95 endfor | |
96 else | |
97 error ("split: both s and t must be strings"); | |
98 endif | |
99 else | |
6046 | 100 print_usage (); |
2274 | 101 endif |
2325 | 102 |
2274 | 103 endfunction |
7411 | 104 |
105 %!assert(all (all (split ("Test string", "t") == ["Tes "; " s "; "ring"]))); | |
106 | |
107 %!error split (); | |
108 | |
109 %!assert(all (strcmp (split ("foo bar baz", " ", 2), ["foo"; "bar baz"]))); | |
110 | |
111 %!error split ("foo", "bar", 3, 4); | |
112 | |
8168
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
113 %!assert (all (strcmp (split("road//to/hell","/"), ["road"; " "; "to "; "hell"]))) |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
114 |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
115 %!assert (all (strcmp (split("/road/to/hell/","/"), [" "; "road"; "to "; "hell"; " "]))) |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
116 |
dadf478ddc42
fix empty string assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
117 |