10
10
% % Licensed under the Apache License, Version 2.0 (the "License");
11
11
% % you may not use this file except in compliance with the License.
12
12
% % You may obtain a copy of the License at
13
- % %
13
+ % %
14
14
% % http://www.apache.org/licenses/LICENSE-2.0
15
- % %
15
+ % %
16
16
% % Unless required by applicable law or agreed to in writing, software
17
17
% % distributed under the License is distributed on an "AS IS" BASIS,
18
18
% % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
22
-module (elli_bstr ).
23
23
24
24
-export ([
25
- to_lower /1 ,
26
- is_equal_ci /2 ,
27
- lchr /1 ,
25
+ to_lower /1 ,
26
+ is_equal_ci /2 ,
27
+ lchr /1 ,
28
+
29
+ trim_left /1 ,
30
+ trim_right /1 ,
31
+ trim /1
32
+ ]).
28
33
29
- trim_left /1 ,
30
- trim_right /1 ,
31
- trim /1
32
- ]).
33
34
34
35
-define (IS_WS (C ), (C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n )).
35
36
37
+
36
38
% %
37
39
% % Types
38
40
% %
44
46
% % Functions
45
47
% %
46
48
47
- % @doc Convert ascii Bin to lowercase
49
+ % % @doc Convert ascii Bin to lowercase
48
50
-spec to_lower (Bin :: binary ()) -> binary ().
49
51
to_lower (Bin ) ->
50
52
<< <<(lchr (C ))>> || <<C >> <= Bin >>.
51
53
52
54
53
- % @doc Compare two binary values, return true iff they are equal by a caseless compare.
55
+ % % @doc Compare two binary values.
56
+ % % Return true iff they are equal by a caseless compare.
54
57
-spec is_equal_ci (binary (), binary ()) -> boolean ().
55
58
is_equal_ci (Bin , Bin ) ->
56
- % Quick match with an Erlang pattern match
59
+ % % Quick match with an Erlang pattern match
57
60
true ;
58
61
is_equal_ci (Bin1 , Bin2 ) when is_binary (Bin1 ) andalso is_binary (Bin2 )
59
- andalso size (Bin1 ) =:= size (Bin2 ) ->
60
- % Both binaries are the same length, do a good check
62
+ andalso size (Bin1 ) =:= size (Bin2 ) ->
63
+ % % Both binaries are the same length, do a good check
61
64
equal_ci (Bin1 , Bin2 );
62
65
is_equal_ci (_ , _ ) ->
63
66
false .
64
67
65
68
66
- % @doc convert character to lowercase.
69
+ % % @doc convert character to lowercase.
67
70
-spec lchr (ascii_char ()) -> ascii_char ().
68
71
lchr ($A ) -> $a ;
69
72
lchr ($B ) -> $b ;
@@ -93,26 +96,30 @@ lchr($Y) -> $y;
93
96
lchr ($Z ) -> $z ;
94
97
lchr (Chr ) -> Chr .
95
98
96
- % @doc Remove leading whitespace from Bin
97
- trim_left (<<C , Rest /binary >>) when ? IS_WS (C ) ->
99
+
100
+ % % @doc Remove leading whitespace from Bin
101
+ trim_left (<<C , Rest /binary >>) when ? IS_WS (C ) ->
98
102
trim_left (Rest );
99
103
trim_left (Bin ) ->
100
104
Bin .
101
105
102
- % @doc Remove trailing whitespace from Bin
106
+
107
+ % % @doc Remove trailing whitespace from Bin
103
108
trim_right (<<>>) -> <<>>;
104
109
trim_right (Bin ) ->
105
110
case binary :last (Bin ) of
106
111
C when ? IS_WS (C ) ->
107
112
trim_right (binary :part (Bin , {0 , size (Bin )- 1 }));
108
- _ ->
113
+ _ ->
109
114
Bin
110
115
end .
111
116
112
- % @doc Remove leading and trailing whitespace.
117
+
118
+ % % @doc Remove leading and trailing whitespace.
113
119
trim (Bin ) ->
114
120
trim_left (trim_right (Bin )).
115
121
122
+
116
123
% %
117
124
% % Helpers
118
125
% %
@@ -123,8 +130,8 @@ equal_ci(<<C, Rest1/binary>>, <<C, Rest2/binary>>) ->
123
130
equal_ci (Rest1 , Rest2 );
124
131
equal_ci (<<C1 , Rest1 /binary >>, <<C2 , Rest2 /binary >>) ->
125
132
case lchr (C1 ) =:= lchr (C2 ) of
126
- true ->
127
- equal_ci (Rest1 , Rest2 );
128
- false ->
129
- false
133
+ true ->
134
+ equal_ci (Rest1 , Rest2 );
135
+ false ->
136
+ false
130
137
end .
0 commit comments