1 ## Copyright (C) 2010-2012 Ben Abbott
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 2 of the License, or (at
8 ## your option) any later version.
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} untabify (@var{t})
21 ## @deftypefnx {Function File} {} untabify (@var{t}, @var{tw})
22 ## @deftypefnx {Function File} {} untabify (@var{t}, @var{tw}, @var{deblank})
23 ## Replace TAB characters in @var{t}, with spaces.
24 ## The tab width is specified by @var{tw}, or defaults to eight.
25 ## The input, @var{t}, may be either a 2-D character array, or a cell
26 ## array of character strings. The output is the same class
29 ## If the optional argument @var{deblank} is true, then the spaces will
30 ## be removed from the end of the character data.
32 ## The following example reads a file and writes an untabified version
33 ## of the same file with trailing spaces stripped.
37 ## fid = fopen ("tabbed_script.m");
38 ## text = char (fread (fid, "uchar")');
40 ## fid = fopen ("untabified_script.m", "w");
41 ## text = untabify (strsplit (text, "\n"), 8, true);
42 ## fprintf (fid, "%s\n", text@{:@});
47 ## @seealso{strjust, strsplit, deblank}
50 ## Author: Ben Abbott <bpabbott@mac.com>
51 ## Created: 2010-10-15
53 function s = untabify (t, tw = 8, dblank = false)
55 if (nargin < 1 || nargin > 3)
57 elseif (! (ischar (t) || iscellstr (t)))
58 error ("untabify: T must be a string or cellstring");
62 s = replace_tabs (t, tw);
64 s = cellfun (@(str) replace_tabs (str, tw), t, "uniformoutput", false);
73 function s = replace_tabs (t, tw)
76 error ("untabify: character strings to untabify must have 2 dimensions");
86 m = find (t(j,:) == "\t");
89 k = tw * ceil (n(m(i)) / tw);
93 sc{j} = blanks (n(end));
103 %! s = untabify ("\thello\t");
104 %! assert (s, [blanks(8) "hello" blanks(3)]);
107 %! s = untabify ("\thello\t", 2);
108 %! assert (s, [blanks(2) "hello" blanks(1)]);
111 %! s = untabify ("\thello\t", 4, true);
112 %! assert (s, [blanks(4) "hello"]);
114 %!assert (isempty (untabify ("")))
117 %! s = char (randi ([97 97+25], 3, 3));
118 %! assert (untabify (s), char (untabify (cellstr (s))));
121 %!error untabify (1,2,3,4)
122 %!error <must be a string> untabify (1)