Mercurial > hg > octave-nkf
diff scripts/strings/split.m @ 5218:4d036412ccca
[project @ 2005-03-16 20:30:04 by jwe]
author | jwe |
---|---|
date | Wed, 16 Mar 2005 20:30:04 +0000 |
parents | d25bc039237b |
children | 96661dd79291 |
line wrap: on
line diff
--- a/scripts/strings/split.m +++ b/scripts/strings/split.m @@ -40,7 +40,10 @@ usage ("split (s, t)"); endif - if (isstr (s) && isstr (t)) + if not(ischar (s) && ischar (t)) + error ("split: both s and t must be strings"); + endif + l_s = length (s); l_t = length (t); @@ -48,41 +51,35 @@ 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 (l_t == 0) - ind = 1 : (l_s + 1); - else - ind = findstr (s, t, 0); - if (length (ind) == 0) - m = s; - return; - endif - ind = [1 - l_t, ind, l_s + 1]; + + if (min(size(s)) ~= 1 | min(size(t)) ~= 1) + error("split: multible strings are not supported"); endif - cmd = ""; - - limit = length (ind) - 1; - - for k = 1 : limit - - range = (ind (k) + l_t) : ind (k + 1) - 1; + ind = findstr (s, t, 0); + if (length (ind) == 0) + m = s; + return; + endif + ind2 = [1, ind+l_t]; + ind = [ind, l_s+1]; - if (k != limit) - cmd = sprintf ("%s\"%s\", ", cmd, undo_string_escapes (s (range))); - else - cmd = sprintf ("%s\"%s\"", cmd, undo_string_escapes (s (range))); - endif + 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) + ' ' ); - endfor - - m = eval (sprintf ("str2mat (%s);", cmd)); - - else - error ("split: both s and t must be strings"); - endif + % 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