Browse Source

Speed up clish's finding of nspace prefix

git-svn-id: https://klish.googlecode.com/svn/trunk@413 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
c024ed8b6e
3 changed files with 27 additions and 13 deletions
  1. 3 0
      clish/nspace.h
  2. 21 13
      clish/nspace/nspace.c
  3. 3 0
      clish/nspace/private.h

+ 3 - 0
clish/nspace.h

@@ -24,6 +24,8 @@ typedef enum {
 	CLISH_NSPACE_CHELP
 } clish_nspace_visibility_t;
 
+#include <regex.h>
+
 #include "clish/view.h"
 
 /*=====================================
@@ -49,6 +51,7 @@ void clish_nspace_clean_proxy(clish_nspace_t * instance);
  * attributes
  *----------------- */
 const char *clish_nspace__get_prefix(const clish_nspace_t * instance);
+const regex_t *clish_nspace__get_prefix_regex(const clish_nspace_t * instance);
 bool_t clish_nspace__get_help(const clish_nspace_t * instance);
 bool_t clish_nspace__get_completion(const clish_nspace_t * instance);
 bool_t clish_nspace__get_context_help(const clish_nspace_t * instance);

+ 21 - 13
clish/nspace/nspace.c

@@ -40,8 +40,10 @@ static void clish_nspace_fini(clish_nspace_t * this)
 	clish_command_t *cmd;
 
 	/* deallocate the memory for this instance */
-	lub_string_free(this->prefix);
-	this->prefix = NULL;
+	if (this->prefix) {
+		free(this->prefix);
+		regfree(&this->prefix_regex);
+	}
 	/* delete each command link held by this nspace */
 	while ((cmd = lub_bintree_findfirst(&this->tree))) {
 		/* remove the command from the tree */
@@ -145,11 +147,10 @@ void clish_nspace_delete(clish_nspace_t * this)
 }
 
 /*--------------------------------------------------------- */
-static const char *clish_nspace_after_prefix(const char *prefix,
+static const char *clish_nspace_after_prefix(const regex_t *prefix_regex,
 	const char *line, char **real_prefix)
 {
 	const char *in_line = NULL;
-	regex_t regexp;
 	regmatch_t pmatch[1];
 	int res;
 
@@ -157,9 +158,7 @@ static const char *clish_nspace_after_prefix(const char *prefix,
 		return NULL;
 
 	/* Compile regular expression */
-	regcomp(&regexp, prefix, REG_EXTENDED | REG_ICASE);
-	res = regexec(&regexp, line, 1, pmatch, 0);
-	regfree(&regexp);
+	res = regexec(prefix_regex, line, 1, pmatch, 0);
 	if (res || (0 != pmatch[0].rm_so))
 		return NULL;
 	/* Empty match */
@@ -177,14 +176,14 @@ clish_command_t *clish_nspace_find_command(clish_nspace_t * this, const char *na
 {
 	clish_command_t *cmd = NULL, *retval = NULL;
 	clish_view_t *view = clish_nspace__get_view(this);
-	const char *prefix = clish_nspace__get_prefix(this);
 	const char *in_line;
 	char *real_prefix = NULL;
 
-	if (!prefix)
+	if (!clish_nspace__get_prefix(this))
 		return clish_view_find_command(view, name, this->inherit);
 
-	if (!(in_line = clish_nspace_after_prefix(prefix, name, &real_prefix)))
+	if (!(in_line = clish_nspace_after_prefix(
+		clish_nspace__get_prefix_regex(this), name, &real_prefix)))
 		return NULL;
 
 	/* If prefix is followed by space */
@@ -212,16 +211,16 @@ const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
 {
 	const clish_command_t *cmd = NULL, *retval = NULL;
 	clish_view_t *view = clish_nspace__get_view(this);
-	const char *prefix = clish_nspace__get_prefix(this);
 	const char *in_iter = "";
 	const char *in_line;
 	char *real_prefix = NULL;
 
-	if (!prefix)
+	if (!clish_nspace__get_prefix(this))
 		return clish_view_find_next_completion(view, iter_cmd,
 			line, field, this->inherit);
 
-	if (!(in_line = clish_nspace_after_prefix(prefix, line, &real_prefix)))
+	if (!(in_line = clish_nspace_after_prefix(
+		clish_nspace__get_prefix_regex(this), line, &real_prefix)))
 		return NULL;
 
 	if (in_line[0] != '\0') {
@@ -281,6 +280,7 @@ void clish_nspace__set_prefix(clish_nspace_t * this, const char *prefix)
 {
 	assert(!this->prefix);
 	this->prefix = lub_string_dup(prefix);
+	regcomp(&this->prefix_regex, prefix, REG_EXTENDED | REG_ICASE);
 }
 
 /*--------------------------------------------------------- */
@@ -289,6 +289,14 @@ const char *clish_nspace__get_prefix(const clish_nspace_t * this)
 	return this->prefix;
 }
 
+/*--------------------------------------------------------- */
+const regex_t *clish_nspace__get_prefix_regex(const clish_nspace_t * this)
+{
+	if (!this->prefix)
+		return NULL;
+	return &this->prefix_regex;
+}
+
 /*--------------------------------------------------------- */
 void clish_nspace__set_help(clish_nspace_t * this, bool_t help)
 {

+ 3 - 0
clish/nspace/private.h

@@ -1,6 +1,8 @@
 /*
  * nspace.h
  */
+#include <regex.h>
+
 #include "clish/nspace.h"
 
 /*---------------------------------------------------------
@@ -10,6 +12,7 @@ struct clish_nspace_s {
 	lub_bintree_t tree;	/* Tree of command links */
 	clish_view_t *view;	/* The view to import commands from */
 	char *prefix;		/* if non NULL the prefix for imported commands */
+	regex_t prefix_regex;
 	bool_t help;
 	bool_t completion;
 	bool_t context_help;