This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
109 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Xgit.Util.ParseHeader do | ||
@moduledoc false | ||
# Internal utility for parsing headers from commit and tag objects. | ||
|
||
import Xgit.Util.ForceCoverage | ||
|
||
@doc ~S""" | ||
Returns the next header that can be parsed from the charlist `b`. | ||
As of this writing, will not parse headers that span multiple lines. | ||
This may be added later if needed. | ||
## Return Values | ||
`{'header_name', 'header_value', next_data}` if a header is successfully | ||
identified. `next_data` will be advanced immediately past the LF that | ||
terminates this header. | ||
`:no_header_found` if unable to find a header at this location. | ||
""" | ||
@spec next_header(b :: charlist) :: | ||
{header :: charlist, value :: charlist, next_data :: charlist} | ||
def next_header(b) when is_list(b) do | ||
with {[_ | _] = header, [?\s | next]} <- Enum.split_while(b, &header_char?/1), | ||
{value, next} <- Enum.split_while(next, &value_char?/1) do | ||
cover {header, value, skip_next_lf(next)} | ||
else | ||
_ -> cover :no_header_found | ||
end | ||
end | ||
|
||
defp header_char?(32), do: cover(false) | ||
defp header_char?(10), do: cover(false) | ||
defp header_char?(_), do: cover(true) | ||
|
||
defp value_char?(10), do: cover(false) | ||
defp value_char?(_), do: cover(true) | ||
|
||
defp skip_next_lf([10 | next]), do: cover(next) | ||
defp skip_next_lf(next), do: cover(next) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Xgit.Util.ParseHeaderTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Xgit.Util.ParseHeader | ||
|
||
describe "next_header/1" do | ||
test "happy path" do | ||
assert {'tree', 'abcdef', 'what remains\n'} = | ||
ParseHeader.next_header(~C""" | ||
tree abcdef | ||
what remains | ||
""") | ||
end | ||
|
||
test "happy path (last line)" do | ||
assert {'tree', 'abcdef', []} = | ||
ParseHeader.next_header(~C""" | ||
tree abcdef | ||
""") | ||
end | ||
|
||
test "happy path (no trailing LF)" do | ||
assert {'tree', 'abcdef', []} = ParseHeader.next_header('tree abcdef') | ||
end | ||
|
||
test "no header value" do | ||
assert :no_header_found = ParseHeader.next_header('abc') | ||
end | ||
|
||
test "no header value (trailing LF)" do | ||
assert :no_header_found = ParseHeader.next_header('abc\n') | ||
end | ||
|
||
test "empty charlist" do | ||
assert :no_header_found = ParseHeader.next_header([]) | ||
end | ||
end | ||
end |