# HG changeset patch # User jwe # Date 1111005879 0 # Node ID 96661dd7929122ade55081cf75bb248ea2ac0e28 # Parent 4d036412cccaf825418ac71efbf6250601513392 [project @ 2005-03-16 20:44:39 by jwe] diff -r 4d036412ccca -r 96661dd79291 scripts/strings/split.m --- a/scripts/strings/split.m Wed Mar 16 20:30:04 2005 +0000 +++ b/scripts/strings/split.m Wed Mar 16 20:44:39 2005 +0000 @@ -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