Mercurial > octave-nkf
comparison scripts/strings/untabify.m @ 11102:eab173e1e80c
untabify.m: New function to untabify text.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sun, 17 Oct 2010 21:44:55 -0400 |
parents | |
children | 965766039d93 |
comparison
equal
deleted
inserted
replaced
11101:1f9ab076f5f7 | 11102:eab173e1e80c |
---|---|
1 ## Copyright (C) 2010 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 2D 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, db = false) | |
54 | |
55 if (nargin > 0 && nargin < 4 && (ischar (t) || iscellstr (t))) | |
56 if (ischar (t)) | |
57 s = replace_tabs (t, tw); | |
58 else | |
59 s = cellfun (@(str) replace_tabs (str, tw), t, "uniformoutput", false); | |
60 endif | |
61 if (db) | |
62 s = deblank (s); | |
63 endif | |
64 else | |
65 print_usage (); | |
66 endif | |
67 | |
68 endfunction | |
69 | |
70 function s = replace_tabs (t, tw) | |
71 if (ndims (t) == 2) | |
72 if (isempty (t)) | |
73 s = t; | |
74 else | |
75 nr = rows (t); | |
76 sc = cell (nr, 1); | |
77 for j = 1:nr | |
78 n = 1:numel(t(j,:)); | |
79 m = find (t(j,:) == "\t"); | |
80 t(j,m) = " "; | |
81 for i = 1:numel(m) | |
82 k = tw * ceil (n(m(i)) / tw); | |
83 dn = k - n(m(i)); | |
84 n(m(i):end) += dn; | |
85 endfor | |
86 sc{j} = blanks (n(end)); | |
87 sc{j}(n) = t(j,:); | |
88 endfor | |
89 s = char (sc); | |
90 endif | |
91 else | |
92 error ("untabify: character strings to untabify must have 2 dimensions"); | |
93 endif | |
94 endfunction | |
95 | |
96 %!test | |
97 %! s = untabify ("\thello\t"); | |
98 %! assert (isequal (s, horzcat (blanks(8), "hello "))) | |
99 | |
100 %!test | |
101 %! s = untabify ("\thello\t", 4, true); | |
102 %! assert (isequal (s, horzcat (blanks(4), "hello"))) | |
103 | |
104 %!test | |
105 %! s = untabify ("\thello\t", 2, true); | |
106 %! assert (isequal (s, horzcat (blanks(2), "hello"))) | |
107 | |
108 %!test | |
109 %! s = untabify (""); | |
110 %! assert (isempty (s)) | |
111 | |
112 %!test | |
113 %! s = char (fix (100 + 10*rand (3,3))); | |
114 %! assert (isequal (untabify (s), untabify ({s}){1})) | |
115 |