git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Riesen <raa.lkml@gmail.com>
To: Brandon Casey <brandon.casey.ctr@nrlssc.navy.mil>
Cc: Nguyen Thai Ngoc Duy <pclouds@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Git Mailing List <git@vger.kernel.org>
Subject: [PATCH 2/2] Use strbufs instead of open-coded string manipulation
Date: Fri, 26 Mar 2010 23:56:01 +0100	[thread overview]
Message-ID: <20100326225601.GA9177@blimp.localdomain> (raw)
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

  reply	other threads:[~2010-03-26 22:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-25  0:41 What's cooking in git.git (Mar 2010, #06; Wed, 24) Junio C Hamano
2010-03-25 15:11 ` Nguyen Thai Ngoc Duy
2010-03-25 16:27   ` Brandon Casey
2010-03-25 23:11     ` Alex Riesen
2010-03-26 22:53       ` [PATCH 1/2] Allow passing of configuration parameters in the command line Alex Riesen
2010-03-26 22:56         ` Alex Riesen [this message]
2010-03-26  5:47     ` What's cooking in git.git (Mar 2010, #06; Wed, 24) Nguyen Thai Ngoc Duy
2010-03-25 15:16 ` Nguyen Thai Ngoc Duy
2010-03-26  2:19 ` Ben Walton
2010-03-26  8:39   ` [PATCH] RPM spec: include bash completion support Ian Ward Comfort
2010-03-26 12:10     ` Ben Walton
2010-03-29 18:17       ` Junio C Hamano
2010-03-26 13:07     ` Julian Phillips

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100326225601.GA9177@blimp.localdomain \
    --to=raa.lkml@gmail.com \
    --cc=brandon.casey.ctr@nrlssc.navy.mil \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).