Skip to content

Avoid duplicate toCode calls in multiple Char.is... functions #1146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
jfmengels committed Jul 30, 2024
commit 76f9dc5e98e5d270421bb3fa89d283b8877537a9
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
Loading