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

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

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Feb 18, 2009, 21:13 UTC
Message-ID
<alpine.DEB.1.00.0902182211550.10279@pacific.mpi-cbg.de>
In-Reply-To
<499C63E7.5040306@kdbg.org>
Hi,
On Wed, 18 Feb 2009, Johannes Sixt wrote:
Show 17 quoted lines
> Johannes Schindelin schrieb:
> > 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.
> 
> But you implemented it so that prefix is actually "C:\\msysgit/\\" 
> (unless, of course, I'm reading the code wrong).
This is supposed to handle that case:
	*prefix = xstrndup(path, chomp_trailing_dir_sep(path, path_len));
Show 7 quoted lines
> > +/* sets prefix if the suffix matches */
> > +int strip_path_suffix(const char *path, const char *suffix, const char
> > **prefix)
> 
> For a general purpose function, the API is very unnatural (and geared 
> towards its only user in patch 2/2). I'd expect that the return value is 
> the result or NULL on failure.
Good point.  Will change.
Show 24 quoted lines
> > +{
> > +	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;
> 
> Should strip_path_suffix("foo/bar", "foo/bar", &prefix) succeed and 
> prefix be the empty string? This implementation says it should be so. 
> That's just a question...
Yes, I assumed that that makes sense.

Ciao, Dscho

Previous: Johannes SixtNext: Johannes Sixt
Message 4 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.