# Cache: Fix findPrefix when paths are slightly out of the ordinary · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/020105d0dde614538a5839ede697e63a43bf6aa6)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 020105d0dde614538a5839ede697e63a43bf6aa6

Requested commit: 020105d0dde614538a5839ede697e63a43bf6aa6

Commit: 020105d0dde614538a5839ede697e63a43bf6aa6

Tree: cf82ea1142d2bf8367049e69a794633d639fbbd5

Author: Ryan Liptak

Committer: Andrew Kelley

## Message

```
Cache: Fix findPrefix when paths are slightly out of the ordinary

This makes Cache.findPrefix/findPrefixResolved use `std.fs.path.relative` instead of `std.mem.startsWith` when checking if a file is within a prefix. This fixes multiple edge cases around prefix detection:

- If a prefix path ended with a path separator, then the first character of the 'sub_path' would get cut off because the previous implementation assumed it was a path separator. Example: prefix: `/foo/`, file_path: `/foo/abc.txt` would see that they both start with `/foo/` and then slice starting from one byte past the common prefix, ending up with `bc.txt` instead of the expected `abc.txt`
- If a prefix contained double path separators after any component, then the `startsWith` check would erroneously fail. Example: prefix: `/foo//bar`, file_path: `/foo/bar/abc.txt` would not see that abc.txt is a sub path of the prefix `/foo//bar`
- On Windows, case insensitivity was not respected at all, instead the UTF-8 bytes were compared directly

This fixes all of the things in the above list (and possibly more).

```

## Parents

- [a11cdb6a34de8820826e49ba54459c3abd4aca4e](https://git.cafe/gitcafe/zig/commit/a11cdb6a34de8820826e49ba54459c3abd4aca4e?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/020105d0dde614538a5839ede697e63a43bf6aa6?format=markdown)
