annotate scripts/strings/untabify.m @ 11191:01ddaedd6ad5

Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
author Rik <octave@nomad.inbox5.com>
date Thu, 04 Nov 2010 12:18:08 -0700
parents b1f4bdc276b6
children c776f063fefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11102
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
1 ## Copyright (C) 2010 Ben Abbott
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
2 ##
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
3 ## This file is part of Octave.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
4 ##
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 2 of the License, or (at
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## your option) any later version.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
10 ## This program is distributed in the hope that it will be useful,
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
13 ## GNU General Public License for more details.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ##
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
18
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19 ## -*- texinfo -*-
11150
564e998017f5 docstring fixes
John W. Eaton <jwe@octave.org>
parents: 11115
diff changeset
20 ## @deftypefn {Function file} {} untabify (@var{t})
564e998017f5 docstring fixes
John W. Eaton <jwe@octave.org>
parents: 11115
diff changeset
21 ## @deftypefnx {Function file} {} untabify (@var{t}, @var{tw})
564e998017f5 docstring fixes
John W. Eaton <jwe@octave.org>
parents: 11115
diff changeset
22 ## @deftypefnx {Function file} {} untabify (@var{t}, @var{tw}, @var{deblank})
11102
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
23 ## Replace TAB characters in @var{t}, with spaces.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
24 ## The tab width is specified by @var{tw}, or defaults to eight.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
25 ## The input, @var{t}, may be either a 2D character array, or a cell
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
26 ## array of character strings. The output is the same class
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
27 ## as the input.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
28 ##
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
29 ## If the optional argument @var{deblank} is true, then the spaces will
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
30 ## be removed from the end of the character data.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
31 ##
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
32 ## The following example reads a file and writes an untabified version
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
33 ## of the same file with trailing spaces stripped.
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
34 ##
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
35 ## @example
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
36 ## @group
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 ## fid = fopen ("tabbed_script.m");
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
38 ## text = char (fread (fid, "uchar")');
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
39 ## fclose (fid);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
40 ## fid = fopen ("untabified_script.m", "w");
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
41 ## text = untabify (strsplit (text, "\n"), 8, true);
11115
965766039d93 finish adding untabify.m to build system
John W. Eaton <jwe@octave.org>
parents: 11102
diff changeset
42 ## fprintf (fid, "%s\n", text@{:@});
11102
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 ## fclose (fid);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
44 ## @end group
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
45 ## @end example
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
46 ##
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
47 ## @seealso{strjust, strsplit, deblank}
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 ## @end deftypefn
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
50 ## Author: Ben Abbott <bpabbott@mac.com>
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
51 ## Created: 2010-10-15
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
52
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53 function s = untabify (t, tw = 8, db = false)
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
54
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
55 if (nargin > 0 && nargin < 4 && (ischar (t) || iscellstr (t)))
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 if (ischar (t))
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 s = replace_tabs (t, tw);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
58 else
11191
01ddaedd6ad5 Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents: 11190
diff changeset
59 s = cellfun (@(str) replace_tabs (str, tw), t, "uniformoutput", false);
11102
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
60 endif
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
61 if (db)
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
62 s = deblank (s);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 endif
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
64 else
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65 print_usage ();
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
66 endif
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
67
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
68 endfunction
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
69
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
70 function s = replace_tabs (t, tw)
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
71 if (ndims (t) == 2)
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72 if (isempty (t))
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 s = t;
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
74 else
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 nr = rows (t);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76 sc = cell (nr, 1);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 for j = 1:nr
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78 n = 1:numel(t(j,:));
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79 m = find (t(j,:) == "\t");
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80 t(j,m) = " ";
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
81 for i = 1:numel(m)
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
82 k = tw * ceil (n(m(i)) / tw);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
83 dn = k - n(m(i));
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
84 n(m(i):end) += dn;
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
85 endfor
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
86 sc{j} = blanks (n(end));
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
87 sc{j}(n) = t(j,:);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
88 endfor
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
89 s = char (sc);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
90 endif
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
91 else
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
92 error ("untabify: character strings to untabify must have 2 dimensions");
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
93 endif
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
94 endfunction
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
95
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
96 %!test
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
97 %! s = untabify ("\thello\t");
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
98 %! assert (isequal (s, horzcat (blanks(8), "hello ")))
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
99
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
100 %!test
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
101 %! s = untabify ("\thello\t", 4, true);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
102 %! assert (isequal (s, horzcat (blanks(4), "hello")))
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
103
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
104 %!test
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
105 %! s = untabify ("\thello\t", 2, true);
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
106 %! assert (isequal (s, horzcat (blanks(2), "hello")))
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
107
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 %!test
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109 %! s = untabify ("");
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
110 %! assert (isempty (s))
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
111
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
112 %!test
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
113 %! s = char (fix (100 + 10*rand (3,3)));
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
114 %! assert (isequal (untabify (s), untabify ({s}){1}))
eab173e1e80c untabify.m: New function to untabify text.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
115