git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] Use strbufs instead of open-coded string manipulation

From
Alex Riesen <raa.lkml@gmail.com>
Date
Mar 26, 2010, 22:56 UTC
Message-ID
<20100326225601.GA9177@blimp.localdomain>
In-Reply-To
<20100326225356.GA6797@blimp.localdomain>
Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 config.c |   51 +++++++++++++++++++--------------------------------
 1 files changed, 19 insertions(+), 32 deletions(-)
diff --git a/config.c b/config.c
index 83ebe0c..7a514fa 100644
--- a/config.c
+++ b/config.c
@@ -7,6 +7,7 @@
  */
 #include "cache.h"
 #include "exec_cmd.h"
+#include "strbuf.h"
 
 #define MAXNAME (256)
 
@@ -21,8 +22,8 @@ const char *config_exclusive_filename = NULL;
 struct config_item
 {
 	struct config_item *next;
+	char *name;
 	char *value;
-	char name[1 /* NUL */];
 };
 static struct config_item *config_parameters;
 static struct config_item **config_parameters_tail = &config_parameters;
@@ -32,43 +33,29 @@ static void lowercase(char *p)
 	for (; *p; ++p)
 		*p = tolower(*p);
 }
-static char *skip_space(const char *p)
-{
-	for (; *p; ++p)
-		if (!isspace(*p))
-			break;
-	return (char *)p;
-}
-static char *trailing_space(const char *begin, const char *p)
-{
-	while (p-- > begin)
-		if (!isspace(*p))
-			break;
-	return (char *)p + 1;
-}
 
 int git_config_parse_parameter(const char *text)
 {
 	struct config_item *ct;
-	const char *name;
-	const char *val;
-	name = skip_space(text);
-	text = val = strchr(name, '=');
-	if (!text)
-		text = name + strlen(name);
-	text = trailing_space(name, text);
-	if (text <= name)
+	struct strbuf tmp = STRBUF_INIT;
+	struct strbuf **pair;
+	strbuf_addstr(&tmp, text);
+	pair = strbuf_split(&tmp, '=');
+	if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
+		strbuf_setlen(pair[0], pair[0]->len - 1);
+	strbuf_trim(pair[0]);
+	if (!pair[0]->len) {
+		strbuf_list_free(pair);
 		return -1;
-	ct = xcalloc(1, sizeof(struct config_item) + (text - name));
-	memcpy(ct->name, name, text - name);
-	lowercase(ct->name);
-	if (!val)
-		ct->value = NULL;
-	else {
-		val = skip_space(++val /* skip "=" */);
-		text = trailing_space(val, val + strlen(val));
-		ct->value = xstrndup(val, text - val);
 	}
+	ct = xcalloc(1, sizeof(struct config_item));
+	ct->name = strbuf_detach(pair[0], NULL);
+	if (pair[1]) {
+		strbuf_trim(pair[1]);
+		ct->value = strbuf_detach(pair[1], NULL);
+	}
+	strbuf_list_free(pair);
+	lowercase(ct->name);
 	*config_parameters_tail = ct;
 	config_parameters_tail = &ct->next;
 	return 0;
-- 
1.7.0.3.309.g532f0
Previous: Alex RiesenNext: Nguyen Thai Ngoc Duy
Message 6 of 13 in “What's cooking in git.git (Mar 2010, #06; Wed, 24)”
  1. Junio C HamanoMar 25, 2010
  2. Nguyen Thai Ngoc DuyMar 25, 2010
  3. Brandon CaseyMar 25, 2010
  4. Alex RiesenMar 25, 2010
  5. 1/2 Allow passing of configuration parameters in the command lineAlex Riesen, Mar 26, 2010
  6. 2/2 Use strbufs instead of open-coded string manipulationAlex Riesen, Mar 26, 2010
  7. Nguyen Thai Ngoc DuyMar 26, 2010
  8. Nguyen Thai Ngoc DuyMar 25, 2010
  9. Ben WaltonMar 26, 2010
  10. RPM spec: include bash completion supportIan Ward Comfort, Mar 26, 2010
  11. Ben WaltonMar 26, 2010
  12. Junio C HamanoMar 29, 2010
  13. Julian PhillipsMar 26, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.