comparison scripts/strings/untabify.m @ 12929:135ec8155eeb

untabify.m: Place input validation first. Simplify assert tests. * untabify.m: Place input validation first. Simplify assert tests.
author Rik <octave@nomad.inbox5.com>
date Fri, 05 Aug 2011 20:22:40 -0700
parents c792872f8942
children e81ddf9cacd5
comparison
equal deleted inserted replaced
12928:0dc4d9f1366c 12929:135ec8155eeb
50 ## Author: Ben Abbott <bpabbott@mac.com> 50 ## Author: Ben Abbott <bpabbott@mac.com>
51 ## Created: 2010-10-15 51 ## Created: 2010-10-15
52 52
53 function s = untabify (t, tw = 8, dblank = false) 53 function s = untabify (t, tw = 8, dblank = false)
54 54
55 if (nargin > 0 && nargin < 4 && (ischar (t) || iscellstr (t))) 55 if (nargin < 1 || nargin > 3)
56 if (ischar (t)) 56 print_usage ();
57 s = replace_tabs (t, tw); 57 elseif (! (ischar (t) || iscellstr (t)))
58 else 58 error ("untabify: T must be a string or cellstring");
59 s = cellfun (@(str) replace_tabs (str, tw), t, "uniformoutput", false); 59 endif
60 endif 60
61 if (dblank) 61 if (ischar (t))
62 s = deblank (s); 62 s = replace_tabs (t, tw);
63 endif 63 else
64 else 64 s = cellfun (@(str) replace_tabs (str, tw), t, "uniformoutput", false);
65 print_usage (); 65 endif
66 endif 66
67 if (dblank)
68 s = deblank (s);
69 endif
67 70
68 endfunction 71 endfunction
69 72
70 function s = replace_tabs (t, tw) 73 function s = replace_tabs (t, tw)
71 if (ndims (t) == 2) 74
72 if (isempty (t)) 75 if (ndims (t) != 2)
73 s = t; 76 error ("untabify: character strings to untabify must have 2 dimensions");
74 else 77 endif
75 nr = rows (t); 78
76 sc = cell (nr, 1); 79 if (isempty (t))
77 for j = 1:nr 80 s = t;
78 n = 1:numel(t(j,:)); 81 else
79 m = find (t(j,:) == "\t"); 82 nr = rows (t);
80 t(j,m) = " "; 83 sc = cell (nr, 1);
81 for i = 1:numel(m) 84 for j = 1:nr
82 k = tw * ceil (n(m(i)) / tw); 85 n = 1:numel(t(j,:));
83 dn = k - n(m(i)); 86 m = find (t(j,:) == "\t");
84 n(m(i):end) += dn; 87 t(j,m) = " ";
85 endfor 88 for i = 1:numel(m)
86 sc{j} = blanks (n(end)); 89 k = tw * ceil (n(m(i)) / tw);
87 sc{j}(n) = t(j,:); 90 dn = k - n(m(i));
88 endfor 91 n(m(i):end) += dn;
89 s = char (sc); 92 endfor
90 endif 93 sc{j} = blanks (n(end));
91 else 94 sc{j}(n) = t(j,:);
92 error ("untabify: character strings to untabify must have 2 dimensions"); 95 endfor
93 endif 96 s = char (sc);
97 endif
98
94 endfunction 99 endfunction
100
95 101
96 %!test 102 %!test
97 %! s = untabify ("\thello\t"); 103 %! s = untabify ("\thello\t");
98 %! assert (isequal (s, horzcat (blanks(8), "hello "))) 104 %! assert (s, [blanks(8) "hello" blanks(3)]);
105
106 %!test
107 %! s = untabify ("\thello\t", 2);
108 %! assert (s, [blanks(2) "hello" blanks(1)]);
99 109
100 %!test 110 %!test
101 %! s = untabify ("\thello\t", 4, true); 111 %! s = untabify ("\thello\t", 4, true);
102 %! assert (isequal (s, horzcat (blanks(4), "hello"))) 112 %! assert (s, [blanks(4) "hello"]);
113
114 %!assert (isempty (untabify ("")))
103 115
104 %!test 116 %!test
105 %! s = untabify ("\thello\t", 2, true); 117 %! s = char (randi ([97 97+25], 3, 3));
106 %! assert (isequal (s, horzcat (blanks(2), "hello"))) 118 %! assert (untabify (s), char (untabify (cellstr (s))));
107 119
108 %!test 120 %!error untabify ()
109 %! s = untabify (""); 121 %!error untabify (1,2,3,4)
110 %! assert (isempty (s)) 122 %!error <must be a string> untabify (1)
111 123
112 %!test
113 %! s = char (fix (100 + 10*rand (3,3)));
114 %! assert (isequal (untabify (s), untabify ({s}){1}))
115