File tree Expand file tree Collapse file tree 2 files changed +10
-7
lines changed Expand file tree Collapse file tree 2 files changed +10
-7
lines changed Original file line number Diff line number Diff line change @@ -6,9 +6,6 @@ defmodule Lexical.Path do
6
6
@ doc """
7
7
Checks if the `parent_path` is a parent directory of the `child_path`.
8
8
9
- This function normalizes both paths and compares their segments to determine
10
- if the `parent_path` is a prefix of the `child_path`.
11
-
12
9
## Examples
13
10
14
11
iex> Lexical.Path.parent_path?("/home/user/docs/file.txt", "/home/user")
@@ -23,13 +20,14 @@ defmodule Lexical.Path do
23
20
iex> Lexical.Path.parent_path?("/home/user/docs", "/home/user/docs/subdir")
24
21
false
25
22
"""
23
+ def parent_path? ( child_path , parent_path ) when byte_size ( child_path ) < byte_size ( parent_path ) do
24
+ false
25
+ end
26
+
26
27
def parent_path? ( child_path , parent_path ) do
27
28
normalized_child = Path . expand ( child_path )
28
29
normalized_parent = Path . expand ( parent_path )
29
30
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
31
+ String . starts_with? ( normalized_child , normalized_parent )
34
32
end
35
33
end
Original file line number Diff line number Diff line change
1
+ defmodule Lexical.PathTest do
2
+ use ExUnit.Case , async: true
3
+
4
+ doctest Lexical.Path
5
+ end
You can’t perform that action at this time.
0 commit comments