remove_dot_segments seems to turn relative paths into absolute paths

178 Views Asked by At

"5.2.4. Remove Dot Segments" of Std 66 explains how to convert a path with . and .. into one without.

IIUC, the input foo/../bar is processed as follows.

  1. Per step 1, the (input buffer, output buffer) are initialized to ("foo/../bar", "")
  2. Step 2.E is applied to get ("/../bar", "foo")
  3. Step 2.C is applied to get ("/bar", "")
  4. Step 2.E is applied to get ("", "/bar")
  5. The step 2 loop exits
  6. Per step 3, the result is /bar.

It seems odd to me that the input is a relative path, but the output is an absolute path.

It seems to me this could be fixed by, in Step 1, setting isAbsolute to true iff the input buffer starts with a / and changing Step 2.C from

C. if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; otherwise,

to

C. if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer, then if not isAbsolute and the output buffer is empty, remove the "/" from the front of the input buffer; otherwise,


Am I misreading the spec? Is that the desired result for some reason I'm not seeing?

I put together a gist with a Java implementation of my understanding of the spec and the "fix" I proposed.

0

There are 0 best solutions below