aboutsummaryrefslogtreecommitdiff
path: root/gen/src/gleam@string.erl
blob: 86b1f790db5f256c21febe4727e176984bf4a7c6 (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
-module(gleam@string).
-compile(no_auto_import).

-export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, slice/3, contains/2, starts_with/2, ends_with/2, split/2, append/2, concat/1, repeat/2, join/2, pad_left/3, pad_right/3, trim/1, trim_left/1, trim_right/1]).

is_empty(Str) ->
    Str =:= <<""/utf8>>.

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

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

replace(String, Pattern, Substitute) ->
    gleam@iodata:to_string(
        gleam@iodata:replace(gleam@iodata:new(String), Pattern, Substitute)
    ).

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

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

compare(A, B) ->
    gleam_stdlib:compare_strings(A, B).

erl_slice(A, B, C) ->
    string:slice(A, B, C).

slice(String, Idx, Len) ->
    case Idx < 0 of
        true ->
            TranslatedIdx = length(String) + Idx,
            case TranslatedIdx < 0 of
                true ->
                    <<""/utf8>>;

                false ->
                    erl_slice(String, TranslatedIdx, Len)
            end;

        false ->
            erl_slice(String, Idx, Len)
    end.

erl_contains(A, B) ->
    gleam_stdlib:string_contains(A, B).

contains(Haystack, Needle) ->
    erl_contains(Haystack, Needle).

starts_with(A, B) ->
    gleam_stdlib:string_starts_with(A, B).

ends_with(A, B) ->
    gleam_stdlib:string_ends_with(A, B).

split(X, Substring) ->
    gleam@list:map(
        gleam@iodata:split(gleam@iodata:new(X), Substring),
        fun gleam@iodata:to_string/1
    ).

append(First, Second) ->
    gleam@iodata:to_string(
        gleam@iodata:append(gleam@iodata:new(First), Second)
    ).

concat(Strings) ->
    gleam@iodata:to_string(gleam@iodata:from_strings(Strings)).

repeat_help(Chunk, Result, Repeats) ->
    case Repeats =< 0 of
        true ->
            concat(Result);

        false ->
            repeat_help(Chunk, [Chunk | Result], Repeats - 1)
    end.

repeat(String, Times) ->
    repeat_help(String, [], Times).

join(Strings, Separator) ->
    gleam@iodata:to_string(
        gleam@iodata:from_strings(gleam@list:intersperse(Strings, Separator))
    ).

erl_pad(A, B, C, D) ->
    gleam_stdlib:string_pad(A, B, C, D).

pad_left(String, Length, PadString) ->
    erl_pad(String, Length, leading, PadString).

pad_right(String, Length, PadString) ->
    erl_pad(String, Length, trailing, PadString).

erl_trim(A, B) ->
    string:trim(A, B).

trim(String) ->
    erl_trim(String, both).

trim_left(String) ->
    erl_trim(String, leading).

trim_right(String) ->
    erl_trim(String, trailing).