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.
Deprecate RawParseUtils.parse_base_10/1 in favor of ParseDecimal.from…
…_decimal_charlist/1. (#195)
- Loading branch information
Showing
5 changed files
with
64 additions
and
7 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
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,39 @@ | ||
defmodule Xgit.Util.ParseDecimal do | ||
@moduledoc false | ||
# Internal utility for parsing decimal values from charlist. | ||
|
||
import Xgit.Util.ForceCoverage | ||
|
||
@doc ~S""" | ||
Parse a base-10 numeric value from a charlist of ASCII digits into a number. | ||
Similar to `Integer.parse/2` but uses charlist instead. | ||
Digit sequences can begin with an optional run of spaces before the | ||
sequence, and may start with a `+` or a `-` to indicate sign position. | ||
Any other characters will cause the method to stop and return the current | ||
result to the caller. | ||
Returns `{number, new_buffer}` where `number` is the integer that was | ||
found (or 0 if no number found there) and `new_buffer` is the charlist | ||
following the number that was parsed. | ||
""" | ||
@spec from_decimal_charlist(b :: charlist) :: {integer, charlist} | ||
def from_decimal_charlist(b) when is_list(b) do | ||
b = skip_white_space(b) | ||
{sign, b} = parse_sign(b) | ||
{n, b} = parse_digits(0, b) | ||
|
||
cover {sign * n, b} | ||
end | ||
|
||
defp skip_white_space([?\s | b]), do: skip_white_space(b) | ||
defp skip_white_space(b), do: b | ||
|
||
defp parse_sign([?- | b]), do: cover({-1, b}) | ||
defp parse_sign([?+ | b]), do: cover({1, b}) | ||
defp parse_sign(b), do: cover({1, b}) | ||
|
||
defp parse_digits(n, [d | b]) when d >= ?0 and d <= ?9, do: parse_digits(n * 10 + (d - ?0), b) | ||
defp parse_digits(n, b), do: cover({n, b}) | ||
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,14 @@ | ||
defmodule Xgit.Util.ParseDecimalTest do | ||
use ExUnit.Case, async: true | ||
|
||
import Xgit.Util.ParseDecimal | ||
|
||
test "from_decimal_charlist/1" do | ||
assert from_decimal_charlist('abc') == {0, 'abc'} | ||
assert from_decimal_charlist('0abc') == {0, 'abc'} | ||
assert from_decimal_charlist('99') == {99, ''} | ||
assert from_decimal_charlist('+99x') == {99, 'x'} | ||
assert from_decimal_charlist(' -42 ') == {-42, ' '} | ||
assert from_decimal_charlist(' xyz') == {0, 'xyz'} | ||
end | ||
end |