Skip to content

Commit

Permalink
Speed up versionRangeP
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Jan 16, 2025
1 parent 2229287 commit e95eb38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
2 changes: 2 additions & 0 deletions ghcup.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ test-suite ghcup-test
GHCup.Prelude.File.Posix.TraversalsSpec
GHCup.Types.JSONSpec
GHCup.Utils.FileSpec
GHCup.ParserSpec
Spec

default-language: Haskell2010
Expand All @@ -477,6 +478,7 @@ test-suite ghcup-test
, ghcup
, hspec >=2.7.10 && <2.12
, hspec-golden-aeson ^>=0.9
, megaparsec >=8.0.0 && <9.8
, QuickCheck ^>=2.14.1 || ^>=2.15
, quickcheck-arbitrary-adt ^>=0.3.1.0
, text ^>=2.0 || ^>=2.1
Expand Down
2 changes: 1 addition & 1 deletion lib/GHCup/Prelude/MegaParsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{-# LANGUAGE OverloadedStrings #-}

{-|
Module : GHCup.Utils.MegaParsec
Module : GHCup.Prelude.MegaParsec
Description : MegaParsec utilities
Copyright : (c) Julian Ospald, 2020
License : LGPL-3.0
Expand Down
27 changes: 10 additions & 17 deletions lib/GHCup/Types/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,16 @@ versionCmpToText (VR_lteq ver') = "<= " <> prettyV ver'
versionCmpToText (VR_eq ver') = "== " <> prettyV ver'

versionCmpP :: MP.Parsec Void T.Text VersionCmp
versionCmpP =
fmap VR_gt (MP.try $ MPC.space *> MP.chunk ">" *> MPC.space *> versioningEnd)
<|> fmap
VR_gteq
(MP.try $ MPC.space *> MP.chunk ">=" *> MPC.space *> versioningEnd)
<|> fmap
VR_lt
(MP.try $ MPC.space *> MP.chunk "<" *> MPC.space *> versioningEnd)
<|> fmap
VR_lteq
(MP.try $ MPC.space *> MP.chunk "<=" *> MPC.space *> versioningEnd)
<|> fmap
VR_eq
(MP.try $ MPC.space *> MP.chunk "==" *> MPC.space *> versioningEnd)
<|> fmap
VR_eq
(MP.try $ MPC.space *> versioningEnd)
versionCmpP = either (fail . T.unpack) pure =<< (translate <$> (MPC.space *> MP.try (MP.takeWhileP Nothing (`elem` ['>', '<', '=']))) <*> (MPC.space *> versioningEnd))
where
translate ">" v = Right $ VR_gt v
translate ">=" v = Right $ VR_gteq v
translate "<" v = Right $ VR_lt v
translate "<=" v = Right $ VR_lteq v
translate "==" v = Right $ VR_eq v
translate "" v = Right $ VR_eq v
translate c _ = Left $ "unexpected comparator: " <> c


instance ToJSON VersionRange where
toJSON = String . verRangeToText
Expand Down

0 comments on commit e95eb38

Please sign in to comment.