]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/untabify.m
update packages
[CreaPhase.git] / octave_packages / m / strings / untabify.m
1 ## Copyright (C) 2010-2012 Ben Abbott
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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
27 ## as the input.
28 ##
29 ## If the optional argument @var{deblank} is true, then the spaces will
30 ## be removed from the end of the character data.
31 ##
32 ## The following example reads a file and writes an untabified version
33 ## of the same file with trailing spaces stripped.
34 ##
35 ## @example
36 ## @group
37 ## fid = fopen ("tabbed_script.m");
38 ## text = char (fread (fid, "uchar")');
39 ## fclose (fid);
40 ## fid = fopen ("untabified_script.m", "w");
41 ## text = untabify (strsplit (text, "\n"), 8, true);
42 ## fprintf (fid, "%s\n", text@{:@});
43 ## fclose (fid);
44 ## @end group
45 ## @end example
46 ##
47 ## @seealso{strjust, strsplit, deblank}
48 ## @end deftypefn
49
50 ## Author: Ben Abbott <bpabbott@mac.com>
51 ## Created: 2010-10-15
52
53 function s = untabify (t, tw = 8, dblank = false)
54
55   if (nargin < 1 || nargin > 3)
56     print_usage ();
57   elseif (! (ischar (t) || iscellstr (t)))
58     error ("untabify: T must be a string or cellstring");
59   endif
60
61   if (ischar (t))
62     s = replace_tabs (t, tw);
63   else
64     s = cellfun (@(str) replace_tabs (str, tw), t, "uniformoutput", false);
65   endif
66
67   if (dblank)
68     s = deblank (s);
69   endif
70
71 endfunction
72
73 function s = replace_tabs (t, tw)
74
75   if (ndims (t) != 2)
76     error ("untabify: character strings to untabify must have 2 dimensions");
77   endif
78
79   if (isempty (t))
80     s = t;
81   else
82     nr = rows (t);
83     sc = cell (nr, 1);
84     for j = 1:nr
85       n = 1:numel(t(j,:));
86       m = find (t(j,:) == "\t");
87       t(j,m) = " ";
88       for i = 1:numel(m)
89         k = tw * ceil (n(m(i)) / tw);
90         dn = k - n(m(i));
91         n(m(i):end) += dn;
92       endfor
93       sc{j} = blanks (n(end));
94       sc{j}(n) = t(j,:);
95     endfor
96     s = char (sc);
97   endif
98
99 endfunction
100
101
102 %!test
103 %! s = untabify ("\thello\t");
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)]);
109
110 %!test
111 %! s = untabify ("\thello\t", 4, true);
112 %! assert (s, [blanks(4) "hello"]);
113
114 %!assert (isempty (untabify ("")))
115
116 %!test
117 %! s = char (randi ([97 97+25], 3, 3));
118 %! assert (untabify (s), char (untabify (cellstr (s))));
119
120 %!error untabify ()
121 %!error untabify (1,2,3,4)
122 %!error <must be a string> untabify (1)
123