Browse Source

Optimization for lub_string_decode(), lub_argv_init() etc.

git-svn-id: https://klish.googlecode.com/svn/trunk@409 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
faee3427a8
4 changed files with 16 additions and 16 deletions
  1. 1 5
      lub/argv/argv_new.c
  2. 5 4
      lub/argv/argv_nextword.c
  3. 1 0
      lub/string.h
  4. 9 7
      lub/string/string_escape.c

+ 1 - 5
lub/argv/argv_new.c

@@ -21,10 +21,8 @@ static void lub_argv_init(lub_argv_t * this, const char *line, size_t offset)
 		this->argc = 0;
 		return;
 	}
-
 	/* first of all count the words in the line */
 	this->argc = lub_argv_wordcount(line);
-
 	/* allocate space to hold the vector */
 	arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
 		assert(arg);
@@ -33,9 +31,7 @@ static void lub_argv_init(lub_argv_t * this, const char *line, size_t offset)
 	for (word = lub_argv_nextword(line, &len, &offset, &quoted);
 		*word;
 		word = lub_argv_nextword(word + len, &len, &offset, &quoted)) {
-		char *tmp = lub_string_dupn(word, len);
-		(*arg).arg = lub_string_decode(tmp);
-		lub_string_free(tmp);
+		(*arg).arg = lub_string_ndecode(word, len);
 		(*arg).offset = offset;
 		(*arg).quoted = quoted;
 

+ 5 - 4
lub/argv/argv_nextword.c

@@ -1,11 +1,12 @@
 /*
  * argv_nextword.c
  */
+#include <stddef.h>
+#include <ctype.h>
+
 #include "private.h"
 #include "lub/types.h"
-#include "lub/ctype.h"
 
-#include <stddef.h>
 /*--------------------------------------------------------- */
 const char *lub_argv_nextword(const char *string,
 	size_t * len, size_t * offset, bool_t * quoted)
@@ -16,7 +17,7 @@ const char *lub_argv_nextword(const char *string,
 	*quoted = BOOL_FALSE;
 
 	/* find the start of a word (not including an opening quote) */
-	while (*string && lub_ctype_isspace(*string)) {
+	while (*string && isspace(*string)) {
 		string++;
 		(*offset)++;
 	}
@@ -44,7 +45,7 @@ const char *lub_argv_nextword(const char *string,
 			}
 			continue;
 		}
-		if ((BOOL_FALSE == quote) && lub_ctype_isspace(*string)) {
+		if ((BOOL_FALSE == quote) && isspace(*string)) {
 			/* end of word */
 			break;
 		}

+ 1 - 0
lub/string.h

@@ -232,6 +232,7 @@ void lub_string_free(
  * - The result string must be freed after using.
  */
 char *lub_string_decode(const char *string);
+char *lub_string_ndecode(const char *string, unsigned int len);
 
 /**
  * This operation encode the string using escape.

+ 9 - 7
lub/string/string_escape.c

@@ -17,7 +17,7 @@
 static const char *default_escape_chars = "`|$<>&()#;";
 
 /*--------------------------------------------------------- */
-char *lub_string_decode(const char *string)
+char *lub_string_ndecode(const char *string, unsigned int len)
 {
 	const char *s = string;
 	char *res, *p;
@@ -27,9 +27,9 @@ char *lub_string_decode(const char *string)
 		return NULL;
 
 	/* Allocate enough memory for result */
-	p = res = malloc(strlen(string) + 1);
+	p = res = malloc(len + 1);
 
-	while (*s) {
+	while (*s && (s < (string +len))) {
 		if (!esc) {
 			if ('\\' == *s)
 				esc = 1;
@@ -56,11 +56,13 @@ char *lub_string_decode(const char *string)
 	}
 	*p = '\0';
 
-	/* Optimize the memory allocated for result */
-	p = lub_string_dup(res);
-	free(res);
+	return res;
+}
 
-	return p;
+/*--------------------------------------------------------- */
+inline char *lub_string_decode(const char *string)
+{
+	return lub_string_ndecode(string, strlen(string));
 }
 
 /*----------------------------------------------------------- */