Mercurial > hg > octave-nkf
changeset 5219:96661dd79291
[project @ 2005-03-16 20:44:39 by jwe]
author | jwe |
---|---|
date | Wed, 16 Mar 2005 20:44:39 +0000 |
parents | 4d036412ccca |
children | 42f9a021aac1 |
files | scripts/strings/split.m |
diffstat | 1 files changed, 44 insertions(+), 43 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/strings/split.m +++ b/scripts/strings/split.m @@ -36,50 +36,51 @@ function m = split (s, t) - if (nargin != 2) + if (nargin == 2) + if (isstr (s) && isstr (t)) + + l_s = length (s); + l_t = length (t); + + if (l_s == 0) + m = ""; + return; + elseif (l_t == 0) + m = s'; + return; + elseif (l_s < l_t) + error ("split: s must not be shorter than t"); + endif + + if (min (size (s)) != 1 || min (size (t)) != 1) + error("split: multi-line strings are not supported"); + endif + + ind = findstr (s, t, 0); + if (length (ind) == 0) + m = s; + return; + endif + ind2 = [1, ind+l_t]; + ind = [ind, l_s+1]; + + ind_diff = ind-ind2; + + ## Create a matrix of the correct size that's filled with spaces. + m_rows = length (ind); + m_cols = max (ind_diff); + m = repmat (" ", m_rows, m_cols); + + ## Copy the strings to the matrix. + for i = 1:length (ind) + tmp = ind2(i):(ind(i)-1); + m(i,1:length(tmp)) = s(tmp); + endfor + else + error ("split: both s and t must be strings"); + endif + else usage ("split (s, t)"); endif - if not(ischar (s) && ischar (t)) - error ("split: both s and t must be strings"); - endif - - - l_s = length (s); - l_t = length (t); - - if (l_s == 0) - m = ""; - return; - elseif (l_t == 0) - m = s'; - return; - elseif (l_s < l_t) - error ("split: s must not be shorter than t"); - endif - - if (min(size(s)) ~= 1 | min(size(t)) ~= 1) - error("split: multible strings are not supported"); - endif - - ind = findstr (s, t, 0); - if (length (ind) == 0) - m = s; - return; - endif - ind2 = [1, ind+l_t]; - ind = [ind, l_s+1]; - - ind_diff = ind-ind2; - % Create a matrix of the correct size that's filled with spaces - m_rows = length(ind); - m_cols = max(ind_diff); - m = char( zeros(m_rows, m_cols) + ' ' ); - - % Copy the strings to the matrix - for i = 1:length(ind) - tmp = ind2(i):(ind(i)-1); - m(i, 1:length(tmp)) = s(tmp); - end - endfunction