Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Buf.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Buf (Buf (..), SizedStr, SizedBuilder) where
module Buf (Buf (..), SizedStr, SizedBuilder, SizedStrictBuilder) where

import Data.Char (intToDigit)
import Data.Coerce
import qualified Data.DList as D
import Data.Kind (Type)
import Data.String
Expand All @@ -15,8 +18,12 @@ import qualified Data.Text.Lazy.Builder.Int as T

newtype Sized a = Sized {unSized :: (a, Int)} deriving (Show, Ord, Eq)

newtype StrictBuilder = StrictBuilder {unStrictBuilder :: T.Builder}
deriving (Eq, Ord, Show, IsString, Semigroup, Monoid)

type SizedStr = Sized (D.DList Char)
type SizedBuilder = Sized T.Builder
type SizedStrictBuilder = Sized StrictBuilder

instance (IsString a) => IsString (Sized a) where
fromString s = Sized (fromString s, length s)
Expand Down Expand Up @@ -74,3 +81,13 @@ instance Buf SizedBuilder where
digit c = Sized (T.hexadecimal c, 1)
finalize = T.toLazyText . fst . unSized
size = snd . unSized

instance Buf SizedStrictBuilder where
type Output SizedStrictBuilder = S.Text
str = coerce $ str @SizedBuilder
sText = coerce $ sText @SizedBuilder
lText = coerce $ lText @SizedBuilder
singleton = coerce $ singleton @SizedBuilder
digit = coerce $ digit @SizedBuilder
finalize = L.toStrict . coerce (finalize @SizedBuilder)
size = coerce $ size @SizedBuilder
7 changes: 7 additions & 0 deletions src/Language/Haskell/Printf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ your input must be an instance of "Bounded".
module Language.Haskell.Printf (
s,
t,
st,
p,
hp,
) where
Expand Down Expand Up @@ -77,6 +78,12 @@ t = quoter $ \s' -> do
(lhss, rhs) <- toSplices s' OutputText
return $ LamE lhss rhs

-- | Behaves identically to 's', but produces strict 'Data.Text.Text'.
st :: QuasiQuoter
st = quoter $ \s' -> do
(lhss, rhs) <- toSplices s' OutputStrictText
return $ LamE lhss rhs

{- | Like 's', but prints the resulting string to @stdout@.

@
Expand Down
4 changes: 3 additions & 1 deletion src/Language/Haskell/Printf/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Language.Haskell.TH.Syntax
import Buf (
SizedBuilder,
SizedStr,
SizedStrictBuilder,
finalize,
)
import Control.Monad (mapAndUnzipM)
Expand All @@ -30,7 +31,7 @@ import Parser.Types hiding (
width,
)

data OutputType = OutputString | OutputText
data OutputType = OutputString | OutputText | OutputStrictText
deriving (Show, Eq, Ord, Generic, Enum, Bounded)

{- | Takes a format string as input and produces a tuple @(args, outputExpr)@.
Expand All @@ -57,6 +58,7 @@ toSplices s' ot = case parseStr s' of
otype = case ot of
OutputString -> [t|SizedStr|]
OutputText -> [t|SizedBuilder|]
OutputStrictText -> [t|SizedStrictBuilder|]

extractExpr :: Atom -> Q ([Name], ExpQ)
extractExpr (Str s') = return ([], [|fromString $(stringE s')|])
Expand Down