git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 01/14] move string functions out of git-compat-util

From
Jeff King <peff@peff.net>
Date
Dec 29, 2015, 07:19 UTC
Message-ID
<20151229071936.GA8842@sigill.intra.peff.net>
In-Reply-To
<20151229071847.GA8726@sigill.intra.peff.net>

We have several inline string helpers (skip_prefix, etc) defined in git-compat-util.h. This is convenient, as it makes them available everywhere, but this is probably not the best place for them. Besides cluttering a file which is otherwise mostly about compatibility issues, defining inline functions in git-compat-util.h restricts what they can do.

For instance, if we wanted to use tolower() in one of the functions, we run into an ordering problem with our redefinitions of tolower(), which are done later in git-compat-util.h.

This patch moves them to their own header file, strutil.h. However, it also includes them in strbuf.h (which needs them for its inline strbuf_strip_suffix). This also puts them indirectly into cache.h, which means they are effectively available everywhere (just as they were before).

Signed-off-by: Jeff King <peff@peff.net>
---
 git-compat-util.h | 63 ---------------------------------------------------
 strbuf.h          |  2 ++
 strutil.h         | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 63 deletions(-)
 create mode 100644 strutil.h
diff --git a/git-compat-util.h b/git-compat-util.h
index 2da0a75..da67dc6 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -430,69 +430,6 @@ extern void set_error_routine(void (*routine)(const char *err, va_list params));
 extern void set_die_is_recursing_routine(int (*routine)(void));
 extern void set_error_handle(FILE *);
 
-extern int starts_with(const char *str, const char *prefix);
-
-/*
- * If the string "str" begins with the string found in "prefix", return 1.
- * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
- * the string right after the prefix).
- *
- * Otherwise, return 0 and leave "out" untouched.
- *
- * Examples:
- *
- *   [extract branch name, fail if not a branch]
- *   if (!skip_prefix(ref, "refs/heads/", &branch)
- *	return -1;
- *
- *   [skip prefix if present, otherwise use whole string]
- *   skip_prefix(name, "refs/heads/", &name);
- */
-static inline int skip_prefix(const char *str, const char *prefix,
-			      const char **out)
-{
-	do {
-		if (!*prefix) {
-			*out = str;
-			return 1;
-		}
-	} while (*str++ == *prefix++);
-	return 0;
-}
-
-/*
- * If buf ends with suffix, return 1 and subtract the length of the suffix
- * from *len. Otherwise, return 0 and leave *len untouched.
- */
-static inline int strip_suffix_mem(const char *buf, size_t *len,
-				   const char *suffix)
-{
-	size_t suflen = strlen(suffix);
-	if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
-		return 0;
-	*len -= suflen;
-	return 1;
-}
-
-/*
- * If str ends with suffix, return 1 and set *len to the size of the string
- * without the suffix. Otherwise, return 0 and set *len to the size of the
- * string.
- *
- * Note that we do _not_ NUL-terminate str to the new length.
- */
-static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
-{
-	*len = strlen(str);
-	return strip_suffix_mem(str, len, suffix);
-}
-
-static inline int ends_with(const char *str, const char *suffix)
-{
-	size_t len;
-	return strip_suffix(str, suffix, &len);
-}
-
 #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
 
 #ifndef PROT_READ
diff --git a/strbuf.h b/strbuf.h
index 7123fca..a1a2df9 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -1,6 +1,8 @@
 #ifndef STRBUF_H
 #define STRBUF_H
 
+#include "strutil.h"
+
 /**
  * strbuf's are meant to be used with all the usual C string and memory
  * APIs. Given that the length of the buffer is known, it's often better to
diff --git a/strutil.h b/strutil.h
new file mode 100644
index 0000000..4fa2071
--- /dev/null
+++ b/strutil.h
@@ -0,0 +1,67 @@
+#ifndef STRUTIL_H
+#define STRUTIL_H
+
+extern int starts_with(const char *str, const char *prefix);
+
+/*
+ * If the string "str" begins with the string found in "prefix", return 1.
+ * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
+ * the string right after the prefix).
+ *
+ * Otherwise, return 0 and leave "out" untouched.
+ *
+ * Examples:
+ *
+ *   [extract branch name, fail if not a branch]
+ *   if (!skip_prefix(ref, "refs/heads/", &branch)
+ *	return -1;
+ *
+ *   [skip prefix if present, otherwise use whole string]
+ *   skip_prefix(name, "refs/heads/", &name);
+ */
+static inline int skip_prefix(const char *str, const char *prefix,
+			      const char **out)
+{
+	do {
+		if (!*prefix) {
+			*out = str;
+			return 1;
+		}
+	} while (*str++ == *prefix++);
+	return 0;
+}
+
+/*
+ * If buf ends with suffix, return 1 and subtract the length of the suffix
+ * from *len. Otherwise, return 0 and leave *len untouched.
+ */
+static inline int strip_suffix_mem(const char *buf, size_t *len,
+				   const char *suffix)
+{
+	size_t suflen = strlen(suffix);
+	if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
+		return 0;
+	*len -= suflen;
+	return 1;
+}
+
+/*
+ * If str ends with suffix, return 1 and set *len to the size of the string
+ * without the suffix. Otherwise, return 0 and set *len to the size of the
+ * string.
+ *
+ * Note that we do _not_ NUL-terminate str to the new length.
+ */
+static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
+{
+	*len = strlen(str);
+	return strip_suffix_mem(str, len, suffix);
+}
+
+static inline int ends_with(const char *str, const char *suffix)
+{
+	size_t len;
+	return strip_suffix(str, suffix, &len);
+}
+
+#endif /* STRUTIL_H */
-- 
2.7.0.rc3.367.g09631da
Previous: Jeff KingNext: Jeff King
Message 2 of 29 in “counting trailers with shortlogs”
  1. 0/14 counting trailers with shortlogsJeff King, Dec 29, 2015
  2. 01/14 move string functions out of git-compat-utilJeff King, Dec 29, 2015
  3. 02/14 log: refactor add_header to drop some magic numbersJeff King, Dec 29, 2015
  4. Eric SunshineDec 31, 2015
  5. Jeff KingJan 1, 2016
  6. Jeff KingJan 1, 2016
  7. 03/14 strutil: add skip_prefix_icaseJeff King, Dec 29, 2015
  8. Eric SunshineDec 31, 2015
  9. Jeff KingJan 1, 2016
  10. 04/14 shortlog: use skip_prefix_icase to parse "Author" linesJeff King, Dec 29, 2015
  11. Eric SunshineDec 31, 2015
  12. Jeff KingJan 1, 2016
  13. 05/14 shortlog: use strbufs to read from stdinJeff King, Dec 29, 2015
  14. 06/14 shortlog: replace hand-parsing of author with pretty-printerJeff King, Dec 29, 2015
  15. Eric SunshineJan 4, 2016
  16. Jeff KingJan 4, 2016
  17. 07/14 shortlog: optimize "--summary" modeJeff King, Dec 29, 2015
  18. 08/14 shortlog: optimize out useless "<none>" normalizationJeff King, Dec 29, 2015
  19. 09/14 shortlog: optimize out useless string listJeff King, Dec 29, 2015
  20. 10/14 shortlog: change "author" variables to "ident"Jeff King, Dec 29, 2015
  21. 11/14 shortlog: allow grouping by committer identJeff King, Dec 29, 2015
  22. Eric SunshineJan 4, 2016
  23. Jeff KingJan 4, 2016
  24. 12/14 trailer: factor out config readingJeff King, Dec 29, 2015
  25. 13/14 trailer: add interface for parsing commit trailersJeff King, Dec 29, 2015
  26. 14/14 shortlog: match commit trailers with --identJeff King, Dec 29, 2015
  27. Jeff KingDec 29, 2015
  28. Eric SunshineJan 4, 2016
  29. Jeff KingJan 4, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.