Talk:Find common directory path: Difference between revisions

no edit summary
No edit summary
No edit summary
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)
 
: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]]However, 14:22that does not mean that the algorithm is completely valid. Consider, 14for Aprilexample, 2011what happens if you change an instance of "home" to "hone". Here's my proposed alternative: <lang c>static void longestSharedPath(UTCconst 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)
6,951

edits