aboutsummaryrefslogtreecommitdiff
path: root/gen/str.erl
blob: 962580764a2d96b07c4afc2ef78201e01d33891e (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
-module(str).
-compile(no_auto_import).
-include_lib("eunit/include/eunit.hrl").

-export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3]).

length(A) ->
    string:length(A).

-ifdef(TEST).
length_test() ->
    expect:equal(length(<<"ß↑e̊">>), 3),
    expect:equal(length(<<"Gleam">>), 5),
    expect:equal(length(<<"">>), 0).
-endif.

lowercase(A) ->
    string:lowercase(A).

-ifdef(TEST).
lowercase_test() ->
    expect:equal(lowercase(<<"Gleam">>), <<"gleam">>).
-endif.

uppercase(A) ->
    string:uppercase(A).

-ifdef(TEST).
uppercase_test() ->
    expect:equal(uppercase(<<"Gleam">>), <<"GLEAM">>).
-endif.

reverse(String) ->
    iodata:to_string(iodata:reverse(iodata:new(String))).

-ifdef(TEST).
reverse_test() ->
    expect:equal(reverse(<<"Gleam">>), <<"maelG">>).
-endif.

split(String, On) ->
    list:map(iodata:split(iodata:new(String), On), fun iodata:to_string/1).

-ifdef(TEST).
split_test() ->
    expect:equal(split(<<"Gleam,Erlang,Elixir">>, <<",">>),
                 [<<"Gleam">>, <<"Erlang">>, <<"Elixir">>]),
    expect:equal(split(<<"Gleam, Erlang,Elixir">>, <<", ">>),
                 [<<"Gleam">>, <<"Erlang,Elixir">>]).
-endif.

replace(String, Pattern, With) ->
    iodata:to_string(iodata:replace(iodata:new(String), Pattern, With)).

-ifdef(TEST).
replace_test() ->
    expect:equal(replace(<<"Gleam,Erlang,Elixir">>, <<",">>, <<"++">>),
                 <<"Gleam++Erlang++Elixir">>).
-endif.