Talk:Find common directory path: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 39: Line 39:
It is intended to work that way, and it works with "dovetail" also. The first conditional handles cases both with and without terminating slash. Try it. [[User:Per|Per]] 12:07, 14 April 2011 (UTC)
It is intended to work that way, and it works with "dovetail" also. The first conditional handles cases both with and without terminating slash. Try it. [[User:Per|Per]] 12:07, 14 April 2011 (UTC)


:ok, yes, nevermind. And the case I was worried about would have appeared with the provided example but is dealt with by a prior test. --[[User:Rdm|Rdm]] 14:22, 14 April 2011 (UTC)
:ok, yes, nevermind. And the case I was worried about would have appeared with the provided example but is dealt with by a prior test. However, that does not mean that the algorithm is completely valid. Consider, for example, what happens if you change an instance of "home" to "hone". Here's my proposed alternative: <lang c>static void longestSharedPath(const char *fixed, char *moving) {
char *t;
unsigned n = 0;
while (moving[n] == fixed[n] && moving[n]) n++;
if (!moving[n]) return;
t = strrchr(moving, '/');
if (t)
if (t == moving)
moving[1]= '\0';
else
*t = '\0';
}</lang> --[[User:Rdm|Rdm]] 14:22, 14 April 2011 (UTC)