Debian Codegraph @

tests/inputs/string.gleam

raw
1 // https://github.com/gleam-lang/stdlib/raw/main/src/gleam/string.gleam
2 //// Strings in Gleam are UTF-8 binaries. They can be written in your code a
3 //// text surrounded by `"double quotes"`.
4
5 import gleam/string_builder
6 import gleam/dynamic.{Dynamic}
7 import gleam/iterator
8 import gleam/list
9 import gleam/order
10 import gleam/result
11
12 pub type String =
13 String
14
15 /// A UtfCodepoint is the integer representation of a valid UTF codepoint
16 pub type UtfCodepoint =
17 UtfCodepoint
18
19 /// Determine if a string is empty.
20 ///
21 /// ## Examples
22 ///
23 /// > is_empty("")
24 /// True
25 ///
26 /// > is_empty("the world")
27 /// False
28 ///
29 pub fn is_empty(str: String) -> Bool {
30 str == ""
31 }
32
33 /// Get the number of grapheme clusters in a given string.
34 ///
35 /// This function has to iterate across the whole string to count the number of
36 /// graphemes, so it runs in linear time.
37 ///
38 /// ## Examples
39 ///
40 /// > length("Gleam")
41 /// 5
42 ///
43 /// > length("ß↑e̊")
44 /// 3
45 ///
46 /// > length("")
47 /// 0
48 ///
49 pub external fn length(String) -> Int =
50 "string" "length"
51
52 ///
53 /// Reverse a string.
54 ///
55 /// This function has to iterate across the whole string so it runs in linear
56 /// time.
57 ///
58 /// ## Examples
59 ///
60 /// > reverse("stressed")
61 /// "desserts"
62 ///
63 pub fn reverse(string: String) -> String {
64 string
65 |> string_builder.from_string
66 |> string_builder.reverse
67 |> string_builder.to_string
68 }