aboutsummaryrefslogtreecommitdiff
path: root/src/iodata.gleam
blob: 81329d01c219a619ba96edcf0932c820832ce13c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import expect

// concat should work on List(Iodata)
// need a name for the string version

pub external type Iodata;

pub external fn prepend(Iodata, String) -> Iodata =
  "gleam__stdlib" "iodata_prepend";

pub external fn append(Iodata, String) -> Iodata =
  "gleam__stdlib" "iodata_append";

pub external fn prepend_iodata(Iodata, Iodata) -> Iodata =
  "gleam__stdlib" "iodata_prepend";

pub external fn append_iodata(Iodata, Iodata) -> Iodata =
  "gleam__stdlib" "iodata_append";

pub external fn from_strings(List(String)) -> Iodata =
  "gleam__stdlib" "identity";

pub external fn concat(List(Iodata)) -> Iodata =
  "gleam__stdlib" "identity";

pub external fn new(String) -> Iodata =
  "gleam__stdlib" "identity";

pub external fn to_string(Iodata) -> String =
  "erlang" "iolist_to_binary";

pub external fn byte_size(Iodata) -> Int =
  "erlang" "iolist_size";

pub external fn from_float(Float) -> Iodata =
  "io_lib_format" "fwrite_g";

test iodata {
  let iodata = new("ello")
    |> append(_, ",")
    |> append(_, " world!")
    |> prepend(_, "H")

  iodata
  |> to_string
  |> expect:equal(_, "Hello, world!")

  iodata
  |> byte_size
  |> expect:equal(_, 13)

  let iodata = new("ello")
    |> append_iodata(_, new(","))
    |> append_iodata(_, concat([new(" wo"), new("rld!")]))
    |> prepend_iodata(_, new("H"))

  iodata
  |> to_string
  |> expect:equal(_, "Hello, world!")

  iodata
  |> byte_size
  |> expect:equal(_, 13)
}

pub external fn lowercase(Iodata) -> Iodata = "string" "lowercase"

test lowercase {
  ["Gleam", "Gleam"]
  |> from_strings
  |> lowercase
  |> to_string
  |> expect:equal(_, "gleamgleam")
}

pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase"

test uppercase {
  ["Gleam", "Gleam"]
  |> from_strings
  |> 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"]
  |> from_strings
  |> split(_, ", ")
  |> expect:equal(_, [new("Gleam"), from_strings(["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(_, from_strings(["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

  from_strings([])
  |> is_empty
  |> expect:true

  from_strings(["", ""])
  |> is_empty
  |> expect:true
}