Fixed use of finalizers for guardians and SMOBs (undoes patches 23-24).
[bpt/guile.git] / libguile / regex-posix.c
index 9587dfa..fcef500 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
+/*     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2006 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -12,7 +12,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 
@@ -52,6 +52,7 @@
 #endif
 #endif
 
+#include "libguile/async.h"
 #include "libguile/smob.h"
 #include "libguile/symbols.h"
 #include "libguile/vectors.h"
@@ -162,7 +163,7 @@ SCM_DEFINE (scm_make_regexp, "make-regexp", 1, 0, 1,
      turn off REG_EXTENDED flag (on by default). */
   cflags = REG_EXTENDED;
   flag = flags;
-  while (!SCM_NULLP (flag))
+  while (!scm_is_null (flag))
     {
       if (scm_to_int (SCM_CAR (flag)) == REG_BASIC)
        cflags &= ~REG_EXTENDED;
@@ -186,7 +187,8 @@ SCM_DEFINE (scm_make_regexp, "make-regexp", 1, 0, 1,
                     scm_from_locale_string (FUNC_NAME),
                     errmsg,
                     SCM_BOOL_F,
-                    SCM_BOOL_F);
+                    scm_list_1 (pat));
+      
       /* never returns */
     }
   SCM_RETURN_NEWSMOB (scm_tc16_regex, rx);
@@ -218,15 +220,23 @@ SCM_DEFINE (scm_regexp_exec, "regexp-exec", 2, 2, 0,
 {
   int status, nmatches, offset;
   regmatch_t *matches;
+  char *c_str;
   SCM mvec = SCM_BOOL_F;
+  SCM substr;
 
   SCM_VALIDATE_RGXP (1, rx);
   SCM_VALIDATE_STRING (2, str);
 
   if (SCM_UNBNDP (start))
-    offset = 0;
+    {
+      substr = str;
+      offset = 0;
+    }
   else
-    offset = scm_to_signed_integer (start, 0, scm_i_string_length (str));
+    {
+      substr = scm_substring (str, start, SCM_UNDEFINED);
+      offset = scm_to_int (start);
+    }
 
   if (SCM_UNBNDP (flags))
     flags = SCM_INUM0;
@@ -235,36 +245,37 @@ SCM_DEFINE (scm_regexp_exec, "regexp-exec", 2, 2, 0,
      whole regexp, so add 1 to nmatches. */
 
   nmatches = SCM_RGX(rx)->re_nsub + 1;
-  SCM_DEFER_INTS;
+  SCM_CRITICAL_SECTION_START;
   matches = scm_malloc (sizeof (regmatch_t) * nmatches);
-  status = regexec (SCM_RGX (rx), scm_i_string_chars (str) + offset,
-                   nmatches, matches,
+  c_str = scm_to_locale_string (substr);
+  status = regexec (SCM_RGX (rx), c_str, nmatches, matches,
                    scm_to_int (flags));
+  free (c_str);
+
   if (!status)
     {
       int i;
       /* The match vector must include a cell for the string that was matched,
         so add 1. */
       mvec = scm_c_make_vector (nmatches + 1, SCM_UNSPECIFIED);
-      SCM_VECTOR_SET(mvec,0, str);
+      SCM_SIMPLE_VECTOR_SET(mvec,0, str);
       for (i = 0; i < nmatches; ++i)
        if (matches[i].rm_so == -1)
-         SCM_VECTOR_SET(mvec, i+1,
+         SCM_SIMPLE_VECTOR_SET(mvec, i+1,
                         scm_cons (scm_from_int (-1), scm_from_int (-1)));
        else
-         SCM_VECTOR_SET(mvec, i+1,
+         SCM_SIMPLE_VECTOR_SET(mvec, i+1,
                         scm_cons (scm_from_long (matches[i].rm_so + offset),
                                   scm_from_long (matches[i].rm_eo + offset)));
     }
   free (matches);
-  SCM_ALLOW_INTS;
+  SCM_CRITICAL_SECTION_END;
 
   if (status != 0 && status != REG_NOMATCH)
     scm_error_scm (scm_regexp_error_key,
                   scm_from_locale_string (FUNC_NAME),
                   scm_regexp_error_msg (status, SCM_RGX (rx)),
-                  SCM_BOOL_F,
-                  SCM_BOOL_F);
+                  SCM_BOOL_F, SCM_BOOL_F);
   return mvec;
 }
 #undef FUNC_NAME