aboutsummaryrefslogtreecommitdiff
path: root/src/str.gleam
blob: 6ee8235553a539ed0e194b992166bda1ea3029da (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
// Named str to avoid name collisions with the Erlang string module.
// Will rename later once we have namespaces for modules.

import expect
import iodata
import list

pub external fn length(String) -> Int = "string" "length"

pub enum ParseError =
  | ParseError

test length {
  length("ß↑e̊")
  |> expect:equal(_, 3)

  length("Gleam")
  |> expect:equal(_, 5)

  length("")
  |> expect:equal(_, 0)
}

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

test lowercase {
  lowercase("Gleam")
  |> expect:equal(_, "gleam")
}

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

test uppercase {
  uppercase("Gleam")
  |> expect:equal(_, "GLEAM")
}

pub fn reverse(string) {
  string
  |> iodata:new
  |> iodata:reverse
  |> iodata:to_string
}

test reverse {
  reverse("Gleam")
  |> expect:equal(_, "maelG")
}

pub fn split(string, on) {
  string
  |> iodata:new
  |> iodata:split(_, on)
  |> list:map(_, iodata:to_string)
}

test split {
  "Gleam,Erlang,Elixir"
  |> split(_, ",")
  |> expect:equal(_, ["Gleam", "Erlang", "Elixir"])

  "Gleam, Erlang,Elixir"
  |> split(_, ", ")
  |> expect:equal(_, ["Gleam", "Erlang,Elixir"])
}

pub fn replace(string, pattern, with) {
  string
  |> iodata:new
  |> iodata:replace(_, pattern, with)
  |> iodata:to_string
}

test replace {
  "Gleam,Erlang,Elixir"
  |> replace(_, ",", "++")
  |> expect:equal(_, "Gleam++Erlang++Elixir")
}

pub external fn from_int(Int) -> String = "erlang" "integer_to_binary"

test from_int {
  123
  |> from_int
  |> expect:equal(_, "123")

  -123
  |> from_int
  |> expect:equal(_, "-123")

  0123
  |> from_int
  |> expect:equal(_, "123")
}

pub external fn parse_int(String) -> Result(Int, ParseError) = "gleam__stdlib" "parse_int";

test parse_int {
  "123"
  |> parse_int
  |> expect:equal(_, Ok(123))

  "-123"
  |> parse_int
  |> expect:equal(_, Ok(-123))

  "0123"
  |> parse_int
  |> expect:equal(_, Ok(123))

  ""
  |> parse_int
  |> expect:equal(_, Error(ParseError))

  "what"
  |> parse_int
  |> expect:equal(_, Error(ParseError))

  "1.23"
  |> parse_int
  |> expect:equal(_, Error(ParseError))
}

pub external fn base_from_int(Int, Int) -> String = "erlang" "integer_to_binary"

test base_from_int {
  100
  |> base_from_int(_, 16)
  |> expect:equal(_, "64")

  -100
  |> base_from_int(_, 16)
  |> expect:equal(_, "-64")
}

pub fn from_float(f) {
  f
  |> iodata:from_float
  |> iodata:to_string
}

test from_float {
  123.0
  |> from_float
  |> expect:equal(_, "123.0")

  -8.1
  |> from_float
  |> expect:equal(_, "-8.1")
}