module Floskell.Buffer
( Buffer
, empty
, newline
, write
, line
, column
, toLazyByteString
) where
import qualified Data.ByteString as BS
import Data.ByteString.Builder ( Builder )
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Lazy as BL
data Buffer =
Buffer { Buffer -> Builder
bufferData :: !Builder
, Buffer -> Builder
bufferDataNoSpace :: !Builder
, Buffer -> Int
bufferLine :: !Int
, Buffer -> Int
bufferColumn :: !Int
}
empty :: Buffer
empty :: Buffer
empty = Buffer { bufferData :: Builder
bufferData = forall a. Monoid a => a
mempty
, bufferDataNoSpace :: Builder
bufferDataNoSpace = forall a. Monoid a => a
mempty
, bufferLine :: Int
bufferLine = Int
0
, bufferColumn :: Int
bufferColumn = Int
0
}
write :: BS.ByteString -> Buffer -> Buffer
write :: ByteString -> Buffer -> Buffer
write ByteString
str Buffer
buf =
Buffer
buf { bufferData :: Builder
bufferData = Builder
newBufferData
, bufferDataNoSpace :: Builder
bufferDataNoSpace =
if (Word8 -> Bool) -> ByteString -> Bool
BS.all (forall a. Eq a => a -> a -> Bool
== Word8
32) ByteString
str then Buffer -> Builder
bufferData Buffer
buf else Builder
newBufferData
, bufferColumn :: Int
bufferColumn = Buffer -> Int
bufferColumn Buffer
buf forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
str
}
where
newBufferData :: Builder
newBufferData = Buffer -> Builder
bufferData Buffer
buf forall a. Monoid a => a -> a -> a
`mappend` ByteString -> Builder
BB.byteString ByteString
str
newline :: Buffer -> Buffer
newline :: Buffer -> Buffer
newline Buffer
buf = Buffer
buf { bufferData :: Builder
bufferData = Builder
newBufferData
, bufferDataNoSpace :: Builder
bufferDataNoSpace = Builder
newBufferData
, bufferLine :: Int
bufferLine = Buffer -> Int
bufferLine Buffer
buf forall a. Num a => a -> a -> a
+ Int
1
, bufferColumn :: Int
bufferColumn = Int
0
}
where
newBufferData :: Builder
newBufferData = Buffer -> Builder
bufferDataNoSpace Buffer
buf forall a. Monoid a => a -> a -> a
`mappend` Char -> Builder
BB.char7 Char
'\n'
line :: Buffer -> Int
line :: Buffer -> Int
line = Buffer -> Int
bufferLine
column :: Buffer -> Int
column :: Buffer -> Int
column = Buffer -> Int
bufferColumn
toLazyByteString :: Buffer -> BL.ByteString
toLazyByteString :: Buffer -> ByteString
toLazyByteString = Builder -> ByteString
BB.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Buffer -> Builder
bufferData