module Ecluse.Core.Version.Token (
VToken (..),
parseNumSeg,
numOr0,
isAsciiAlphaNum,
maxVersionLength,
) where
import Data.Char (isAsciiLower, isAsciiUpper, isDigit)
import Data.Text qualified as T
data VToken = VNum Integer | VStr Text
deriving stock (VToken -> VToken -> Bool
(VToken -> VToken -> Bool)
-> (VToken -> VToken -> Bool) -> Eq VToken
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VToken -> VToken -> Bool
== :: VToken -> VToken -> Bool
$c/= :: VToken -> VToken -> Bool
/= :: VToken -> VToken -> Bool
Eq, Int -> VToken -> ShowS
[VToken] -> ShowS
VToken -> String
(Int -> VToken -> ShowS)
-> (VToken -> String) -> ([VToken] -> ShowS) -> Show VToken
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VToken -> ShowS
showsPrec :: Int -> VToken -> ShowS
$cshow :: VToken -> String
show :: VToken -> String
$cshowList :: [VToken] -> ShowS
showList :: [VToken] -> ShowS
Show)
instance Ord VToken where
compare :: VToken -> VToken -> Ordering
compare (VNum Integer
m) (VNum Integer
n) = Integer -> Integer -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Integer
m Integer
n
compare (VStr Text
s) (VStr Text
t) = Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Text
s Text
t
compare (VNum Integer
_) (VStr Text
_) = Ordering
GT
compare (VStr Text
_) (VNum Integer
_) = Ordering
LT
maxVersionLength :: Int
maxVersionLength :: Int
maxVersionLength = Int
1024
parseNumSeg :: Text -> Maybe Integer
parseNumSeg :: Text -> Maybe Integer
parseNumSeg Text
t
| Bool -> Bool
not (Text -> Bool
T.null Text
t) Bool -> Bool -> Bool
&& (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
t = String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
forall a. ToString a => a -> String
toString Text
t)
| Bool
otherwise = Maybe Integer
forall a. Maybe a
Nothing
numOr0 :: Text -> Integer
numOr0 :: Text -> Integer
numOr0 Text
t = if Text -> Bool
T.null Text
t then Integer
0 else Integer -> Maybe Integer -> Integer
forall a. a -> Maybe a -> a
fromMaybe Integer
0 (String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
forall a. ToString a => a -> String
toString Text
t))
isAsciiAlphaNum :: Char -> Bool
isAsciiAlphaNum :: Char -> Bool
isAsciiAlphaNum Char
c = Char -> Bool
isAsciiUpper Char
c Bool -> Bool -> Bool
|| Char -> Bool
isAsciiLower Char
c Bool -> Bool -> Bool
|| Char -> Bool
isDigit Char
c