Skip to content

Commit

Permalink
Remove regex FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Dec 22, 2024
1 parent 05e515b commit cbec8f5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 101 deletions.
61 changes: 9 additions & 52 deletions src/gleam_stdlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
decode_float/1, decode_list/1, decode_option/2, decode_field/2, parse_int/1,
parse_float/1, less_than/2, string_pop_grapheme/1, string_pop_codeunit/1,
string_starts_with/2, wrap_list/1, string_ends_with/2, string_pad/4,
decode_map/1, uri_parse/1,
decode_result/1, bit_array_slice/3, decode_bit_array/1, compile_regex/2,
regex_scan/2, percent_encode/1, percent_decode/1, regex_check/2,
regex_split/2, base_decode64/1, parse_query/1, bit_array_concat/1,
bit_array_base64_encode/2, size_of_tuple/1, decode_tuple/1, decode_tuple2/1,
decode_tuple3/1, decode_tuple4/1, decode_tuple5/1, decode_tuple6/1,
tuple_get/2, classify_dynamic/1, print/1, println/1, print_error/1,
println_error/1, inspect/1, float_to_string/1, int_from_base_string/2,
decode_map/1, uri_parse/1, decode_result/1, bit_array_slice/3,
decode_bit_array/1, percent_encode/1, percent_decode/1, base_decode64/1,
parse_query/1, bit_array_concat/1, bit_array_base64_encode/2,
size_of_tuple/1, decode_tuple/1, decode_tuple2/1, decode_tuple3/1,
decode_tuple4/1, decode_tuple5/1, decode_tuple6/1, tuple_get/2,
classify_dynamic/1, print/1, println/1, print_error/1, println_error/1,
inspect/1, float_to_string/1, int_from_base_string/2,
utf_codepoint_list_to_string/1, contains_string/2, crop_string/2,
base16_encode/1, base16_decode/1, string_replace/3, regex_replace/3,
slice/3, bit_array_to_int_and_size/1, bit_array_pad_to_bytes/1
base16_encode/1, base16_decode/1, string_replace/3, slice/3,
bit_array_to_int_and_size/1, bit_array_pad_to_bytes/1
]).

%% Taken from OTP's uri_string module
Expand Down Expand Up @@ -232,48 +231,6 @@ bit_array_slice(Bin, Pos, Len) ->
catch error:badarg -> {error, nil}
end.

compile_regex(String, Options) ->
{options, Caseless, Multiline} = Options,
OptionsList = [
unicode,
ucp,
Caseless andalso caseless,
Multiline andalso multiline
],
FilteredOptions = [Option || Option <- OptionsList, Option /= false],
case re:compile(String, FilteredOptions) of
{ok, MP} -> {ok, MP};
{error, {Str, Pos}} ->
{error, {compile_error, unicode:characters_to_binary(Str), Pos}}
end.

regex_check(Regex, String) ->
re:run(String, Regex) /= nomatch.

regex_split(Regex, String) ->
re:split(String, Regex).

regex_submatches(_, {-1, 0}) -> none;
regex_submatches(String, {Start, Length}) ->
BinarySlice = binary:part(String, {Start, Length}),
case string:is_empty(binary_to_list(BinarySlice)) of
true -> none;
false -> {some, BinarySlice}
end.

regex_matches(String, [{Start, Length} | Submatches]) ->
Submatches1 = lists:map(fun(X) -> regex_submatches(String, X) end, Submatches),
{match, binary:part(String, Start, Length), Submatches1}.

regex_scan(Regex, String) ->
case re:run(String, Regex, [global]) of
{match, Captured} -> lists:map(fun(X) -> regex_matches(String, X) end, Captured);
nomatch -> []
end.

regex_replace(Regex, Subject, Replacement) ->
re:replace(Subject, Regex, Replacement, [global, {return, binary}]).

base_decode64(S) ->
try {ok, base64:decode(S)}
catch error:_ -> {error, nil}
Expand Down
49 changes: 0 additions & 49 deletions src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import {
NonEmpty,
CustomType,
} from "./gleam.mjs";
import {
CompileError as RegexCompileError,
Match as RegexMatch,
} from "./gleam/regex.mjs";
import { DecodeError } from "./gleam/dynamic.mjs";
import { Some, None } from "./gleam/option.mjs";
import { Eq, Gt, Lt } from "./gleam/order.mjs";
Expand Down Expand Up @@ -448,51 +444,6 @@ export function utf_codepoint_to_int(utf_codepoint) {
return utf_codepoint.value;
}

export function regex_check(regex, string) {
regex.lastIndex = 0;
return regex.test(string);
}

export function compile_regex(pattern, options) {
try {
let flags = "gu";
if (options.case_insensitive) flags += "i";
if (options.multi_line) flags += "m";
return new Ok(new RegExp(pattern, flags));
} catch (error) {
const number = (error.columnNumber || 0) | 0;
return new Error(new RegexCompileError(error.message, number));
}
}

export function regex_split(regex, string) {
return List.fromArray(
string.split(regex).map((item) => (item === undefined ? "" : item)),
);
}

export function regex_scan(regex, string) {
const matches = Array.from(string.matchAll(regex)).map((match) => {
const content = match[0];
const submatches = [];
for (let n = match.length - 1; n > 0; n--) {
if (match[n]) {
submatches[n - 1] = new Some(match[n]);
continue;
}
if (submatches.length > 0) {
submatches[n - 1] = new None();
}
}
return new RegexMatch(content, List.fromArray(submatches));
});
return List.fromArray(matches);
}

export function regex_replace(regex, original_string, replacement) {
return original_string.replaceAll(regex, replacement);
}

export function new_map() {
return Dict.new();
}
Expand Down

0 comments on commit cbec8f5

Please sign in to comment.