aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Terpstra <erterpstra@gmail.com>2020-06-25 11:20:25 +0200
committerLouis Pilfold <louis@lpil.uk>2020-06-25 14:31:57 +0100
commit8de363f9d7ddd3df55df73b27a3a87cf7c9bd707 (patch)
treead2f3fbe34697ffe542573b552038bff2bae5bca
parent641c5050ed226ef9fc1dbbe35df579dcd2e504d3 (diff)
downloadgleam_stdlib-8de363f9d7ddd3df55df73b27a3a87cf7c9bd707.tar.gz
gleam_stdlib-8de363f9d7ddd3df55df73b27a3a87cf7c9bd707.zip
Remove Match.number
-rw-r--r--src/gleam/regex.gleam10
-rw-r--r--src/gleam_stdlib.erl10
-rw-r--r--test/gleam/regex_test.gleam20
3 files changed, 9 insertions, 31 deletions
diff --git a/src/gleam/regex.gleam b/src/gleam/regex.gleam
index dc2a3f8..6534095 100644
--- a/src/gleam/regex.gleam
+++ b/src/gleam/regex.gleam
@@ -11,16 +11,10 @@ pub external type Regex
///
/// - match — the full string of the match.
/// - index — the index of the match in the original string.
-/// - number — each match is numbered, starting from 1
/// - submatches — a Regex can have subpatterns, sup-parts that are in parentheses.
///
pub type Match {
- Match(
- match: String,
- index: Int,
- number: Int,
- submatches: List(Option(String)),
- )
+ Match(match: String, index: Int, submatches: List(Option(String)))
}
/// When a regular expression fails to compile:
@@ -113,13 +107,11 @@ pub external fn split(Regex, String) -> List(String) =
/// Match(
/// match: "on a boat",
/// index: 5,
-/// number: 1,
/// submatches: [Some("boat")]
/// ),
/// Match(
/// match: "in a lake",
/// index: 15,
-/// number: 2,
/// submatches: [Some("lake")]
/// )
/// ]
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 217d4d1..85855d1 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -200,17 +200,13 @@ regex_submatches(String, {S, L}) ->
false -> {some, SubMatch}
end.
-regex_matches(String, [{S, L} | Submatches], Number) ->
- {match, binary:part(String, S, L), S, Number,
+regex_matches(String, [{S, L} | Submatches]) ->
+ {match, binary:part(String, S, L), S,
lists:map(fun(X) -> regex_submatches(String, X) end, Submatches)}.
-regex_captured(_, [], _) -> [];
-regex_captured(String, [ H | T ], Number) ->
- [ regex_matches(String, H, Number) | regex_captured(String, T, Number + 1) ].
-
regex_scan(Regex, String) ->
case re:run(String, Regex, [global]) of
- {match, Captured} -> regex_captured(String, Captured, 1);
+ {match, Captured} -> lists:map(fun(X) -> regex_matches(String, X) end, Captured);
_ -> []
end.
diff --git a/test/gleam/regex_test.gleam b/test/gleam/regex_test.gleam
index fec4bd5..8f69fee 100644
--- a/test/gleam/regex_test.gleam
+++ b/test/gleam/regex_test.gleam
@@ -57,31 +57,21 @@ pub fn scan_test() {
assert Ok(re) = regex.from_string("Gl\\w+")
regex.scan(re, "!Gleam")
- |> should.equal([Match(match: "Gleam", index: 1, number: 1, submatches: [])])
+ |> should.equal([Match(match: "Gleam", index: 1, submatches: [])])
regex.scan(re, "हGleam")
- |> should.equal([Match(match: "Gleam", index: 3, number: 1, submatches: [])])
+ |> should.equal([Match(match: "Gleam", index: 3, submatches: [])])
regex.scan(re, "𐍈Gleam")
- |> should.equal([Match(match: "Gleam", index: 4, number: 1, submatches: [])])
+ |> should.equal([Match(match: "Gleam", index: 4, submatches: [])])
assert Ok(re) = regex.from_string("[oi]n a(.?) (\\w+)")
regex.scan(re, "I am on a boat in a lake.")
|> should.equal(
[
- Match(
- match: "on a boat",
- index: 5,
- number: 1,
- submatches: [None, Some("boat")],
- ),
- Match(
- match: "in a lake",
- index: 15,
- number: 2,
- submatches: [None, Some("lake")],
- ),
+ Match(match: "on a boat", index: 5, submatches: [None, Some("boat")]),
+ Match(match: "in a lake", index: 15, submatches: [None, Some("lake")]),
],
)
}