Просмотр исходного кода

libdpkg: Add new str_strip_quotes function

Guillem Jover лет назад: 17
Родитель
Сommit
5410b4904e
3 измененных файлов с 66 добавлено и 2 удалено
  1. 21 1
      lib/dpkg/string.c
  2. 2 1
      lib/dpkg/string.h
  3. 43 0
      lib/dpkg/test/t-string.c

+ 21 - 1
lib/dpkg/string.c

@@ -3,7 +3,7 @@
  * string.c - string handling routines
  *
  * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
- * Copyright © 2008 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
  *
  * 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;
+}
+

+ 2 - 1
lib/dpkg/string.h

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * string.h - string handling routines
  *
- * Copyright © 2008 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
  *
  * 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
 

+ 43 - 0
lib/dpkg/test/t-string.c

@@ -50,9 +50,52 @@ test_str_escape_fmt(void)
 	test_str(buf, ==, "%%b%%b%%c%%c%%%% end");
 }
 
+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();
 }