Skip to content

Commit 76f9dc5

Browse files
committed
Avoid duplicate toCode calls in multiple Char.is... functions
This reduces the number of calls to `toCode char`. For instance, in the isAlphaNum function, the char argument would be converted to a char up to 3 times (once in isLower, isUpper and isDigit). This makes it so that the conversion is done at most once, improving performance of the functions.
1 parent 65cea00 commit 76f9dc5

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/Char.elm

+28-17
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ type Char = Char -- NOTE: The compiler provides the real implementation.
8080
-}
8181
isUpper : Char -> Bool
8282
isUpper char =
83-
let
84-
code =
85-
toCode char
86-
in
87-
code <= 0x5A && 0x41 <= code
83+
isUpperCode (toCode char)
84+
85+
86+
isUpperCode : Int -> Bool
87+
isUpperCode code =
88+
code <= 0x5A && 0x41 <= code
8889

8990

9091
{-| Detect lower case ASCII characters.
@@ -101,11 +102,12 @@ isUpper char =
101102
-}
102103
isLower : Char -> Bool
103104
isLower char =
104-
let
105-
code =
106-
toCode char
107-
in
108-
0x61 <= code && code <= 0x7A
105+
isLowerCode (toCode char)
106+
107+
108+
isLowerCode : Int -> Bool
109+
isLowerCode code =
110+
0x61 <= code && code <= 0x7A
109111

110112

111113
{-| Detect upper case and lower case ASCII characters.
@@ -121,7 +123,11 @@ isLower char =
121123
-}
122124
isAlpha : Char -> Bool
123125
isAlpha char =
124-
isLower char || isUpper char
126+
let
127+
code =
128+
toCode char
129+
in
130+
isLowerCode code || isUpperCode code
125131

126132

127133
{-| Detect upper case and lower case ASCII characters.
@@ -138,7 +144,11 @@ isAlpha char =
138144
-}
139145
isAlphaNum : Char -> Bool
140146
isAlphaNum char =
141-
isLower char || isUpper char || isDigit char
147+
let
148+
code =
149+
toCode char
150+
in
151+
isLowerCode code || isUpperCode code || isDigitCode code
142152

143153

144154
{-| Detect digits `0123456789`
@@ -154,11 +164,12 @@ isAlphaNum char =
154164
-}
155165
isDigit : Char -> Bool
156166
isDigit char =
157-
let
158-
code =
159-
toCode char
160-
in
161-
code <= 0x39 && 0x30 <= code
167+
isDigitCode (toCode char)
168+
169+
170+
isDigitCode : Int -> Bool
171+
isDigitCode code =
172+
code <= 0x39 && 0x30 <= code
162173

163174

164175
{-| Detect octal digits `01234567`

0 commit comments

Comments
 (0)