瀏覽代碼

libdpkg: Add new str_match_end() function

Guillem Jover 12 年之前
父節點
當前提交
9933a758c0
共有 4 個文件被更改,包括 37 次插入4 次删除
  1. 1 0
      lib/dpkg/libdpkg.map
  2. 22 1
      lib/dpkg/string.c
  3. 3 1
      lib/dpkg/string.h
  4. 11 2
      lib/dpkg/test/t-string.c

+ 1 - 0
lib/dpkg/libdpkg.map

@@ -67,6 +67,7 @@ LIBDPKG_PRIVATE {
 
 	cisdigit;
 
+	str_match_end;
 	str_escape_fmt;
 	str_strip_quotes;
 	str_quote_meta;

+ 22 - 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, 2009 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2014 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 published by
@@ -27,6 +27,27 @@
 #include <dpkg/string.h>
 #include <dpkg/dpkg.h>
 
+/**
+ * Match the end of a string.
+ *
+ * @param str The string.
+ * @param end The end to match in str.
+ *
+ * @return Whether the string was matched at the end.
+ */
+bool
+str_match_end(const char *str, const char *end)
+{
+	size_t str_len = strlen(str);
+	size_t end_len = strlen(end);
+	const char *str_end = str + str_len - end_len;
+
+	if (str_len >= end_len && strcmp(str_end, end) == 0)
+		return true;
+	else
+		return false;
+}
+
 /**
  * Escape format characters from a string.
  *

+ 3 - 1
lib/dpkg/string.h

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * string.h - string handling routines
  *
- * Copyright © 2008-2013 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2014 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 published by
@@ -51,6 +51,8 @@ str_is_set(const char *str)
 	return str != NULL && str[0] != '\0';
 }
 
+bool str_match_end(const char *str, const char *end);
+
 char *str_escape_fmt(char *dest, const char *src, size_t n);
 char *str_quote_meta(const char *src);
 char *str_strip_quotes(char *str);

+ 11 - 2
lib/dpkg/test/t-string.c

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * t-string.c - test string handling
  *
- * Copyright © 2009 Guillem Jover <guillem@debian.org>
+ * Copyright © 2009-2011, 2014 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 published by
@@ -27,6 +27,14 @@
 #include <stdlib.h>
 #include <string.h>
 
+static void
+test_str_match_end(void)
+{
+	test_pass(str_match_end("foo bar quux", "quux"));
+	test_pass(str_match_end("foo bar quux", "bar quux"));
+	test_pass(str_match_end("foo bar quux", "foo bar quux"));
+}
+
 static void
 test_str_escape_fmt(void)
 {
@@ -127,8 +135,9 @@ test_str_strip_quotes(void)
 static void
 test(void)
 {
-	test_plan(18);
+	test_plan(21);
 
+	test_str_match_end();
 	test_str_escape_fmt();
 	test_str_quote_meta();
 	test_str_strip_quotes();