Skip to content

Commit 4b688e9

Browse files
committed
Create a Text type
1 parent def6d63 commit 4b688e9

3 files changed

Lines changed: 283 additions & 54 deletions

File tree

  • benchmark/Streamly/Benchmark/Unicode
  • core/src/Streamly/Internal/Unicode
  • src/Streamly/Internal/Unicode

benchmark/Streamly/Benchmark/Unicode/Utf8.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
--------------------------------------------------------------------------------
1212

1313
import Data.Char (chr)
14-
import Streamly.Internal.Unicode.Utf8 (Utf8)
14+
import Streamly.Internal.Unicode.Utf8 (Text)
1515

1616
import Test.Tasty.Bench hiding (env)
1717
import Streamly.Benchmark.Common
1818

19-
import qualified Streamly.Internal.Unicode.Utf8 as Utf8
19+
import qualified Streamly.Internal.Unicode.Utf8 as Text
2020

2121
--------------------------------------------------------------------------------
2222
-- Utilities
@@ -30,12 +30,12 @@ moduleName = "Unicode.Stream"
3030
--------------------------------------------------------------------------------
3131

3232
{-# INLINE pack #-}
33-
pack :: Int -> Utf8
34-
pack i = Utf8.pack $ chr <$> [1 .. i]
33+
pack :: Int -> Text
34+
pack i = Text.pack $ chr <$> [1 .. i]
3535

3636
{-# INLINE packUnpack #-}
3737
packUnpack :: Int -> String
38-
packUnpack = Utf8.unpack . pack
38+
packUnpack = Text.unpack . pack
3939

4040
--------------------------------------------------------------------------------
4141
-- Main function

core/src/Streamly/Internal/Unicode/String.hs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ module Streamly.Internal.Unicode.String
4040
-- $setup
4141

4242
str
43+
44+
-- * Internals
45+
, strWith
4346
) where
4447

4548

@@ -92,30 +95,42 @@ strParser = Parser.many content Fold.toList
9295
-- order is important
9396
content = plainText <|> escHash <|> lineCont <|> var <|> plainHash
9497

95-
strSegmentExp :: StrSegment -> Q Exp
96-
strSegmentExp (StrText text) = stringE text
97-
strSegmentExp (StrVar name) = do
98+
strSegmentExp ::
99+
(Q Exp -> Q Exp) -> (Q Exp -> Q Exp) -> StrSegment -> Q Exp
100+
strSegmentExp f _ (StrText text) = f (stringE text)
101+
strSegmentExp _ f (StrVar name) = do
98102
valueName <- lookupValueName name
99103
case valueName of
100-
Just vn -> varE vn
104+
Just vn -> f (varE vn)
101105
Nothing ->
102106
fail
103107
$ "str quote: Haskell symbol `" ++ name
104108
++ "` is not in scope"
105109

106-
strExp :: [StrSegment] -> Q Exp
107-
strExp xs = appE [| concat |] $ listE $ map strSegmentExp xs
110+
strExp :: Q Exp -> (Q Exp -> Q Exp) -> (Q Exp -> Q Exp) -> [StrSegment] -> Q Exp
111+
strExp c f g xs = appE c $ listE $ map (strSegmentExp f g) xs
108112

109113
parseStr :: String -> Either ParseError [StrSegment]
110114
parseStr = runIdentity . Stream.parse strParser . Stream.fromList
111115

112-
expandVars :: String -> Q Exp
113-
expandVars input =
116+
expandVars :: Q Exp -> (Q Exp -> Q Exp) -> (Q Exp -> Q Exp) -> String -> Q Exp
117+
expandVars c f g input =
114118
case parseStr input of
115-
Left e ->
116-
fail $ "str QuasiQuoter parse error: " ++ displayException e
117-
Right x ->
118-
strExp x
119+
Left e -> fail $ "str QuasiQuoter parse error: " ++ displayException e
120+
Right x -> strExp c f g x
121+
122+
strWith :: Q Exp -> (Q Exp -> Q Exp) -> (Q Exp -> Q Exp) -> QuasiQuoter
123+
strWith c f g =
124+
QuasiQuoter
125+
{ quoteExp = expandVars c f g
126+
, quotePat = notSupported
127+
, quoteType = notSupported
128+
, quoteDec = notSupported
129+
}
130+
131+
where
132+
133+
notSupported = error "str: Not supported."
119134

120135
-- | A QuasiQuoter that treats the input as a string literal:
121136
--
@@ -145,14 +160,4 @@ expandVars input =
145160
-- "hello world!"
146161
--
147162
str :: QuasiQuoter
148-
str =
149-
QuasiQuoter
150-
{ quoteExp = expandVars
151-
, quotePat = notSupported
152-
, quoteType = notSupported
153-
, quoteDec = notSupported
154-
}
155-
156-
where
157-
158-
notSupported = error "str: Not supported."
163+
str = strWith [|concat|] id id

0 commit comments

Comments
 (0)