瀏覽代碼

libdpkg: Add new str_fmt()

This function is easier and more natural to use than m_asprintf(), and
we currently never use the returned length anyway.
Guillem Jover 10 年之前
父節點
當前提交
302829039a
共有 5 個文件被更改,包括 43 次插入4 次删除
  1. 1 0
      lib/dpkg/dpkg.h
  2. 1 0
      lib/dpkg/libdpkg.map
  3. 22 1
      lib/dpkg/string.c
  4. 2 1
      lib/dpkg/string.h
  5. 17 2
      lib/dpkg/t/t-string.c

+ 1 - 0
lib/dpkg/dpkg.h

@@ -116,6 +116,7 @@ DPKG_BEGIN_DECLS
 #include <dpkg/progname.h>
 #include <dpkg/ehandle.h>
 #include <dpkg/report.h>
+#include <dpkg/string.h>
 #include <dpkg/program.h>
 
 /*** log.c ***/

+ 1 - 0
lib/dpkg/libdpkg.map

@@ -79,6 +79,7 @@ LIBDPKG_PRIVATE {
 
 	str_match_end;
 	str_fnv_hash;
+	str_fmt;
 	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 <ijackson@chiark.greenend.org.uk>
- * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2015 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
@@ -49,6 +49,27 @@ str_match_end(const char *str, const char *end)
 		return false;
 }
 
+/**
+ * Print formatted output to an allocated string.
+ *
+ * @param fmt The format string.
+ * @param ... The format arguments.
+ *
+ * @return The new allocated formatted output string (never NULL).
+ */
+char *
+str_fmt(const char *fmt, ...)
+{
+	va_list args;
+	char *str;
+
+	va_start(args, fmt);
+	m_vasprintf(&str, fmt, args);
+	va_end(args);
+
+	return str;
+}
+
 /**
  * Escape format characters from a string.
  *

+ 2 - 1
lib/dpkg/string.h

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * string.h - string handling routines
  *
- * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2015 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
@@ -56,6 +56,7 @@ bool str_match_end(const char *str, const char *end);
 
 unsigned int str_fnv_hash(const char *str);
 
+char *str_fmt(const char *fmt, ...) DPKG_ATTR_PRINTF(1);
 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);

+ 17 - 2
lib/dpkg/t/t-string.c

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * t-string.c - test string handling
  *
- * Copyright © 2009-2011, 2014 Guillem Jover <guillem@debian.org>
+ * Copyright © 2009-2011, 2014-2015 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
@@ -76,6 +76,20 @@ test_str_fnv_hash(void)
 	test_pass(str_fnv_hash("Rest-string") == 0x20464b9fUL);
 }
 
+static void
+test_str_fmt(void)
+{
+	char *str;
+
+	str = str_fmt("%s", "abcde");
+	test_str(str, ==, "abcde");
+	free(str);
+
+	str = str_fmt("%d", 15);
+	test_str(str, ==, "15");
+	free(str);
+}
+
 static void
 test_str_escape_fmt(void)
 {
@@ -184,11 +198,12 @@ test_str_strip_quotes(void)
 static void
 test(void)
 {
-	test_plan(48);
+	test_plan(50);
 
 	test_str_is_set();
 	test_str_match_end();
 	test_str_fnv_hash();
+	test_str_fmt();
 	test_str_escape_fmt();
 	test_str_quote_meta();
 	test_str_strip_quotes();