Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Implement Xgit.Core.FilePath.starts_with?/2. #146

Merged
merged 1 commit into from Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/xgit/core/file_path.ex
Expand Up @@ -534,6 +534,18 @@ defmodule Xgit.Core.FilePath do
defp to_lower(b) when b >= ?A and b <= ?Z, do: cover(b + 32)
defp to_lower(b), do: cover(b)

@doc ~S"""
Returns `true` if `path` starts with `prefix`.

Unlike `String.starts_with?/2`, only accepts a single prefix path.
"""
@spec starts_with?(path :: t, prefix :: t) :: boolean
def starts_with?(path, prefix)

def starts_with?(_path, []), do: cover(true)
def starts_with?([c | path], [c | prefix]), do: starts_with?(path, prefix)
def starts_with?(_path, _prefix), do: cover(false)

@doc ~S"""
Ensure that a trailing `/` is present.

Expand Down
11 changes: 11 additions & 0 deletions test/xgit/core/file_path_test.exs
Expand Up @@ -491,6 +491,17 @@ defmodule Xgit.Core.FilePathTest do
end
end

test "starts_with?/2" do
assert starts_with?('', '')
assert starts_with?('abc', '')
assert starts_with?('abc', 'a')
assert starts_with?('abc', 'abc')
refute starts_with?('xabc', 'abc')
refute starts_with?('abc', 'abd')
refute starts_with?('ab', 'abc')
refute starts_with?('', 'a')
end

describe "ensure_trailing_separator/1" do
test "empty list" do
assert ensure_trailing_separator([]) == []
Expand Down