Skip to content

Commit bf1e7d2

Browse files
committed
Move utilities from .Common to new Utils module (formal API change)
1 parent 5cc9760 commit bf1e7d2

File tree

6 files changed

+83
-39
lines changed

6 files changed

+83
-39
lines changed

lib/Text/Regex/TDFA/Common.hs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Data.Sequence as S(Seq)
2121
--import Debug.Trace
2222

2323
import Text.Regex.TDFA.IntArrTrieSet(TrieSet)
24+
import Utils
2425

2526
{-# INLINE look #-}
2627
look :: Int -> IntMap a -> a
@@ -30,43 +31,6 @@ common_error :: String -> String -> a
3031
common_error moduleName message =
3132
error ("Explict error in module "++moduleName++" : "++message)
3233

33-
on :: (t1 -> t1 -> t2) -> (t -> t1) -> t -> t -> t2
34-
f `on` g = (\x y -> (g x) `f` (g y))
35-
36-
-- | After 'sort' or 'sortBy' the use of 'nub' or 'nubBy' can be replaced by 'norep' or 'norepBy'.
37-
norep :: (Eq a) => [a]->[a]
38-
norep [] = []
39-
norep x@[_] = x
40-
norep (a:bs@(c:cs)) | a==c = norep (a:cs)
41-
| otherwise = a:norep bs
42-
43-
-- | After 'sort' or 'sortBy' the use of 'nub' or 'nubBy' can be replaced by 'norep' or 'norepBy'.
44-
norepBy :: (a -> a -> Bool) -> [a] -> [a]
45-
norepBy _ [] = []
46-
norepBy _ x@[_] = x
47-
norepBy eqF (a:bs@(c:cs)) | a `eqF` c = norepBy eqF (a:cs)
48-
| otherwise = a:norepBy eqF bs
49-
50-
mapFst :: (Functor f) => (t -> t2) -> f (t, t1) -> f (t2, t1)
51-
mapFst f = fmap (\ (a,b) -> (f a,b))
52-
53-
mapSnd :: (Functor f) => (t1 -> t2) -> f (t, t1) -> f (t, t2)
54-
mapSnd f = fmap (\ (a,b) -> (a,f b))
55-
56-
fst3 :: (a,b,c) -> a
57-
fst3 (x,_,_) = x
58-
59-
snd3 :: (a,b,c) -> b
60-
snd3 (_,x,_) = x
61-
62-
thd3 :: (a,b,c) -> c
63-
thd3 (_,_,x) = x
64-
65-
flipOrder :: Ordering -> Ordering
66-
flipOrder GT = LT
67-
flipOrder LT = GT
68-
flipOrder EQ = EQ
69-
7034
noWin :: WinTags -> Bool
7135
noWin = null
7236

lib/Text/Regex/TDFA/CorePattern.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import Data.Semigroup as Sem
4949

5050
import Text.Regex.TDFA.Common {- all -}
5151
import Text.Regex.TDFA.Pattern(Pattern(..),starTrans)
52+
import Utils
5253
-- import Debug.Trace
5354

5455
{- By Chris Kuklewicz, 2007. BSD License, see the LICENSE file. -}

lib/Text/Regex/TDFA/TDFA.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import qualified Text.Regex.TDFA.IntArrTrieSet as Trie(lookupAsc,fromSinglesMerg
2828
import Text.Regex.TDFA.Pattern(Pattern)
2929
--import Text.Regex.TDFA.RunMutState(toInstructions)
3030
import Text.Regex.TDFA.TNFA(patternToNFA)
31+
import Utils
3132
--import Debug.Trace
3233

3334
{- By Chris Kuklewicz, 2007. BSD License, see the LICENSE file. -}

lib/Text/Regex/TDFA/TNFA.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ import qualified Data.Set as S(Set,insert,toAscList,empty)
5656
import Text.Regex.TDFA.Common(QT(..),QNFA(..),QTrans,TagTask(..),TagUpdate(..),DoPa(..)
5757
,CompOption(..)
5858
,Tag,TagTasks,TagList,Index,WinTags,GroupIndex,GroupInfo(..)
59-
,common_error,noWin,snd3,mapSnd)
59+
,common_error,noWin)
6060
import Text.Regex.TDFA.CorePattern(Q(..),P(..),OP(..),WhichTest,cleanNullView,NullView
6161
,SetTestInfo(..),Wanted(..),TestInfo
6262
,mustAccept,cannotAccept,patternToQ)
6363
import Text.Regex.TDFA.Pattern(Pattern(..),PatternSet(..),unSEC,PatternSetCharacterClass(..))
64+
import Utils
6465
--import Debug.Trace
6566

6667
ecart :: String -> a -> a

lib/Utils.hs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE RankNTypes #-}
3+
4+
-- | Internal module for utilities used in the implementation.
5+
6+
module Utils (module Utils, module X) where
7+
8+
import Control.Applicative (Const(..))
9+
import Control.Applicative as X ((<*>))
10+
import Data.Functor as X
11+
import Data.Functor.Identity
12+
13+
-- * Lenses
14+
---------------------------------------------------------------------------
15+
16+
type Lens' o i = forall f. Functor f => (i -> f i) -> (o -> f o)
17+
18+
type LensGet o i = o -> i
19+
type LensSet o i = i -> o -> o
20+
type LensMap o i = (i -> i) -> o -> o
21+
22+
infixl 8 ^.
23+
-- | Get inner part @i@ of structure @o@ as designated by @Lens' o i@.
24+
(^.) :: o -> Lens' o i -> i
25+
o ^. l = getConst $ l Const o
26+
27+
-- | Set inner part @i@ of structure @o@ as designated by @Lens' o i@.
28+
set :: Lens' o i -> LensSet o i
29+
set l = over l . const
30+
31+
-- | Modify inner part @i@ of structure @o@ using a function @i -> i@.
32+
over :: Lens' o i -> LensMap o i
33+
over l f o = runIdentity $ l (Identity . f) o
34+
35+
-- * Misc
36+
---------------------------------------------------------------------------
37+
38+
#if !MIN_VERSION_base(4,11,0)
39+
(<&>) :: Functor f => f a -> (a -> b) -> f b
40+
(<&>) = flip fmap
41+
#endif
42+
43+
-- | After 'sort' or 'sortBy' the use of 'nub' or 'nubBy' can be replaced by 'norep' or 'norepBy'.
44+
norep :: (Eq a) => [a]->[a]
45+
norep [] = []
46+
norep x@[_] = x
47+
norep (a:bs@(c:cs)) | a==c = norep (a:cs)
48+
| otherwise = a:norep bs
49+
50+
-- | After 'sort' or 'sortBy' the use of 'nub' or 'nubBy' can be replaced by 'norep' or 'norepBy'.
51+
norepBy :: (a -> a -> Bool) -> [a] -> [a]
52+
norepBy _ [] = []
53+
norepBy _ x@[_] = x
54+
norepBy eqF (a:bs@(c:cs)) | a `eqF` c = norepBy eqF (a:cs)
55+
| otherwise = a:norepBy eqF bs
56+
57+
mapFst :: (Functor f) => (t -> t2) -> f (t, t1) -> f (t2, t1)
58+
mapFst f = fmap (\ (a,b) -> (f a,b))
59+
60+
mapSnd :: (Functor f) => (t1 -> t2) -> f (t, t1) -> f (t, t2)
61+
mapSnd f = fmap (\ (a,b) -> (a,f b))
62+
63+
fst3 :: (a,b,c) -> a
64+
fst3 (x,_,_) = x
65+
66+
snd3 :: (a,b,c) -> b
67+
snd3 (_,x,_) = x
68+
69+
thd3 :: (a,b,c) -> c
70+
thd3 (_,_,x) = x
71+
72+
flipOrder :: Ordering -> Ordering
73+
flipOrder GT = LT
74+
flipOrder LT = GT
75+
flipOrder EQ = EQ

regex-tdfa.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ library
8484
Text.Regex.TDFA.Text
8585
Text.Regex.TDFA.Text.Lazy
8686

87-
other-modules: Paths_regex_tdfa
87+
other-modules: Utils
88+
Paths_regex_tdfa
8889

8990
-- Support Semigroup instances uniformly
9091
--
@@ -123,6 +124,7 @@ library
123124
UnboxedTuples
124125
UnliftedFFITypes
125126
other-extensions: CPP
127+
RankNTypes
126128

127129
ghc-options: -Wall -funbox-strict-fields -fspec-constr-count=10 -fno-warn-orphans
128130

0 commit comments

Comments
 (0)