Mercurial > hg > octave-nkf
changeset 5218:4d036412ccca
[project @ 2005-03-16 20:30:04 by jwe]
author | jwe |
---|---|
date | Wed, 16 Mar 2005 20:30:04 +0000 |
parents | e88886a6934d |
children | 96661dd79291 |
files | scripts/ChangeLog scripts/strings/split.m |
diffstat | 2 files changed, 33 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,9 @@ +2005-03-16 Soren Hauberg <soren@hauberg.org> + + * strings/split.m: Quick return for empty second arg. + Improve warning for multi-line strings. + Speed up by avoiding sprintf in loop and eval. + 2005-03-16 Paul Kienzle <pkienzle@users.sf.net> * polynomial/polyderiv.m : Accept a*b, a/b. Auto-reduce common terms.
--- 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