changeset 11785:35d5c824f1eb release-3-0-x

make regexp(...,'once') matlab compatible
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 18 Jun 2008 21:00:06 +0200
parents 5667eafad9a1
children 9de3ccd2e7ac
files src/ChangeLog src/DLD-FUNCTIONS/regexp.cc
diffstat 2 files changed, 81 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2008-06-20  Jaroslav Hajek <highegg@gmail.com>
+
+	* DLD-FUNCTIONS/regexp.cc (octregexp_list): Make "once" an output
+	argument.
+	(octregexp): Do not use cell arrays when "once" is requested.
+
 2008-06-10  John W. Eaton  <jwe@octave.org>
 
 	* mexproto.h (mxCreateLogicalScalar): Declar arg as mxLogical, not int.
--- a/src/DLD-FUNCTIONS/regexp.cc
+++ b/src/DLD-FUNCTIONS/regexp.cc
@@ -83,17 +83,17 @@
 static int
 octregexp_list (const octave_value_list &args, const std::string &nm, 
 		bool case_insensitive, std::list<regexp_elem> &lst, 
-		string_vector &named, int &nopts)
+		string_vector &named, int &nopts, bool &once)
 {
   int sz = 0;
 #if defined (HAVE_REGEX) || defined (HAVE_PCRE) 
   int nargin = args.length();
-  bool once = false;
   bool lineanchors = false;
   bool dotexceptnewline = false;
   bool freespacing = false;
 
   nopts = nargin - 2;
+  once = false;
 
   std::string buffer = args(0).string_value ();
   if (error_state)
@@ -451,7 +451,8 @@
   std::list<regexp_elem> lst;
   string_vector named;
   int nopts;
-  int sz = octregexp_list (args, nm, case_insensitive, lst, named, nopts);
+  bool once;
+  int sz = octregexp_list (args, nm, case_insensitive, lst, named, nopts, once);
 
   if (! error_state)
     {
@@ -482,36 +483,70 @@
       retval(5) = Octave_map();
 #endif
 
-      Cell t (dim_vector(1, sz));
-      i = 0;
-      for (const_iterator p = lst.begin(); p != lst.end(); p++)
-	t(i++) = p->t;
-      retval(4) = t;
+      if (once)
+        retval(4) = sz ? lst.front ().t : Cell();
+      else
+        {
+          Cell t (dim_vector(1, sz));
+          i = 0;
+          for (const_iterator p = lst.begin(); p != lst.end(); p++)
+            t(i++) = p->t;
+          retval(4) = t;
+        }
 
-      Cell m (dim_vector(1, sz));
-      i = 0;
-      for (const_iterator p = lst.begin(); p != lst.end(); p++)
-	m(i++) = p->m;
-      retval(3) = m;
-
+      if (once)
+        retval(3) = sz ? lst.front ().m : std::string();
+      else
+        {
+          Cell m (dim_vector(1, sz));
+          i = 0;
+          for (const_iterator p = lst.begin(); p != lst.end(); p++)
+            m(i++) = p->m;
+          retval(3) = m;
+        }
 
-      Cell te (dim_vector(1, sz));
-      i = 0;
-      for (const_iterator p = lst.begin(); p != lst.end(); p++)
-	te(i++) = p->te;
-      retval(2) = te;
+      if (once)
+        retval(2) = sz ? lst.front ().te : Matrix();
+      else
+        {
+          Cell te (dim_vector(1, sz));
+          i = 0;
+          for (const_iterator p = lst.begin(); p != lst.end(); p++)
+            te(i++) = p->te;
+          retval(2) = te;
+        }
 
-      NDArray e (dim_vector(1, sz));
-      i = 0;
-      for (const_iterator p = lst.begin(); p != lst.end(); p++)
-	e(i++) = p->e;
-      retval(1) = e;
+      if (once)
+        {
+          if (sz)
+            retval(1) = lst.front ().e;
+          else
+            retval(1) = Matrix();
+        }
+      else
+        {
+          NDArray e (dim_vector(1, sz));
+          i = 0;
+          for (const_iterator p = lst.begin(); p != lst.end(); p++)
+            e(i++) = p->e;
+          retval(1) = e;
+        }
 
+      if (once)
+        {
+          if (sz)
+            retval(0) = lst.front ().s;
+          else
+            retval(0) = Matrix();
+        }
+      else
+        {
       NDArray s (dim_vector(1, sz));
       i = 0;
       for (const_iterator p = lst.begin(); p != lst.end(); p++)
 	s(i++) = p->s;
       retval(0) = s;
+        }
 
       // Alter the order of the output arguments
       if (nopts > 0)
@@ -911,21 +946,17 @@
 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*','once');
 %! assert (s,1)
 %! assert (e,5)
-%! assert (size(te), [1,1])
-%! assert (isempty(te{1}))
-%! assert (m{1},'short')
-%! ## Matlab gives [1,0] here but that seems wrong.
-%! assert (size(t), [1,1])
+%! assert (isempty(te))
+%! assert (m,'short')
+%! assert (isempty(t))
 
 %!test
 %! [m, te, e, s, t] = regexp('short test string','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens');
 %! assert (s,1)
 %! assert (e,5)
-%! assert (size(te), [1,1])
-%! assert (isempty(te{1}))
-%! assert (m{1},'short')
-%! ## Matlab gives [1,0] here but that seems wrong.
-%! assert (size(t), [1,1])
+%! assert (isempty(te))
+%! assert (m,'short')
+%! assert (isempty(t))
 
 %!testif HAVE_PCRE
 %! ## This test is expected to fail if PCRE is not installed
@@ -1087,21 +1118,17 @@
 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*','once');
 %! assert (s,1)
 %! assert (e,5)
-%! assert (size(te), [1,1])
-%! assert (isempty(te{1}))
-%! assert (m{1},'ShoRt')
-%! ## Matlab gives [1,0] here but that seems wrong.
-%! assert (size(t), [1,1])
+%! assert (isempty(te))
+%! assert (m,'ShoRt')
+%! assert (isempty(t))
 
 %!test
 %! [m, te, e, s, t] = regexpi('ShoRt Test String','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens');
 %! assert (s,1)
 %! assert (e,5)
-%! assert (size(te), [1,1])
-%! assert (isempty(te{1}))
-%! assert (m{1},'ShoRt')
-%! ## Matlab gives [1,0] here but that seems wrong.
-%! assert (size(t), [1,1])
+%! assert (isempty(te))
+%! assert (m,'ShoRt')
+%! assert (isempty(t))
 
 %!testif HAVE_PCRE
 %! ## This test is expected to fail if PCRE is not installed
@@ -1237,7 +1264,8 @@
       std::list<regexp_elem> lst;
       string_vector named;
       int nopts;
-      int sz = octregexp_list (regexpargs, nm , false, lst, named, nopts);
+      bool once;
+      int sz = octregexp_list (regexpargs, nm , false, lst, named, nopts, once);
 
       if (error_state)
 	return retval;
@@ -1323,7 +1351,8 @@
       std::list<regexp_elem> lst;
       string_vector named;
       int nopts;
-      int sz = octregexp_list (regexpargs, nm, false, lst, named,nopts);
+      bool once;
+      int sz = octregexp_list (regexpargs, nm, false, lst, named, nopts, once);
 
       if (error_state)
 	return retval;