gnome-regex



There is old code in libgnome which implements regular expression cache.

Neither grep in my cvs tree, nor LXR search does not indicate any usage
of gnome-regex API.

Except gtranslator, which has it's own copy with a couple of bugs and a
FIXME fixed; and gnome-libs/libgnome/gnome-metadata.*, which is
obsolete.

It is a good time to fix this for libgnome, and I have a patch. But I
wonder if it is useful at all, if no one uses this feature.

Either remove this feature completely (I see no point in putting it into
libcompat), or have someone to fix it. There is some glib/gcache.h, but
it's very simple, maybe not useful...

--
Gediminas Paulauskas
Kaunas, Lithuania
diff -u libgnome/gnome-regex.c src/gnome-regex.c
--- libgnome/gnome-regex.c	Wed Oct 18 15:36:47 2000
+++ src/gnome-regex.c	Thu Jul  5 18:56:35 2001
@@ -21,11 +21,13 @@
   @NOTATION@
  */
 
+#ifdef HAVE_CONFIG_H
 #include <config.h>
+#endif
 
 #include <string.h>
+#include <glib.h>
 
-#include "libgnomeP.h"
 #include "gnome-regex.h"
 
 #define DEFAULT_SIZE 96
@@ -40,11 +42,27 @@
 GnomeRegexCache *
 gnome_regex_cache_new (void)
 {
+	return gnome_regex_cache_new_with_size (DEFAULT_SIZE);
+}
+
+/**
+ * gnome_regex_cache_new_with_size:
+ * @size: the number of cache items
+ * 
+ * Creates a new regular expression cache object with specified
+ * number of items.
+ * 
+ * Return value: the new cache object.
+ **/
+GnomeRegexCache *
+gnome_regex_cache_new_with_size (int size)
+{
 	GnomeRegexCache *rxc = g_new (GnomeRegexCache, 1);
-	rxc->size = DEFAULT_SIZE;
+	rxc->size = size;
 	rxc->next = 0;
 	rxc->regexs = g_new0 (char *, rxc->size);
 	rxc->patterns = g_new (regex_t, rxc->size);
+	rxc->flags = g_new0 (int, rxc->size);
 	return rxc;
 }
 
@@ -78,6 +96,7 @@
 
 	g_free (rxc->regexs);
 	g_free (rxc->patterns);
+	g_free (rxc->flags);
 	g_free (rxc);
 }
 
@@ -98,7 +117,9 @@
 
 	if (new_size < rxc->size) {
 		int i;
-		for (i = new_size + 1; i < rxc->size; ++i) {
+		/* FIXME This deletes not the oldest elements in cache,
+		 * but truncates the end of it instead  */
+		for (i = new_size; i < rxc->size; i++) {
 			free_element (rxc, i);
 		}
 	}
@@ -135,10 +156,11 @@
 	int i;
 	regex_t rx;
 
-	for (i = 0; i < rxc->size; ++i) {
+	for (i = 0; i < rxc->size; i++) {
 		if (! rxc->regexs[i])
 			break;
-		if (! strcmp (rxc->regexs[i], pattern)) {
+		if ((rxc->flags[i] == flags)
+		    && (! strcmp (rxc->regexs[i], pattern))) {
 			return &rxc->patterns[i];
 		}
 	}
@@ -153,6 +175,7 @@
 
 	rxc->regexs[rxc->next] = g_strdup (pattern);
 	memcpy (&rxc->patterns[rxc->next], &rx, sizeof (regex_t));
+	rxc->flags[rxc->next] = flags;
 
 	i = rxc->next;
 	if (++rxc->next >= rxc->size)
@@ -160,3 +183,4 @@
 
 	return &rxc->patterns[i];
 }
+
diff -u libgnome/gnome-regex.h src/gnome-regex.h
--- libgnome/gnome-regex.h	Thu Apr 19 22:33:50 2001
+++ src/gnome-regex.h	Thu Jul  5 18:53:37 2001
@@ -24,28 +24,28 @@
 #ifndef GNOME_REGEX_H
 #define GNOME_REGEX_H
 
-
-G_BEGIN_DECLS
-
+#include <glib.h>
 #include <sys/types.h>
 #include <regex.h>
 
+G_BEGIN_DECLS
+
 typedef struct _GnomeRegexCache GnomeRegexCache;
 struct _GnomeRegexCache {
 	char **regexs;		/* Regular expression strings.  */
 	regex_t *patterns;	/* Compiled expressions.  */
+	int *flags;		/* Compilation flags for each expression */
 	int size;		/* Total number of cache slots.  */
 	int next;		/* Next available slot.  */
-	/* FIXME: probably should cache compilation flags along with
-	   regex and use those to determine caching.  For now we
-	   assume that the flags never change.  Another option would
-	   be to put the flag info into this structure and just not
-	   let the user ever change it.  */
 };
 
 /* Create a new regular expression cache with default number of
    items.  */
 GnomeRegexCache *gnome_regex_cache_new (void);
+
+/* Create a new regular expression cache with specified number of
+   items.  */
+GnomeRegexCache *gnome_regex_cache_new_with_size (int size);
 
 /* Free a regular expression cache.  */
 void gnome_regex_cache_destroy (GnomeRegexCache *rxc);


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