octave-maintainers
[Top][All Lists]
Advanced

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

Re: Error with pkg.m (== isspace does work with cell arrays anymore)


From: John W. Eaton
Subject: Re: Error with pkg.m (== isspace does work with cell arrays anymore)
Date: Mon, 25 Feb 2008 19:12:19 -0500

On 25-Feb-2008, Søren Hauberg wrote:

| 
| man, 25 02 2008 kl. 16:17 -0500, skrev John W. Eaton:
| > On 24-Feb-2008, Michael Goffioul wrote:
| > 
| > | I have an error with recent mercurial code, in pkg.m. It reduces to
| > | the fact that isspace does not work anymore on cell array of
| > | strings. The error occurs around line 1299:
| > | 
| > |   if (! all (isspace (filenames)))
| > | 
| > | where filenames is a cell array of strings. I'm not sure what's
| > | the correct way to fix this.
| > 
| > I could probably help if I knew what isspace should do for a cell
| > array.  Matlab appears to do this:
| > 
| >   >> isspace ({' ', ' ', ' '})      
| > 
| >   ans =
| > 
| >        0     0     0
| > 
| > 
| > Which makes no sense to me.  Is this behavior useful in some way that
| > I'm not seeing?
| It seems to be that matlab is doing something like
| 
| function output = isspace(input)
|   if (ischar(input))
|     ...
|   else
|     output = zeros(size(input), "logical");
|   endif
| endfunction
| 
| As an example:
|   isspace(@sin)
| returns 0. Does that make sense? Not in my mind. I think the logic is:
| 
|   "A space is part of a string, hence if the input is not a string, it
| is not a space."

Yes, probably an error is better.

| I don't think we should copy this. If people depend on this behaviour,
| then there's a pretty good chance of a bug in their code.

OK, isspace appears to be unique.  Matlab doesn't seem to have the
other is* predicates, but it does have isstrprop, which lumps them all
in one function.  I propose the following change.  The added functions in
{Cell,ov-cell}.{h,cc} make writing the isstrprop function fairly
simple.

I would guess this still doesn't solve the isspace problem in pkg.m.
We need to know what the intent of the isspace call there is.

jwe


# HG changeset patch
# User John W. Eaton <address@hidden>
# Date 1203984090 18000
# Node ID bb0f2353cff56ce32d23919fc684174158826009
# Parent  7e1b042c5418240ec84cc3bf46e1f9d91d060105
new cell array ctype mappers

diff --git a/scripts/ChangeLog b/scripts/ChangeLog
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,7 @@ 2008-02-25  Ryan Hinton  <address@hidden
+2008-02-25  John W. Eaton  <address@hidden>
+
+       * strings/isstrprop.m: New file.
+
 2008-02-25  Ryan Hinton  <address@hidden>
 
        * miscellaneous/unpack.m: Use "-f -" args for tar.
diff --git a/scripts/strings/isstrprop.m b/scripts/strings/isstrprop.m
new file mode 100644
--- /dev/null
+++ b/scripts/strings/isstrprop.m
@@ -0,0 +1,118 @@
+## Copyright (C) 2008 John W. Eaton
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} isstrprop (@var{str}, @var{pred})
+## Test character string properties.  For example,
+##
+## @example
+## @group
+## isstrprop ("abc123", "isalpha")
+## @result{} [1, 1, 1, 0, 0, 0]
+## @end group
+## @end example
+## 
+## If @var{str} is a cell array, @code{isstrpop} is applied recursively
+## to each element of the cell array.
+##
+## Numeric arrays are converted to character strings.
+##
+## The second argument @var{pred} may be one of
+##
+## @table @code
+## @item "alpha"
+## True for characters that are alphabetic
+##
+## @item "alnum"
+## @itemx "alphanum"
+## True for characters that are alphabetic or digits.
+## 
+## @item "ascii"
+## True for characters that are in the range of ASCII encoding.
+## 
+## @item "cntrl"
+## True for control characters.
+## 
+## @item "digit"
+## True for decimal digits.
+## 
+## @item "graph"
+## @itemx "graphic"
+## True for printing characters except space.
+## 
+## @item "lower"
+## True for lower-case letters.
+## 
+## @item "print"
+## True for printing characters including space.
+## 
+## @item "punct"
+## True for printing characters except space or letter or digit.
+## 
+## @item "space"
+## @itemx "wspace"
+## True for whitespace characters (space, formfeed, newline, carriage
+## return, tab, vertical tab).
+## 
+## @item "upper"
+## True for upper-case letters.
+## 
+## @item "xdigit"
+## True for hexadecimal digits.
+## @end table
+##
+## @seealso{isalnum, isalpha, isascii, iscntrl, isdigit, isgraph,
+## islower, isprint, ispunct, isspace, isupper, isxdigit}
+## @end deftypefn
+
+function retval = isstrprop (str, pred)
+
+  if (nargin == 2)
+    switch (pred)
+      case "alpha"
+       retval = isalpha (str);
+      case {"alnum", "alphanum"}
+       retval = isalnum (str);
+      case "ascii"
+       retval = isascii (str);
+      case "cntrl"
+       retval = iscntrl (str);
+      case "digit"
+       retval = isdigit (str);
+      case {"graph", "graphic"}
+       retval = isgraph (str);
+      case "lower"
+       retval = islower (str);
+      case "print"
+       retval = isprint (str);
+      case "punct"
+       retval = ispunct (str);
+      case {"space", "wspace"}
+       retval = isspace (str);
+      case "upper"
+       retval = isupper (str);
+      case "xdigit"
+       retval = isxdigit (str);
+      otherwise
+       error ("isstrprop: invalid predicate");
+    endswitch
+  else
+    print_usage ();
+  endif
+
+endfunction
diff --git a/src/Cell.cc b/src/Cell.cc
--- a/src/Cell.cc
+++ b/src/Cell.cc
@@ -224,6 +224,20 @@ Cell::insert (const Cell& a, const Array
   return *this;
 }
 
+Cell
+Cell::map (ctype_mapper fcn) const
+{
+  Cell retval (dims ());
+  octave_value *r = retval.fortran_vec ();
+
+  const octave_value *p = data ();
+
+  for (octave_idx_type i = 0; i < numel (); i++)
+    r[i] = ((p++)->*fcn) ();
+
+  return retval;
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***
diff --git a/src/Cell.h b/src/Cell.h
--- a/src/Cell.h
+++ b/src/Cell.h
@@ -114,6 +114,28 @@ public:
   bool is_true (void) const { return false; }
 
   static octave_value resize_fill_value (void) { return Matrix (); }
+
+  Cell xisalnum (void) const { return map (&octave_value::xisalnum); }
+  Cell xisalpha (void) const { return map (&octave_value::xisalpha); }
+  Cell xisascii (void) const { return map (&octave_value::xisascii); }
+  Cell xiscntrl (void) const { return map (&octave_value::xiscntrl); }
+  Cell xisdigit (void) const { return map (&octave_value::xisdigit); }
+  Cell xisgraph (void) const { return map (&octave_value::xisgraph); }
+  Cell xislower (void) const { return map (&octave_value::xislower); }
+  Cell xisprint (void) const { return map (&octave_value::xisprint); }
+  Cell xispunct (void) const { return map (&octave_value::xispunct); }
+  Cell xisspace (void) const { return map (&octave_value::xisspace); }
+  Cell xisupper (void) const { return map (&octave_value::xisupper); }
+  Cell xisxdigit (void) const { return map (&octave_value::xisxdigit); }
+  Cell xtoascii (void) const { return map (&octave_value::xtoascii); }
+  Cell xtolower (void) const { return map (&octave_value::xtolower); }
+  Cell xtoupper (void) const { return map (&octave_value::xtoupper); }
+
+private:
+
+  typedef octave_value (octave_value::*ctype_mapper) (void) const;
+
+  Cell map (ctype_mapper) const;
 };
 
 #endif
diff --git a/src/ChangeLog b/src/ChangeLog
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,16 @@ 2008-02-25  Michael Goffioul  <michael.g
+2008-02-25  John W. Eaton  <address@hidden>
+
+       * Cell.cc (Cell::map): New function.
+       * Cell.h (Cell::map): Declare.
+       (xisalnum, xisalpha, xisascii, xiscntrl, xisdigit,
+       xisgraph, xislower, xisprint, xispunct, xisspace, xisupper,
+       xisxdigit, xtoascii, xtolower, xtoupper): New mapper functions.
+       (ctype_mapper): New private typedef.
+
+       * ov-cell.h (xisalnum, xisalpha, xisascii, xiscntrl, xisdigit,
+       xisgraph, xislower, xisprint, xispunct, xisspace, xisupper,
+       xisxdigit, xtoascii, xtolower, xtoupper): New mapper functions.
+
 2008-02-25  Michael Goffioul  <address@hidden>
 
        * ov-scalar.cc (octave_scalar::round): Use xround instead of ::round
diff --git a/src/ov-cell.h b/src/ov-cell.h
--- a/src/ov-cell.h
+++ b/src/ov-cell.h
@@ -130,6 +130,22 @@ public:
   bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug);
 #endif
 
+  octave_value xisalnum (void) const { return matrix.xisalnum (); }
+  octave_value xisalpha (void) const { return matrix.xisalpha (); }
+  octave_value xisascii (void) const { return matrix.xisascii (); }
+  octave_value xiscntrl (void) const { return matrix.xiscntrl (); }
+  octave_value xisdigit (void) const { return matrix.xisdigit (); }
+  octave_value xisgraph (void) const { return matrix.xisgraph (); }
+  octave_value xislower (void) const { return matrix.xislower (); }
+  octave_value xisprint (void) const { return matrix.xisprint (); }
+  octave_value xispunct (void) const { return matrix.xispunct (); }
+  octave_value xisspace (void) const { return matrix.xisspace (); }
+  octave_value xisupper (void) const { return matrix.xisupper (); }
+  octave_value xisxdigit (void) const { return matrix.xisxdigit (); }
+  octave_value xtoascii (void) const { return matrix.xtoascii (); }
+  octave_value xtolower (void) const { return matrix.xtolower (); }
+  octave_value xtoupper (void) const { return matrix.xtoupper (); }
+
   mxArray *as_mxArray (void) const;
 
 private:

reply via email to

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