aboutsummaryrefslogtreecommitdiff
path: root/src/iodata.gleam
diff options
context:
space:
mode:
Diffstat (limited to 'src/iodata.gleam')
-rw-r--r--src/iodata.gleam94
1 files changed, 92 insertions, 2 deletions
diff --git a/src/iodata.gleam b/src/iodata.gleam
index 2d14ed0..cc54965 100644
--- a/src/iodata.gleam
+++ b/src/iodata.gleam
@@ -8,7 +8,10 @@ pub external fn prepend(Iodata, String) -> Iodata =
pub external fn append(Iodata, String) -> Iodata =
"gleam__stdlib" "iodata_append";
-pub external fn from(List(String)) -> Iodata =
+pub external fn concat(List(String)) -> Iodata =
+ "gleam__stdlib" "identity";
+
+pub external fn new(String) -> Iodata =
"gleam__stdlib" "identity";
pub external fn to_string(Iodata) -> String =
@@ -18,7 +21,7 @@ pub external fn byte_size(Iodata) -> Int =
"erlang" "iolist_size";
test iodata {
- let iodata = from(["ello"])
+ let iodata = new("ello")
|> append(_, ",")
|> append(_, " world!")
|> prepend(_, "H")
@@ -31,3 +34,90 @@ test iodata {
|> byte_size
|> expect:equal(_, 13)
}
+
+pub external fn lowercase(Iodata) -> Iodata = "string" "lowercase"
+
+test lowercase {
+ ["Gleam", "Gleam"]
+ |> concat
+ |> lowercase
+ |> to_string
+ |> expect:equal(_, "gleamgleam")
+}
+
+pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase"
+
+test uppercase {
+ ["Gleam", "Gleam"]
+ |> concat
+ |> uppercase
+ |> to_string
+ |> expect:equal(_, "GLEAMGLEAM")
+}
+
+pub external fn reverse(Iodata) -> Iodata = "string" "reverse"
+
+enum Direction =
+ | All
+
+external fn erl_split(Iodata, String, Direction) -> List(Iodata) =
+ "string" "split"
+
+pub fn split(iodata, on) {
+ erl_split(iodata, on, All)
+}
+
+test split {
+ "Gleam,Erlang,Elixir"
+ |> new
+ |> split(_, ",")
+ |> expect:equal(_, [new("Gleam"), new("Erlang"), new("Elixir")])
+
+ ["Gleam, Erl", "ang,Elixir"]
+ |> concat
+ |> split(_, ", ")
+ |> expect:equal(_, [new("Gleam"), concat(["Erl", "ang,Elixir"])])
+}
+
+external fn erl_replace(Iodata, String, String, Direction) -> Iodata =
+ "string" "replace"
+
+pub fn replace(iodata, pattern, replacement) {
+ erl_replace(iodata, pattern, replacement, All)
+}
+
+pub external fn is_equal(Iodata, Iodata) -> Bool = "string" "equal"
+
+test is_equal {
+ new("12")
+ |> is_equal(_, concat(["1", "2"]))
+ |> expect:true
+
+ new("12")
+ |> is_equal(_, new("12"))
+ |> expect:true
+
+ new("12")
+ |> is_equal(_, new("2"))
+ |> expect:false
+}
+
+pub external fn is_empty(Iodata) -> Bool = "string" "is_empty"
+
+test is_empty {
+ new("")
+ |> is_empty
+ |> expect:true
+
+ new("12")
+ |> is_empty
+ |> expect:false
+
+ concat([])
+ |> is_empty
+ |> expect:true
+
+ concat(["", ""])
+ |> is_empty
+ |> expect:true
+}