help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

New function - untabify (was: strjust)


From: John W. Eaton
Subject: New function - untabify (was: strjust)
Date: Sun, 17 Oct 2010 13:13:33 -0400

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.

function s = untabify (t, tw = 8, db = false)

  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

endfunction

function s = replace_tabs (t, tw)
  if (ndims (t) == 2)
    if (isempty (t))
      s = t;
    else
      nr = rows (t);
      sc = cell (nr, 1);
      for j = 1:nr
        n = 1:numel(t(j,:));
        m = find (t(j,:) == "\t");
        t(j,m) = " ";
        for i = 1:numel(m)
          k = tw * ceil (n(m(i)) / tw);
          dn = k - n(m(i));
          n(m(i):end) += dn;
        endfor
        tmp = blanks (n(end));
        tmp(n) = t(j,:);
        sc{j} = tmp;
      endfor
      s = char (sc);
    endif
  else
    error ("untabify: character strings to untabify must have 2 dimensions");
  endif
endfunction


jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]