aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2020-05-19 19:49:22 +0100
committerLouis Pilfold <louis@lpil.uk>2020-05-19 19:49:22 +0100
commitecceb4e7f332749753391d728c121ef1792e9191 (patch)
tree41f07dc0d56a09f10ddf0c8eb34f80f75a6aefc1 /src
parent1ee8dbf4c7e5abf831e3118f8953b24c4043e70f (diff)
downloadgleam_stdlib-ecceb4e7f332749753391d728c121ef1792e9191.tar.gz
gleam_stdlib-ecceb4e7f332749753391d728c121ef1792e9191.zip
Remove some Erlang glue
Diffstat (limited to 'src')
-rw-r--r--src/gleam/string.gleam10
-rw-r--r--src/gleam_stdlib.erl36
2 files changed, 17 insertions, 29 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index c897a69..fc58329 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -2,6 +2,7 @@
//// text surrounded by `"double quotes"`.
import gleam/iodata
+import gleam/dynamic.{Dynamic}
import gleam/list
import gleam/order
import gleam/result.{Option}
@@ -179,8 +180,8 @@ pub fn slice(
//
//
// pub fn drop_right(from string: String, up_to num_graphemes: Int) -> String {}
-external fn erl_contains(String, String) -> Bool =
- "gleam_stdlib" "string_contains"
+external fn erl_contains(String, String) -> Dynamic =
+ "string" "find"
/// Check if the first string contains the second.
///
@@ -196,7 +197,10 @@ external fn erl_contains(String, String) -> Bool =
/// False
///
pub fn contains(does haystack: String, contain needle: String) -> Bool {
- erl_contains(haystack, needle)
+ haystack
+ |> erl_contains(needle)
+ |> dynamic.atom
+ |> result.is_error
}
/// See if the first string starts with the second one.
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index e78d60e..0efedec 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -8,8 +8,8 @@
decode_int/1, decode_string/1, decode_bool/1, decode_float/1,
decode_thunk/1, decode_atom/1, decode_list/1, decode_field/2,
decode_element/2, parse_int/1, parse_float/1, compare_strings/2,
- string_contains/2, string_starts_with/2, string_ends_with/2,
- string_pad/4, decode_tuple2/1, decode_map/1]).
+ string_starts_with/2, string_ends_with/2, string_pad/4,
+ decode_tuple2/1, decode_map/1]).
should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
@@ -92,31 +92,23 @@ decode_field(Data, Key) ->
decode_element(Data, Position) when is_tuple(Data) ->
case catch element(Position + 1, Data) of
{'EXIT', _Reason} ->
- Reason = io_lib:format("Expected a tuple of at least ~w size, got a tuple of ~w sise",
- [Position + 1, tuple_size(Data)]),
- {error, Reason};
+ decode_error_msg(["a tuple of at least ", integer_to_list(Position + 1), " size"], Data);
Value ->
{ok, Value}
end;
-decode_element(Data, _Position) -> decode_error_msg("a Tuple", Data).
+decode_element(Data, _Position) -> decode_error_msg("a tuple", Data).
parse_int(String) ->
- case string:to_integer(binary:bin_to_list(String)) of
- {Integer, []} ->
- {ok, Integer};
-
- _ ->
- {error, nil}
+ case catch binary_to_integer(String) of
+ Int when is_integer(Int) -> {ok, Int};
+ _ -> {error, nil}
end.
parse_float(String) ->
- case string:to_float(binary:bin_to_list(String)) of
- {Float, []} ->
- {ok, Float};
-
- _ ->
- {error, nil}
+ case catch binary_to_float(String) of
+ Float when is_float(Float) -> {ok, Float};
+ _ -> {error, nil}
end.
compare_strings(Lhs, Rhs) ->
@@ -129,14 +121,6 @@ compare_strings(Lhs, Rhs) ->
gt
end.
-string_contains(Haystack, Needle) ->
- case string:find(Haystack, Needle) of
- nomatch ->
- false;
- _ ->
- true
- end.
-
string_starts_with(_, <<>>) -> true;
string_starts_with(String, Prefix) when byte_size(Prefix) > byte_size(String) -> false;
string_starts_with(String, Prefix) ->