File: Fusion.hs

package info (click to toggle)
haskell-text-manipulate 0.3.1.0-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 112 kB
  • sloc: haskell: 711; makefile: 6
file content (270 lines) | stat: -rw-r--r-- 8,345 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}

-- Module      : Data.Text.Manipulate.Internal.Fusion
-- Copyright   : (c) 2014-2020 Brendan Hay <brendan.g.hay@gmail.com>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : Brendan Hay <brendan.g.hay@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Data.Text.Manipulate.Internal.Fusion where

import qualified Data.Char as Char
import Data.Text (Text)
import qualified Data.Text.Internal.Fusion as Fusion
import Data.Text.Internal.Fusion.CaseMapping (lowerMapping, upperMapping)
import Data.Text.Internal.Fusion.Common
import Data.Text.Internal.Fusion.Types
import qualified Data.Text.Internal.Lazy.Fusion as LFusion
import qualified Data.Text.Lazy as LText
import Data.Text.Manipulate.Internal.Types

#if MIN_VERSION_text(2,0,0)
import Data.Bits (shiftL, shiftR, (.&.))
import GHC.Exts (Char(..), Int(..), chr#)
import GHC.Int (Int64(..))
#endif

takeWord :: Stream Char -> Stream Char
takeWord = transform (const Done) yield . tokenise
{-# INLINE [0] takeWord #-}

dropWord :: Stream Char -> Stream Char
dropWord (tokenise -> Stream next0 s0 len) = Stream next (True :*: s0) len
  where
    next (skip :*: s) =
      case next0 s of
        Done -> Done
        Skip s' -> Skip (skip :*: s')
        Yield t s' ->
          case t of
            B '\0' -> Skip (False :*: s')
            B _ | skip -> Skip (False :*: s')
            B c -> Yield c (False :*: s')
            _ | skip -> Skip (skip :*: s')
            U c -> Yield c (skip :*: s')
            L c -> Yield c (skip :*: s')
{-# INLINE [0] dropWord #-}

toTitle :: Stream Char -> Stream Char
toTitle = mapHead toUpper . transformWith (yield ' ') upper lower . tokenise
{-# INLINE [0] toTitle #-}

toCamel :: Stream Char -> Stream Char
toCamel = mapHead toLower . transformWith skip' upper lower . tokenise
{-# INLINE [0] toCamel #-}

toPascal :: Stream Char -> Stream Char
toPascal = mapHead toUpper . transformWith skip' upper lower . tokenise
{-# INLINE [0] toPascal #-}

toSnake :: Stream Char -> Stream Char
toSnake = transform (yield '_') lower . tokenise
{-# INLINE [0] toSnake #-}

toSpinal :: Stream Char -> Stream Char
toSpinal = transform (yield '-') lower . tokenise
{-# INLINE [0] toSpinal #-}

toTrain :: Stream Char -> Stream Char
toTrain = mapHead toUpper . transformWith (yield '-') upper lower . tokenise
{-# INLINE [0] toTrain #-}

strict :: (Stream Char -> Stream Char) -> Text -> Text
strict f t = Fusion.unstream (f (Fusion.stream t))
{-# INLINE [0] strict #-}

lazy :: (Stream Char -> Stream Char) -> LText.Text -> LText.Text
lazy f t = LFusion.unstream (f (LFusion.stream t))
{-# INLINE [0] lazy #-}

skip' :: forall s. s -> Step (CC s) Char
#if MIN_VERSION_text(2,0,0)
skip' s = Skip (CC s 0)
#else
skip' s = Skip (CC s '\0' '\0')
#endif

yield, upper, lower :: forall s. Char -> s -> Step (CC s) Char
#if MIN_VERSION_text(2,0,0)

yield !c s = Yield c (CC s 0)

upper !c@(C# c#) s = case I64# (upperMapping c#) of
  0 -> Yield c (CC s 0)
  ab -> let (a, b) = chopOffChar ab in
              Yield a (CC s b)

lower !c@(C# c#) s = case I64# (lowerMapping c#) of
  0 -> Yield c (CC s 0)
  ab -> let (a, b) = chopOffChar ab in
              Yield a (CC s b)

chopOffChar :: Int64 -> (Char, Int64)
chopOffChar ab = (chr a, ab `shiftR` 21)
  where
    chr (I# n) = C# (chr# n)
    mask = (1 `shiftL` 21) - 1
    a = fromIntegral $ ab .&. mask

#else

yield !c s = Yield c (CC s '\0' '\0')
upper !c s = upperMapping c s
lower !c s = lowerMapping c s

#endif

-- | Step across word boundaries using a custom action, and transform
-- both subsequent uppercase and lowercase characters uniformly.
--
-- /See:/ 'transformWith'
transform ::
  -- | Boundary action.
  (forall s. s -> Step (CC s) Char) ->
  -- | Character mapping.
  (forall s. Char -> s -> Step (CC s) Char) ->
  -- | Input stream.
  Stream Token ->
  Stream Char
transform s m = transformWith s m m
{-# INLINE [0] transform #-}

-- | Step across word boundaries using a custom action, and transform
-- subsequent characters after the word boundary is encountered with a mapping
-- depending on case.
transformWith ::
  -- | Boundary action.
  (forall s. s -> Step (CC s) Char) ->
  -- | Boundary mapping.
  (forall s. Char -> s -> Step (CC s) Char) ->
  -- | Subsequent character mapping.
  (forall s. Char -> s -> Step (CC s) Char) ->
  -- | Input stream.
  Stream Token ->
  Stream Char
transformWith md mu mc (Stream next0 s0 len) =
  -- HINT: len incorrect when the boundary replacement yields a char.
#if MIN_VERSION_text(2,0,0)
  Stream next (CC (False :*: False :*: s0) 0) len
#else
  Stream next (CC (False :*: False :*: s0) '\0' '\0') len
#endif
  where
#if MIN_VERSION_text(2,0,0)
    next (CC (up :*: prev :*: s) 0) =
#else
    next (CC (up :*: prev :*: s) '\0' _) =
#endif
      case next0 s of
        Done -> Done
#if MIN_VERSION_text(2,0,0)
        Skip s' -> Skip (CC (up :*: prev :*: s') 0)
#else
        Skip s' -> Skip (CC (up :*: prev :*: s') '\0' '\0')
#endif
        Yield t s' ->
          case t of
            B _ -> md (False :*: True :*: s')
            U c | prev -> mu c (True :*: False :*: s')
            L c | prev -> mu c (False :*: False :*: s')
            U c | up -> mu c (True :*: False :*: s')
            U c -> mc c (True :*: False :*: s')
            L c -> mc c (False :*: False :*: s')
#if MIN_VERSION_text(2,0,0)
    next (CC s ab) = let (a, b) = chopOffChar ab in Yield a (CC s b)
#else
    next (CC s a b) = Yield a (CC s b '\0')
#endif
{-# INLINE [0] transformWith #-}

-- | A token representing characters and boundaries in a stream.
data Token
  = -- | Word boundary.
    B {-# UNPACK #-} !Char
  | -- | Upper case character.
    U {-# UNPACK #-} !Char
  | -- | Lower case character.
    L {-# UNPACK #-} !Char
  deriving (Show)

-- | Tokenise a character stream using the default 'isBoundary' predicate.
--
-- /See:/ 'tokeniseWith'
tokenise ::
  -- | Input stream.
  Stream Char ->
  Stream Token
tokenise = tokeniseWith isBoundary
{-# INLINE [0] tokenise #-}

-- | Tokenise a character stream using a custom boundary predicate.
tokeniseWith ::
  -- | Boundary predicate.
  (Char -> Bool) ->
  -- | Input stream.
  Stream Char ->
  Stream Token
tokeniseWith f (Stream next0 s0 len) =
  -- HINT: len incorrect if there are adjacent boundaries, which are skipped.
#if MIN_VERSION_text(2,0,0)
  Stream next (CC (True :*: False :*: False :*: s0) 0) len
#else
  Stream next (CC (True :*: False :*: False :*: s0) '\0' '\0') len
#endif
  where
#if MIN_VERSION_text(2,0,0)
    next (CC (start :*: up :*: prev :*: s) 0) =
#else
    next (CC (start :*: up :*: prev :*: s) '\0' _) =
#endif
      case next0 s of
        Done -> Done
#if MIN_VERSION_text(2,0,0)
        Skip s' -> Skip (CC (start :*: up :*: prev :*: s') 0)
#else
        Skip s' -> Skip (CC (start :*: up :*: prev :*: s') '\0' '\0')
#endif
        Yield c s'
          | not b, start -> push
          | up -> push
          | b, prev -> Skip (step start)
          | otherwise -> push
          where
            push
              | b = Yield (B c) (step False)
              | u, skip = Yield (U c) (step False)
#if MIN_VERSION_text(2,0,0)
              | u = Yield (B '\0') (CC (False :*: u :*: b :*: s') (fromIntegral (Char.ord c)))
#else
              | u = Yield (B '\0') (CC (False :*: u :*: b :*: s') c '\0')
#endif
              | otherwise = Yield (L c) (step False)

#if MIN_VERSION_text(2,0,0)
            step p = CC (p :*: u :*: b :*: s') 0
#else
            step p = CC (p :*: u :*: b :*: s') '\0' '\0'
#endif

            skip = up || start || prev

            b = f c
            u = Char.isUpper c
#if MIN_VERSION_text(2,0,0)
    next (CC s ab) = let (a, b) = chopOffChar ab in Yield (U a) (CC s b)
#else
    next (CC s a b) = Yield (U a) (CC s b '\0')
#endif
{-# INLINE [0] tokeniseWith #-}

mapHead :: (Stream Char -> Stream Char) -> Stream Char -> Stream Char
mapHead f s = maybe s (\(x, s') -> f (singleton x) `append` s') (uncons s)
{-# INLINE [0] mapHead #-}