changeset 7530:bb0f2353cff5

new cell array ctype mappers
author John W. Eaton <jwe@octave.org>
date Mon, 25 Feb 2008 19:01:30 -0500
parents 7e1b042c5418
children c9a476b1e664
files scripts/ChangeLog scripts/strings/isstrprop.m src/Cell.cc src/Cell.h src/ChangeLog src/ov-cell.h
diffstat 6 files changed, 187 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Mon Feb 25 16:09:48 2008 -0500
+++ b/scripts/ChangeLog	Mon Feb 25 19:01:30 2008 -0500
@@ -1,3 +1,7 @@
+2008-02-25  John W. Eaton  <jwe@octave.org>
+
+	* strings/isstrprop.m: New file.
+
 2008-02-25  Ryan Hinton  <rwh4s@virginia.edu>
 
 	* miscellaneous/unpack.m: Use "-f -" args for tar.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/strings/isstrprop.m	Mon Feb 25 19:01:30 2008 -0500
@@ -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
--- a/src/Cell.cc	Mon Feb 25 16:09:48 2008 -0500
+++ b/src/Cell.cc	Mon Feb 25 19:01:30 2008 -0500
@@ -224,6 +224,20 @@
   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++ ***
--- a/src/Cell.h	Mon Feb 25 16:09:48 2008 -0500
+++ b/src/Cell.h	Mon Feb 25 19:01:30 2008 -0500
@@ -114,6 +114,28 @@
   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
--- a/src/ChangeLog	Mon Feb 25 16:09:48 2008 -0500
+++ b/src/ChangeLog	Mon Feb 25 19:01:30 2008 -0500
@@ -1,3 +1,16 @@
+2008-02-25  John W. Eaton  <jwe@octave.org>
+
+	* 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  <michael.goffioul@gmail.com>
 
 	* ov-scalar.cc (octave_scalar::round): Use xround instead of ::round
--- a/src/ov-cell.h	Mon Feb 25 16:09:48 2008 -0500
+++ b/src/ov-cell.h	Mon Feb 25 19:01:30 2008 -0500
@@ -130,6 +130,22 @@
   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: