1
- /// Represents Erlang's `iodata` type and the associated functions
2
- /// to work with it.
3
-
1
+ /// Iodata is a type used for efficiently building strings.
2
+ ///
3
+ /// When we append one string to another the strings must be copied to a
4
+ /// new location in memory so that they can sit together. This behaviour
5
+ /// enables efficient reading of the string but copying can be expensive,
6
+ /// especially if we want to join many strings together.
7
+ ///
8
+ /// Iodata is different in that it can be joined together in constant time
9
+ /// using minimal memory, and then can be efficiently converted to a string
10
+ /// using the `to_string` function.
11
+ ///
4
12
pub external type Iodata ;
5
13
6
- /// Prepends the string `prefix` to `to`
14
+ /// Prepend a String onto the start of some Iodata.
15
+ ///
16
+ /// Runs in constant time.
7
17
///
8
18
pub external fn prepend(to: Iodata, prefix: String) -> Iodata =
9
19
"gleam_stdlib" "iodata_prepend";
10
20
11
- /// Appends the string `suffix` to `to`
21
+ /// Append a String onto the end of some Iodata.
22
+ ///
23
+ /// Runs in constant time.
12
24
///
13
25
pub external fn append(to: Iodata, suffix: String) -> Iodata =
14
26
"gleam_stdlib" "iodata_append";
15
27
16
- /// Prepends the Iodata `prefix` to `to`
28
+ /// Prepend some Iodata onto the start of another.
29
+ ///
30
+ /// Runs in constant time.
17
31
///
18
32
pub external fn prepend_iodata(to: Iodata, prefix: Iodata) -> Iodata =
19
33
"gleam_stdlib" "iodata_prepend";
20
34
21
- /// Prepends the Iodata `prefix` to `to`
35
+ /// Append some Iodata onto the end of another.
36
+ ///
37
+ /// Runs in constant time.
22
38
///
23
39
pub external fn append_iodata(to: Iodata, suffix: Iodata) -> Iodata =
24
40
"gleam_stdlib" "iodata_append";
25
41
26
- /// Builds an Iodata value from the supplied list of strings
42
+ /// Convert a list of strings into iodata.
43
+ ///
44
+ /// Runs in constant time.
27
45
///
28
46
pub external fn from_strings(List(String)) -> Iodata =
29
47
"gleam_stdlib" "identity";
30
48
31
- /// Builds a new Iodata out of a list of Iodata
49
+ /// Joins a list of iodata into a single iodata.
50
+ ///
51
+ /// Runs in constant time.
32
52
///
33
53
pub external fn concat(List(Iodata)) -> Iodata =
34
54
"gleam_stdlib" "identity";
35
55
36
- /// Turns a `String` into an `Iodata`
56
+ /// Convert a string into iodata.
57
+ ///
58
+ /// Runs in constant time.
37
59
///
38
60
pub external fn new(String) -> Iodata =
39
61
"gleam_stdlib" "identity";
40
62
41
63
/// Turns an `Iodata` into a `String`
42
64
///
65
+ /// This function is implemented natively by the virtual machine and is highly
66
+ /// optimised.
67
+ ///
43
68
pub external fn to_string(Iodata) -> String =
44
69
"erlang" "iolist_to_binary";
45
70
46
- /// Returns the size of the Iodata in bytes
71
+ /// Returns the size of the Iodata in bytes.
47
72
///
48
73
pub external fn byte_size(Iodata) -> Int =
49
74
"erlang" "iolist_size";
50
75
51
- /// Creates an `Iodata` value from a `Float` value .
76
+ /// Creates textual representation of the given float as iodata .
52
77
///
53
78
pub external fn from_float(Float) -> Iodata =
54
79
"io_lib_format" "fwrite_g";
55
80
56
81
/// Converts Iodata to a new Iodata where valid UTF-8 string data is
57
- /// lowercased
82
+ /// lowercased.
58
83
///
59
84
pub external fn lowercase(Iodata) -> Iodata = "string" "lowercase"
60
85
61
86
/// Converts Iodata to a new Iodata where valid UTF-8 string data is
62
- /// uppercased
87
+ /// uppercased.
63
88
///
64
89
pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase"
65
90
66
91
/// Converts Iodata to a new Iodata where valid UTF-8 string data is
67
- /// reversed
92
+ /// reversed.
68
93
///
69
94
pub external fn reverse(Iodata) -> Iodata = "string" "reverse"
70
95
@@ -75,7 +100,7 @@ type Direction {
75
100
external fn erl_split(Iodata, String, Direction) -> List(Iodata) =
76
101
"string" "split"
77
102
78
- /// Splits the supplied Iodata by the pattern `on`
103
+ /// Splits iodata on a given pattern into a list of iodata.
79
104
///
80
105
pub fn split(iodata: Iodata, on pattern: String) -> List(Iodata) {
81
106
erl_split(iodata, pattern, All)
@@ -84,7 +109,7 @@ pub fn split(iodata: Iodata, on pattern: String) -> List(Iodata) {
84
109
external fn erl_replace(Iodata, String, String, Direction) -> Iodata =
85
110
"string" "replace"
86
111
87
- /// Replaces all instances of `all` with the string `with`
112
+ /// Replaces all instances of a pattern with a given string substitute.
88
113
///
89
114
pub fn replace(
90
115
in iodata: Iodata,
@@ -94,10 +119,30 @@ pub fn replace(
94
119
erl_replace(iodata, pattern, substitute, All)
95
120
}
96
121
97
- /// Prepends the string `prefix` to `to`
122
+ /// Compare two pieces of iodata to determine if they have the same textual
123
+ /// content.
124
+ ///
125
+ /// Comparing two iodata using the `==` operator may return False even if they
126
+ /// have the same content as they may have been build in different ways, so
127
+ /// using this function is often preferred.
128
+ ///
129
+ /// ## Examples
130
+ ///
131
+ /// ```
132
+ /// from_strings(["a", "b"]) == new("ab") == False
133
+ /// is_equal(from_strings(["a", "b"]), new("ab")) == True
134
+ /// ```
98
135
///
99
136
pub external fn is_equal(Iodata, Iodata) -> Bool = "string" "equal"
100
137
101
- /// Prepends the string `prefix` to `to`
138
+ /// Inspect some iodata to determine if it is equivalent to an empty string.
139
+ ///
140
+ /// ## Examples
141
+ ///
142
+ /// ```
143
+ /// new("ok") |> is_empty == False
144
+ /// new("") |> is_empty == True
145
+ /// from_strings([]) |> is_empty == True
146
+ /// ```
102
147
///
103
148
pub external fn is_empty(Iodata) -> Bool = "string" "is_empty"
0 commit comments