Skip to content

Commit 328d567

Browse files
committed
Use saner parent_path? implementation and add tests
1 parent 35a360f commit 328d567

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

apps/common/lib/lexical/path.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ defmodule Lexical.Path do
2323
iex> Lexical.Path.parent_path?("/home/user/docs", "/home/user/docs/subdir")
2424
false
2525
"""
26+
def parent_path?(child_path, parent_path) when byte_size(child_path) < byte_size(parent_path) do
27+
false
28+
end
29+
2630
def parent_path?(child_path, parent_path) do
2731
normalized_child = Path.expand(child_path)
2832
normalized_parent = Path.expand(parent_path)
2933

30-
child_segments = Path.split(normalized_child)
31-
parent_segments = Path.split(normalized_parent)
32-
33-
Enum.take(child_segments, length(parent_segments)) == parent_segments
34+
String.starts_with?(normalized_child, normalized_parent)
3435
end
3536
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule Lexical.PathTest do
2+
use ExUnit.Case, async: true
3+
4+
doctest Lexical.Path
5+
end

0 commit comments

Comments
 (0)