From 76f9dc5e98e5d270421bb3fa89d283b8877537a9 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Wed, 31 Jul 2024 00:02:10 +0200 Subject: [PATCH] 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. --- src/Char.elm | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Char.elm b/src/Char.elm index 059c75a5..f7f76e40 100644 --- a/src/Char.elm +++ b/src/Char.elm @@ -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. @@ -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. @@ -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. @@ -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` @@ -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`