[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: New function - untabify (was: strjust)
From: |
Ben Abbott |
Subject: |
Re: New function - untabify (was: strjust) |
Date: |
Mon, 18 Oct 2010 08:23:09 +0800 |
On Oct 18, 2010, at 1:13 AM, John W. Eaton wrote:
> On 17-Oct-2010, Ben Abbott wrote:
>
> | On Oct 15, 2010, at 10:35 PM, John W. Eaton wrote:
> |
> | > On 15-Oct-2010, Ben Abbott wrote:
> | >
> | > | Would a tab_to_spaces() be a good idea?
> | >
> | > How about calling it untabify, like the Emacs function?
> | >
> | > jwe
> |
> |
> | I've attached a proposal.
>
> I think tabs should default to 8 spaces.
>
> It would be nice to avoid converting a character array to a cell array
> and then back.
>
> I think this loop
>
> s = cell (size (t));
> for n = 1:numel(t)
> if (! isempty (t{n}))
> s{n} = replace_tabs (t{n}, tw);
> else
> s{n} = "";
> endif
> endfor
>
> can be replaced by a call to cellfun:
>
> s = cellfun (@(str) replace_tabs (str, tw), t, "uniformoutput", false)
>
> Then you could write
>
> Instead of
>
> if (nargin < 2)
> tw = 4;
> endif
>
> and
>
> if (nargin == 3 && db)
> s = deblank (s);
> endif
>
> you could use Octave's default argument value feature:
>
> function s = untabify (t, tw = 8, db = false)
>
> and then simply
>
> if (db)
> deblank (s);
> endif
>
> Instead of checking for what conditions are necessary to generate a
> usage error, I prefer to check for what conditions are necessary for
> the function call to succeed, like this:
>
> if (nargin > 0 && nargin < 4 && ischar (t) || iscellstr (t))
> if (ischar (t))
> s = replace_tabs (t, tw);
> else
> s = cellfun (@(str) replace_tabs (str, tw), t, "uniformoutput", false)
> endif
> if (db)
> s = deblank (s);
> endif
> else
> print_usage ();
> endif
>
> Is it necessary to restrict character arrays to be 2D? You don't seem
> to do that for the individual elements of cellstring arrays. OK,
> maybe restricting this is OK, but it should also be done for
> cellstring arrays.
>
> Also, I see
>
> octave:1> x = char (fix (100 + 10*rand (3,3)));
> octave:2> untabify (x)
> ans =
>
> hkk
> mff
> djl
>
> octave:3> untabify ({x})
> ans =
>
> {
> [1,1] = hmdkfjkfl
> }
>
> octave:4> {x}
> ans =
>
> {
> [1,1] =
>
> hkk
> mff
> djl
>
> }
>
> I don't understand why the element of the cellstring array loses its
> shape.
>
> If this function is added to Octave, the copyright notice should be
> changed to say "This file is part of Octave. ..." instead
> of the "This program is free software..."; see the file str2num for
> example. I see there are abotu 50 other .m files and a couple of .cc
> files that have the incorrect copyright statement. I'll fix those
> soon if someone else doesn't beat me to it.
>
> Here's a new version with the changes above and that I think fixes the
> shape problem. It would be nice to vectorize and/or simplify the
> replace_tabs function, but I'm not sure of the best way to do that at
> the moment.
>
> jwe
I'm embarrassed to day that I hadn't considered having the cells contain
multiple lines.
I've made a couple of minor changes to your version and taken care of the
copyright.
Regarding the changes in shape, I don't see it. I've added a test for that, and
it passes.
Do the tests pass for you?
Ben
changeset.patch
Description: Binary data
- Re: strjust, (continued)
- Re: strjust, Ben Abbott, 2010/10/14
- Re: strjust, Ozzy Lash, 2010/10/14
- Re: strjust, Leo Butler, 2010/10/15
- Re: strjust, Jaroslav Hajek, 2010/10/15
- Re: strjust, Ben Abbott, 2010/10/15
- Re: strjust, John W. Eaton, 2010/10/15
- Re: strjust, Leo Butler, 2010/10/15
- Re: strjust, Ben Abbott, 2010/10/15
- New function - untabify (was: strjust), Ben Abbott, 2010/10/17
- New function - untabify (was: strjust), John W. Eaton, 2010/10/17
- Re: New function - untabify (was: strjust),
Ben Abbott <=
- Re: New function - untabify (was: strjust), John W. Eaton, 2010/10/17
- Re: New function - untabify (was: strjust), John W. Eaton, 2010/10/18
- Re: strjust, Ben Abbott, 2010/10/18
- Re: strjust, Ben Abbott, 2010/10/15