The following commit has been merged in the master branch:
commit 5410b4904eeb830c858daaf30420523122ec4042
Author: Guillem Jover <[email protected]>
Date:   Fri Sep 4 14:26:32 2009 +0200

    libdpkg: Add new str_strip_quotes function

diff --git a/lib/dpkg/string.c b/lib/dpkg/string.c
index b777bd5..e98d388 100644
--- a/lib/dpkg/string.c
+++ b/lib/dpkg/string.c
@@ -3,7 +3,7 @@
  * string.c - string handling routines
  *
  * Copyright © 1995 Ian Jackson <[email protected]>
- * Copyright © 2008 Guillem Jover <[email protected]>
+ * Copyright © 2008, 2009 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
@@ -23,6 +23,8 @@
 #include <config.h>
 #include <compat.h>
 
+#include <string.h>
+
 #include <dpkg/string.h>
 
 char *
@@ -42,3 +44,21 @@ str_escape_fmt(char *dst, const char *src)
        return d;
 }
 
+/* Check and strip possible surrounding quotes in string. */
+char *
+str_strip_quotes(char *str)
+{
+       if (str[0] == '"' || str[0] == '\'') {
+               size_t str_len = strlen(str);
+
+               if (str[0] != str[str_len - 1])
+                       return NULL;
+
+               /* Remove surrounding quotes. */
+               str[str_len - 1] = '\0';
+               str++;
+       }
+
+       return str;
+}
+
diff --git a/lib/dpkg/string.h b/lib/dpkg/string.h
index c50d48c..a7bc5ae 100644
--- a/lib/dpkg/string.h
+++ b/lib/dpkg/string.h
@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * string.h - string handling routines
  *
- * Copyright © 2008 Guillem Jover <[email protected]>
+ * Copyright © 2008, 2009 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
@@ -27,6 +27,7 @@
 DPKG_BEGIN_DECLS
 
 char *str_escape_fmt(char *dest, const char *src);
+char *str_strip_quotes(char *str);
 
 DPKG_END_DECLS
 
diff --git a/lib/dpkg/test/t-string.c b/lib/dpkg/test/t-string.c
index 66e6680..e2b957e 100644
--- a/lib/dpkg/test/t-string.c
+++ b/lib/dpkg/test/t-string.c
@@ -51,8 +51,51 @@ test_str_escape_fmt(void)
 }
 
 static void
+test_str_strip_quotes(void)
+{
+       char buf[1024], *str;
+
+       strcpy(buf, "unquoted text");
+       str = str_strip_quotes(buf);
+       test_str(str, ==, "unquoted text");
+
+       strcpy(buf, "contained 'quoted text'");
+       str = str_strip_quotes(buf);
+       test_str(str, ==, "contained 'quoted text'");
+
+       strcpy(buf, "contained \"quoted text\"");
+       str = str_strip_quotes(buf);
+       test_str(str, ==, "contained \"quoted text\"");
+
+       strcpy(buf, "'unbalanced quotes");
+       str = str_strip_quotes(buf);
+       test_pass(str == NULL);
+
+       strcpy(buf, "\"unbalanced quotes");
+       str = str_strip_quotes(buf);
+       test_pass(str == NULL);
+
+       strcpy(buf, "'mismatched quotes\"");
+       str = str_strip_quotes(buf);
+       test_pass(str == NULL);
+
+       strcpy(buf, "\"mismatched quotes'");
+       str = str_strip_quotes(buf);
+       test_pass(str == NULL);
+
+       strcpy(buf, "'completely quoted text'");
+       str = str_strip_quotes(buf);
+       test_str(str, ==, "completely quoted text");
+
+       strcpy(buf, "\"completely quoted text\"");
+       str = str_strip_quotes(buf);
+       test_str(str, ==, "completely quoted text");
+}
+
+static void
 test(void)
 {
        test_str_escape_fmt();
+       test_str_strip_quotes();
 }
 

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to