module Data.Text.Builder.Common.Internal where
import Data.Text (Text)
import Control.Monad.ST
import Data.Monoid
import Data.Word
import Data.Bits
import Text.Printf
import Debug.Trace
import Data.Char (ord)
import Data.Word.Synthetic (Word12)
import Data.Vector (Vector)
import Data.Foldable (fold)
import qualified Data.Vector as Vector
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LText
import qualified Data.Text.Lazy.IO as LText
import qualified Data.Text.Lazy.Builder as TBuilder
import qualified Data.Text.Lazy.Builder.Int as TBuilder
import qualified Data.Text.IO as Text
import qualified Data.Text.Array as A
import qualified Data.Text.Internal as TI
import qualified Data.Text.Internal.Unsafe.Char as TC
portableTextArray :: Text -> A.Array
portableTextArray = fst . portableUntext
portableTextLength :: Text -> Int
portableTextLength = snd . portableUntext
portableUntext :: Text -> (A.Array,Int)
portableUntext t =
let str = Text.unpack t
Sum len = foldMap (Sum . charUtf16Size) str
arr = A.run $ do
marr <- A.new len
writeString marr str
return marr
in (arr,len)
writeString :: A.MArray s -> String -> ST s ()
writeString marr = go 0 where
go i s = case s of
c : cs -> do
n <- TC.unsafeWrite marr i c
go (i + n) cs
[] -> return ()
charUtf16Size :: Char -> Int
charUtf16Size c = if ord c < 0x10000 then 1 else 2
hexValuesWord12Upper :: A.Array
hexValuesWord12Upper = portableTextArray $ fold
$ map (Text.pack . printf "%03X") [0 :: Int ..4096]
hexValuesWord12Lower :: A.Array
hexValuesWord12Lower = portableTextArray $ fold
$ map (Text.pack . printf "%03x") [0 :: Int ..4096]
hexValuesWord8Upper :: A.Array
hexValuesWord8Upper = portableTextArray $ fold
$ map (Text.pack . printf "%02X") [0 :: Int ..255]
hexValuesWord8Lower :: A.Array
hexValuesWord8Lower = portableTextArray $ fold
$ map (Text.pack . printf "%02x") [0 :: Int ..255]
twoDecimalDigits :: A.Array
twoDecimalDigits = portableTextArray
$ foldMap (Text.pack . printf "%02d") [0 :: Int ..99]
threeDecimalDigits :: A.Array
threeDecimalDigits = portableTextArray
$ foldMap (Text.pack . printf "%03d") [0 :: Int ..255]