Skip to content

Commit

Permalink
Avoid duplicate toCode calls in multiple Char.is... functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jfmengels committed Jul 30, 2024
1 parent 65cea00 commit 76f9dc5
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/Char.elm
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ type Char = Char -- NOTE: The compiler provides the real implementation.
-}
isUpper : Char -> Bool
isUpper char =
let
code =
toCode char
in
code <= 0x5A && 0x41 <= code
isUpperCode (toCode char)


isUpperCode : Int -> Bool
isUpperCode code =
code <= 0x5A && 0x41 <= code


{-| Detect lower case ASCII characters.
Expand All @@ -101,11 +102,12 @@ isUpper char =
-}
isLower : Char -> Bool
isLower char =
let
code =
toCode char
in
0x61 <= code && code <= 0x7A
isLowerCode (toCode char)


isLowerCode : Int -> Bool
isLowerCode code =
0x61 <= code && code <= 0x7A


{-| Detect upper case and lower case ASCII characters.
Expand All @@ -121,7 +123,11 @@ isLower char =
-}
isAlpha : Char -> Bool
isAlpha char =
isLower char || isUpper char
let
code =
toCode char
in
isLowerCode code || isUpperCode code


{-| Detect upper case and lower case ASCII characters.
Expand All @@ -138,7 +144,11 @@ isAlpha char =
-}
isAlphaNum : Char -> Bool
isAlphaNum char =
isLower char || isUpper char || isDigit char
let
code =
toCode char
in
isLowerCode code || isUpperCode code || isDigitCode code


{-| Detect digits `0123456789`
Expand All @@ -154,11 +164,12 @@ isAlphaNum char =
-}
isDigit : Char -> Bool
isDigit char =
let
code =
toCode char
in
code <= 0x39 && 0x30 <= code
isDigitCode (toCode char)


isDigitCode : Int -> Bool
isDigitCode code =
code <= 0x39 && 0x30 <= code


{-| Detect octal digits `01234567`
Expand Down

0 comments on commit 76f9dc5

Please sign in to comment.