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

[PATCH 1/2] Introduce the function strip_path_suffix()

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Feb 18, 2009, 15:11 UTC
Message-ID
<7b5ee74d497bbb7144b8683725cd83c8bbd33f9a.1234969572u.git.johannes.schindelin@gmx.de>
In-Reply-To
<cover.1234969572u.git.johannes.schindelin@gmx.de>

The function strip_path_suffix() will try to split the given path into prefix/suffix. The suffix has to be passed to the function, and if the path matches, the prefix is set.

Arbitrary runs of directory separators ("slashes") are assumed identical.
Example:
	strip_path_suffix("C:\\msysgit/\\libexec\\git-core",
		"libexec///git-core", &prefix)
will set prefix to "C:\\msysgit" and return 0.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 cache.h |    2 ++
 path.c  |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/cache.h b/cache.h
index 4e28c24..2186143 100644
--- a/cache.h
+++ b/cache.h
@@ -629,6 +629,8 @@ const char *make_nonrelative_path(const char *path);
 const char *make_relative_path(const char *abs, const char *base);
 int normalize_path_copy(char *dst, const char *src);
 int longest_ancestor_length(const char *path, const char *prefix_list);
+int strip_path_suffix(const char *path, const char *suffix,
+		const char **prefix);
 
 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
 extern int sha1_object_info(const unsigned char *, unsigned long *);
diff --git a/path.c b/path.c
index 4b9107f..947e5b7 100644
--- a/path.c
+++ b/path.c
@@ -499,3 +499,36 @@ int longest_ancestor_length(const char *path, const char *prefix_list)
 
 	return max_len;
 }
+
+/* strip arbitrary amount of directory separators at end of path */
+static inline int chomp_trailing_dir_sep(const char *path, int len)
+{
+	while (len && is_dir_sep(path[len - 1]))
+		len--;
+	return len;
+}
+
+/* sets prefix if the suffix matches */
+int strip_path_suffix(const char *path, const char *suffix, const char **prefix)
+{
+	int path_len = strlen(path), suffix_len = strlen(suffix);
+
+	while (suffix_len) {
+		if (!path_len)
+			return 1;
+
+		if (is_dir_sep(path[path_len - 1])) {
+			if (!is_dir_sep(suffix[suffix_len - 1]))
+				return 1;
+			path_len = chomp_trailing_dir_sep(path, path_len);
+			suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
+		}
+		else if (path[--path_len] != suffix[--suffix_len])
+			return 1;
+	}
+
+	if (path_len && !is_dir_sep(path[path_len - 1]))
+		return 1;
+	*prefix = xstrndup(path, chomp_trailing_dir_sep(path, path_len));
+	return 0;
+}
-- 
1.6.1.1.825.g72a9f
Previous: Johannes SchindelinNext: Johannes Sixt
Message 2 of 20 in “RUNTIME_PREFIX enhancements”
  1. 0/2 RUNTIME_PREFIX enhancementsJohannes Schindelin, Feb 18, 2009
  2. 1/2 Introduce the function strip_path_suffix()Johannes Schindelin, Feb 18, 2009
  3. Johannes SixtFeb 18, 2009
  4. Johannes SchindelinFeb 18, 2009
  5. Johannes SixtFeb 18, 2009
  6. Johannes SchindelinFeb 18, 2009
  7. 0/2 RUNTIME_PREFIX enhancementsJohannes Schindelin, Feb 19, 2009
  8. 1/2 Introduce the function strip_path_suffix()Johannes Schindelin, Feb 19, 2009
  9. Johannes SchindelinFeb 19, 2009
  10. Johannes SixtFeb 19, 2009
  11. Johannes SchindelinFeb 19, 2009
  12. 2/2 system_path(): simplify using strip_path_suffix(), and add suffix "git"Johannes Schindelin, Feb 19, 2009
  13. Johannes SixtFeb 19, 2009
  14. Johannes SchindelinFeb 19, 2009
  15. Junio C HamanoFeb 20, 2009
  16. 2/2 system_path(): simplify using strip_path_suffix(), and add suffix "git"Johannes Schindelin, Feb 18, 2009
  17. Johannes SixtFeb 18, 2009
  18. Johannes SchindelinFeb 18, 2009
  19. Johannes SixtFeb 18, 2009
  20. Johannes SchindelinFeb 18, 2009

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.