Skip to content

Commit 6f635df

Browse files
Ryan Mivillelpil
Ryan Miville
authored andcommitted
refactor ffi and rename to
1 parent 9f4d4de commit 6f635df

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

src/gleam/regexp.gleam

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ pub fn replace(
226226
///
227227
/// ```gleam
228228
/// let assert Ok(re) = regexp.from_string("\\w+")
229-
/// regexp.replace_map(re, "hello, joe!", fn(m) { string.capitalise(m.content) })
229+
/// regexp.match_map(re, "hello, joe!", fn(m) { string.capitalise(m.content) })
230230
/// // -> "Hello, Joe!"
231231
/// ```
232-
@external(erlang, "gleam_regexp_ffi", "replace_map")
233-
@external(javascript, "../gleam_regexp_ffi.mjs", "replace_map")
234-
pub fn replace_map(
232+
@external(erlang, "gleam_regexp_ffi", "match_map")
233+
@external(javascript, "../gleam_regexp_ffi.mjs", "match_map")
234+
pub fn match_map(
235235
each pattern: Regexp,
236236
in string: String,
237237
with substitute: fn(Match) -> String,

src/gleam_regexp_ffi.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-module(gleam_regexp_ffi).
22

3-
-export([compile/2, check/2, split/2, replace/3, scan/2, replace_map/3]).
3+
-export([compile/2, check/2, split/2, replace/3, scan/2, match_map/3]).
44

55
compile(String, Options) ->
66
{options, Caseless, Multiline} = Options,
@@ -44,9 +44,9 @@ scan(Regexp, String) ->
4444
replace(Regexp, Subject, Replacement) ->
4545
re:replace(Subject, Regexp, Replacement, [global, {return, binary}]).
4646

47-
replace_map(Regexp, Subject, Replacement) ->
47+
match_map(Regexp, Subject, Replacement) ->
4848
Replacement1 = fun(Content, Submatches) ->
49-
Submatches1 = gleam@list:map(Submatches, fun gleam@string:to_option/1),
49+
Submatches1 = lists:map(fun gleam@string:to_option/1, Submatches),
5050
Replacement({match, Content, Submatches1})
5151
end,
5252
re:replace(Subject, Regexp, Replacement1, [global, {return, binary}]).

src/gleam_regexp_ffi.mjs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,7 @@ export function split(regex, string) {
3131
export function scan(regex, string) {
3232
const matches = Array.from(string.matchAll(regex)).map((match) => {
3333
const content = match[0];
34-
const submatches = [];
35-
for (let n = match.length - 1; n > 0; n--) {
36-
if (match[n]) {
37-
submatches[n - 1] = new Some(match[n]);
38-
continue;
39-
}
40-
if (submatches.length > 0) {
41-
submatches[n - 1] = new None();
42-
}
43-
}
44-
return new RegexMatch(content, List.fromArray(submatches));
34+
return new RegexMatch(content, submatches(match.slice(1)));
4535
});
4636
return List.fromArray(matches);
4737
}
@@ -50,22 +40,26 @@ export function replace(regex, original_string, replacement) {
5040
return original_string.replaceAll(regex, replacement);
5141
}
5242

53-
export function replace_map(regex, original_string, replacement) {
43+
export function match_map(regex, original_string, replacement) {
5444
let replace = (match, ...args) => {
5545
const hasNamedGroups = typeof args.at(-1) === "object";
5646
const groups = args.slice(0, hasNamedGroups ? -3 : -2);
57-
const submatches = [];
58-
for (let n = 0; n < groups.length; n++) {
59-
if (groups[n]) {
60-
submatches[n] = new Some(groups[n]);
61-
continue;
62-
}
63-
if (submatches.length > 0) {
64-
submatches[n] = new None();
65-
}
66-
}
67-
let regexMatch = new RegexMatch(match, List.fromArray(submatches));
47+
let regexMatch = new RegexMatch(match, submatches(groups));
6848
return replacement(regexMatch);
6949
};
7050
return original_string.replaceAll(regex, replace);
7151
}
52+
53+
function submatches(groups) {
54+
const submatches = [];
55+
for (let n = groups.length - 1; n >= 0; n--) {
56+
if (groups[n]) {
57+
submatches[n] = new Some(groups[n]);
58+
continue;
59+
}
60+
if (submatches.length > 0) {
61+
submatches[n] = new None();
62+
}
63+
}
64+
return List.fromArray(submatches);
65+
}

test/gleam_regexp_test.gleam

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub fn replace_3_test() {
191191
|> should.equal("🐕🐕 are great!")
192192
}
193193

194-
pub fn replace_map_0_test() {
194+
pub fn match_map_0_test() {
195195
let replace = fn(match: Match) {
196196
case match.content {
197197
"1" -> "one"
@@ -201,11 +201,11 @@ pub fn replace_map_0_test() {
201201
}
202202
}
203203
let assert Ok(re) = regexp.from_string("1|2|3")
204-
regexp.replace_map(re, "1, 2, 3, 4", replace)
204+
regexp.match_map(re, "1, 2, 3, 4", replace)
205205
|> should.equal("one, two, three, 4")
206206
}
207207

208-
pub fn replace_map_1_test() {
208+
pub fn match_map_1_test() {
209209
let replace = fn(match: Match) {
210210
case match.submatches {
211211
[Some("1")] -> "one"
@@ -215,6 +215,6 @@ pub fn replace_map_1_test() {
215215
}
216216
}
217217
let assert Ok(re) = regexp.from_string("'(1|2|3)'")
218-
regexp.replace_map(re, "'1', '2', '3', '4'", replace)
218+
regexp.match_map(re, "'1', '2', '3', '4'", replace)
219219
|> should.equal("one, two, three, '4'")
220220
}

0 commit comments

Comments
 (0)