-- |
-- Module      : Basement.String
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : portable
--
-- A String type backed by a UTF8 encoded byte array and all the necessary
-- functions to manipulate the string.
--
-- You can think of String as a specialization of a byte array that
-- have element of type Char.
--
-- The String data must contain UTF8 valid data.
--
{-# LANGUAGE BangPatterns               #-}
{-# LANGUAGE MagicHash                  #-}
{-# LANGUAGE NoImplicitPrelude          #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE UnboxedTuples              #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE CPP                        #-}
module Basement.String
    ( String(..)
    , MutableString(..)
    , create
    , replicate
    , length
    -- * Binary conversion
    , Encoding(..)
    , fromBytes
    , fromChunkBytes
    , fromBytesUnsafe
    , fromBytesLenient
    , toBytes
    , mutableValidate
    , copy
    , ValidationFailure(..)
    , index
    , null
    , drop
    , take
    , splitAt
    , revDrop
    , revTake
    , revSplitAt
    , splitOn
    , sub
    , elem
    , indices
    , intersperse
    , span
    , spanEnd
    , break
    , breakEnd
    , breakElem
    , breakLine
    , dropWhile
    , singleton
    , charMap
    , snoc
    , cons
    , unsnoc
    , uncons
    , find
    , findIndex
    , sortBy
    , filter
    , reverse
    , replace
    , builderAppend
    , builderBuild
    , builderBuild_
    , readInteger
    , readIntegral
    , readNatural
    , readDouble
    , readRational
    , readFloatingExact
    , upper
    , lower
    , caseFold
    , isPrefixOf
    , isSuffixOf
    , isInfixOf
    , stripPrefix
    , stripSuffix
    , all
    , any
    -- * Legacy utility
    , lines
    , words
    , toBase64
    , toBase64URL
    , toBase64OpenBSD
    ) where

import           Basement.UArray           (UArray)
import qualified Basement.UArray           as Vec
import qualified Basement.UArray           as C
import qualified Basement.UArray.Mutable   as MVec
import           Basement.Block.Mutable (Block(..), MutableBlock(..))
import qualified Basement.Block.Mutable    as MBLK
import           Basement.Compat.Bifunctor
import           Basement.Compat.Base
import           Basement.Compat.Natural
import           Basement.Compat.MonadTrans
import           Basement.Compat.Primitive
import           Basement.Types.OffsetSize
import           Basement.Numerical.Additive
import           Basement.Numerical.Subtractive
import           Basement.Numerical.Multiplicative
import           Basement.Numerical.Number
import           Basement.Cast
import           Basement.Monad
import           Basement.PrimType
import           Basement.FinalPtr
import           Basement.IntegralConv
import           Basement.Floating
import           Basement.MutableBuilder
import           Basement.String.CaseMapping (upperMapping, lowerMapping, foldMapping)
import           Basement.UTF8.Table
import           Basement.UTF8.Helper
import           Basement.UTF8.Base
import           Basement.UTF8.Types
import           Basement.UArray.Base as C (onBackendPrim, onBackend, onBackendPure, offset, ValidRange(..), offsetsValidRange, MUArray(..), MUArrayBackend(..))
import           Basement.Alg.Class (Indexable)
import qualified Basement.Alg.UTF8 as UTF8
import qualified Basement.Alg.String as Alg
import           Basement.Types.Char7 (Char7(..), c7Upper, c7Lower)
import qualified Basement.Types.Char7 as Char7
import           GHC.Prim
import           GHC.ST
import           GHC.Types
import           GHC.Word
#if MIN_VERSION_base(4,9,0)
import           GHC.Char
#endif

 -- temporary
import qualified Data.List
import           Data.Ratio
import           Data.Char (toUpper, toLower)
import qualified Prelude

import qualified Basement.String.Encoding.Encoding   as Encoder
import qualified Basement.String.Encoding.ASCII7     as Encoder
import qualified Basement.String.Encoding.UTF16      as Encoder
import qualified Basement.String.Encoding.UTF32      as Encoder
import qualified Basement.String.Encoding.ISO_8859_1 as Encoder

-- | UTF8 Encoder
data EncoderUTF8 = EncoderUTF8

instance Encoder.Encoding EncoderUTF8 where
    type Unit EncoderUTF8 = Word8
    type Error EncoderUTF8 = ValidationFailure
    encodingNext :: EncoderUTF8
-> (Offset (Unit EncoderUTF8) -> Unit EncoderUTF8)
-> Offset (Unit EncoderUTF8)
-> Either (Error EncoderUTF8) (Char, Offset (Unit EncoderUTF8))
encodingNext  EncoderUTF8
_ = \Offset (Unit EncoderUTF8) -> Unit EncoderUTF8
ofs -> forall a b. b -> Either a b
Right forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Offset Word8 -> Word8) -> Offset Word8 -> (Char, Offset Word8)
nextWithIndexer Offset (Unit EncoderUTF8) -> Unit EncoderUTF8
ofs
    encodingWrite :: forall (st :: * -> *) err.
(PrimMonad st, Monad st) =>
EncoderUTF8
-> Char
-> Builder
     (UArray (Unit EncoderUTF8))
     (MUArray (Unit EncoderUTF8))
     (Unit EncoderUTF8)
     st
     err
     ()
encodingWrite EncoderUTF8
_ = forall (st :: * -> *) err.
(PrimMonad st, Monad st) =>
Char -> Builder (UArray Word8) (MUArray Word8) Word8 st err ()
writeWithBuilder

-- | Validate a bytearray for UTF8'ness
--
-- On success Nothing is returned
-- On Failure the position along with the failure reason
validate :: UArray Word8
         -> Offset8
         -> CountOf Word8
         -> (Offset8, Maybe ValidationFailure)
validate :: UArray Word8
-> Offset Word8
-> CountOf Word8
-> (Offset Word8, Maybe ValidationFailure)
validate UArray Word8
array Offset Word8
ofsStart CountOf Word8
sz = forall ty a s.
(Block ty -> Offset ty -> a)
-> (Ptr ty -> Offset ty -> ST s a) -> UArray ty -> a
C.unsafeDewrap Block Word8
-> Offset Word8 -> (Offset Word8, Maybe ValidationFailure)
goBa Ptr Word8
-> Offset Word8 -> ST Any (Offset Word8, Maybe ValidationFailure)
goAddr UArray Word8
array
  where
    unTranslateOffset :: Offset a -> p (Offset a) c -> p (Offset a) c
unTranslateOffset Offset a
start = forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (\Offset a
e -> Offset a
e forall a. Offset a -> Offset a -> Offset a
`offsetSub` Offset a
start)
    goBa :: Block Word8
-> Offset Word8 -> (Offset Word8, Maybe ValidationFailure)
goBa Block Word8
ba Offset Word8
start =
        forall {p :: * -> * -> *} {a} {c}.
Bifunctor p =>
Offset a -> p (Offset a) c -> p (Offset a) c
unTranslateOffset Offset Word8
start forall a b. (a -> b) -> a -> b
$ forall container.
Indexable container Word8 =>
Offset Word8
-> container
-> Offset Word8
-> (Offset Word8, Maybe ValidationFailure)
Alg.validate (Offset Word8
startforall a. Additive a => a -> a -> a
+Offset Word8
end) Block Word8
ba (Offset Word8
start forall a. Additive a => a -> a -> a
+ Offset Word8
ofsStart)
    goAddr :: Ptr Word8
-> Offset Word8 -> ST Any (Offset Word8, Maybe ValidationFailure)
goAddr ptr :: Ptr Word8
ptr@(Ptr !Addr#
_) Offset Word8
start =
        forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall {p :: * -> * -> *} {a} {c}.
Bifunctor p =>
Offset a -> p (Offset a) c -> p (Offset a) c
unTranslateOffset Offset Word8
start forall a b. (a -> b) -> a -> b
$ forall container.
Indexable container Word8 =>
Offset Word8
-> container
-> Offset Word8
-> (Offset Word8, Maybe ValidationFailure)
Alg.validate (Offset Word8
startforall a. Additive a => a -> a -> a
+Offset Word8
end) Ptr Word8
ptr (Offset Word8
ofsStart forall a. Additive a => a -> a -> a
+ Offset Word8
start)
    end :: Offset Word8
end = Offset Word8
ofsStart forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz

-- | Similar to 'validate' but works on a 'MutableByteArray'
mutableValidate :: PrimMonad prim
                => MVec.MUArray Word8 (PrimState prim)
                -> Offset Word8
                -> CountOf Word8
                -> prim (Offset Word8, Maybe ValidationFailure)
mutableValidate :: forall (prim :: * -> *).
PrimMonad prim =>
MUArray Word8 (PrimState prim)
-> Offset Word8
-> CountOf Word8
-> prim (Offset Word8, Maybe ValidationFailure)
mutableValidate MUArray Word8 (PrimState prim)
mba Offset Word8
ofsStart CountOf Word8
sz = do
    Offset Word8 -> prim (Offset Word8, Maybe ValidationFailure)
loop Offset Word8
ofsStart
  where
    end :: Offset Word8
end = Offset Word8
ofsStart forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz

    loop :: Offset Word8 -> prim (Offset Word8, Maybe ValidationFailure)
loop Offset Word8
ofs
        | Offset Word8
ofs forall a. Ord a => a -> a -> Bool
> Offset Word8
end  = forall a. HasCallStack => [Char] -> a
error [Char]
"mutableValidate: internal error: went pass offset"
        | Offset Word8
ofs forall a. Eq a => a -> a -> Bool
== Offset Word8
end = forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
end, forall a. Maybe a
Nothing)
        | Bool
otherwise  = do
            (Offset Word8, Maybe ValidationFailure)
r <- Offset Word8 -> prim (Offset Word8, Maybe ValidationFailure)
one Offset Word8
ofs
            case (Offset Word8, Maybe ValidationFailure)
r of
                (Offset Word8
nextOfs, Maybe ValidationFailure
Nothing)  -> Offset Word8 -> prim (Offset Word8, Maybe ValidationFailure)
loop Offset Word8
nextOfs
                (Offset Word8
pos, Just ValidationFailure
failure) -> forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos, forall a. a -> Maybe a
Just ValidationFailure
failure)

    one :: Offset Word8 -> prim (Offset Word8, Maybe ValidationFailure)
one Offset Word8
pos = do
        StepASCII
h <- Word8 -> StepASCII
StepASCII forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> prim ty
Vec.unsafeRead MUArray Word8 (PrimState prim)
mba Offset Word8
pos
        let nbConts :: Int
nbConts = StepASCII -> Int
getNbBytes StepASCII
h
        if Int
nbConts forall a. Eq a => a -> a -> Bool
== Int
0xff
            then forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos, forall a. a -> Maybe a
Just ValidationFailure
InvalidHeader)
            else if Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
1 forall a. Additive a => a -> a -> a
+ forall ty. Int -> Offset ty
Offset Int
nbConts forall a. Ord a => a -> a -> Bool
> Offset Word8
end
                then forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos, forall a. a -> Maybe a
Just ValidationFailure
MissingByte)
                else do
                    case Int
nbConts of
                        Int
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
1, forall a. Maybe a
Nothing)
                        Int
1 -> do
                            Word8
c1 <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> prim ty
Vec.unsafeRead MUArray Word8 (PrimState prim)
mba (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
1)
                            if Word8 -> Bool
isContinuation Word8
c1
                                then forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
2, forall a. Maybe a
Nothing)
                                else forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos, forall a. a -> Maybe a
Just ValidationFailure
InvalidContinuation)
                        Int
2 -> do
                            Word8
c1 <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> prim ty
Vec.unsafeRead MUArray Word8 (PrimState prim)
mba (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
1)
                            Word8
c2 <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> prim ty
Vec.unsafeRead MUArray Word8 (PrimState prim)
mba (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
2)
                            if Word8 -> Bool
isContinuation Word8
c1 Bool -> Bool -> Bool
&& Word8 -> Bool
isContinuation Word8
c2
                                then forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
3, forall a. Maybe a
Nothing)
                                else forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos, forall a. a -> Maybe a
Just ValidationFailure
InvalidContinuation)
                        Int
3 -> do
                            Word8
c1 <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> prim ty
Vec.unsafeRead MUArray Word8 (PrimState prim)
mba (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
1)
                            Word8
c2 <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> prim ty
Vec.unsafeRead MUArray Word8 (PrimState prim)
mba (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
2)
                            Word8
c3 <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> prim ty
Vec.unsafeRead MUArray Word8 (PrimState prim)
mba (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
3)
                            if Word8 -> Bool
isContinuation Word8
c1 Bool -> Bool -> Bool
&& Word8 -> Bool
isContinuation Word8
c2 Bool -> Bool -> Bool
&& Word8 -> Bool
isContinuation Word8
c3
                                then forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos forall a. Additive a => a -> a -> a
+ Offset Word8
4, forall a. Maybe a
Nothing)
                                else forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
pos, forall a. a -> Maybe a
Just ValidationFailure
InvalidContinuation)
                        Int
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"internal error"

nextWithIndexer :: (Offset Word8 -> Word8)
                -> Offset Word8
                -> (Char, Offset Word8)
nextWithIndexer :: (Offset Word8 -> Word8) -> Offset Word8 -> (Char, Offset Word8)
nextWithIndexer Offset Word8 -> Word8
getter Offset Word8
off =
    case Word8# -> Int#
getNbBytes# Word8#
b# of
        Int#
0# -> (Word# -> Char
toChar Word#
h, Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
1)
        Int#
1# -> (Word# -> Char
toChar (Word8 -> Word#
decode2 (Offset Word8 -> Word8
getter forall a b. (a -> b) -> a -> b
$ Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
1)), Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
2)
        Int#
2# -> (Word# -> Char
toChar (Word8 -> Word8 -> Word#
decode3 (Offset Word8 -> Word8
getter forall a b. (a -> b) -> a -> b
$ Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
1) (Offset Word8 -> Word8
getter forall a b. (a -> b) -> a -> b
$ Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
2)), Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
3)
        Int#
3# -> (Word# -> Char
toChar (Word8 -> Word8 -> Word8 -> Word#
decode4 (Offset Word8 -> Word8
getter forall a b. (a -> b) -> a -> b
$ Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
1) (Offset Word8 -> Word8
getter forall a b. (a -> b) -> a -> b
$ Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
2) (Offset Word8 -> Word8
getter forall a b. (a -> b) -> a -> b
$ Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
3))
              , Offset Word8
off forall a. Additive a => a -> a -> a
+ Offset Word8
4)
        Int#
r -> forall a. HasCallStack => [Char] -> a
error ([Char]
"next: internal error: invalid input: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show (Int# -> Int
I# Int#
r) forall a. Semigroup a => a -> a -> a
<> [Char]
" " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show (Word# -> Word
W# Word#
h))
  where
    b :: Word8
b@(W8# Word8#
b#) = Offset Word8 -> Word8
getter Offset Word8
off
    !(W# Word#
h) = forall a b. IntegralUpsize a b => a -> b
integralUpsize Word8
b

    toChar :: Word# -> Char
    toChar :: Word# -> Char
toChar Word#
w = Char# -> Char
C# (Int# -> Char#
chr# (Word# -> Int#
word2Int# Word#
w))

    decode2 :: Word8 -> Word#
    decode2 :: Word8 -> Word#
decode2 (W8# Word8#
b1) =
        Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word# -> Word#
and# Word#
h Word#
0x1f##) Int#
6#)
            (Word# -> Word# -> Word#
and# Word#
c1 Word#
0x3f##)
      where
        c1 :: Word#
c1 = Word8# -> Word#
word8ToWord# Word8#
b1

    decode3 :: Word8 -> Word8 -> Word#
    decode3 :: Word8 -> Word8 -> Word#
decode3 (W8# Word8#
b1) (W8# Word8#
b2) =
        Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word# -> Word#
and# Word#
h Word#
0xf##) Int#
12#)
            (Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word# -> Word#
and# Word#
c1 Word#
0x3f##) Int#
6#)
                 (Word# -> Word# -> Word#
and# Word#
c2 Word#
0x3f##))
      where
        c1 :: Word#
c1 = Word8# -> Word#
word8ToWord# Word8#
b1
        c2 :: Word#
c2 = Word8# -> Word#
word8ToWord# Word8#
b2

    decode4 :: Word8 -> Word8 -> Word8 -> Word#
    decode4 :: Word8 -> Word8 -> Word8 -> Word#
decode4 (W8# Word8#
b1) (W8# Word8#
b2) (W8# Word8#
b3) =
        Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word# -> Word#
and# Word#
h Word#
0x7##) Int#
18#)
            (Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word# -> Word#
and# Word#
c1 Word#
0x3f##) Int#
12#)
                (Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftL# (Word# -> Word# -> Word#
and# Word#
c2 Word#
0x3f##) Int#
6#)
                    (Word# -> Word# -> Word#
and# Word#
c3 Word#
0x3f##))
            )
      where
        c1 :: Word#
c1 = Word8# -> Word#
word8ToWord# Word8#
b1
        c2 :: Word#
c2 = Word8# -> Word#
word8ToWord# Word8#
b2
        c3 :: Word#
c3 = Word8# -> Word#
word8ToWord# Word8#
b3

writeWithBuilder :: (PrimMonad st, Monad st)
                 => Char
                 -> Builder (UArray Word8) (MVec.MUArray Word8) Word8 st err ()
writeWithBuilder :: forall (st :: * -> *) err.
(PrimMonad st, Monad st) =>
Char -> Builder (UArray Word8) (MUArray Word8) Word8 st err ()
writeWithBuilder Char
c
    | Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x80##   ) = Builder (UArray Word8) (MUArray Word8) Word8 st err ()
encode1
    | Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x800##  ) = Builder (UArray Word8) (MUArray Word8) Word8 st err ()
encode2
    | Int# -> Bool
bool# (Word# -> Word# -> Int#
ltWord# Word#
x Word#
0x10000##) = Builder (UArray Word8) (MUArray Word8) Word8 st err ()
encode3
    | Bool
otherwise = Builder (UArray Word8) (MUArray Word8) Word8 st err ()
encode4
  where
    !(I# Int#
xi) = forall a. Enum a => a -> Int
fromEnum Char
c
    !x :: Word#
x       = Int# -> Word#
int2Word# Int#
xi

    encode1 :: Builder (UArray Word8) (MUArray Word8) Word8 st err ()
encode1 = forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x))

    encode2 :: Builder (UArray Word8) (MUArray Word8) Word8 st err ()
encode2 = do
        let x1 :: Word#
x1  = Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
6#) Word#
0xc0##
            x2 :: Word#
x2  = Word# -> Word#
toContinuation Word#
x
        forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x1)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x2))

    encode3 :: Builder (UArray Word8) (MUArray Word8) Word8 st err ()
encode3 = do
        let x1 :: Word#
x1  = Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
12#) Word#
0xe0##
            x2 :: Word#
x2  = Word# -> Word#
toContinuation (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
6#)
            x3 :: Word#
x3  = Word# -> Word#
toContinuation Word#
x
        forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x1)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x2)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x3))

    encode4 :: Builder (UArray Word8) (MUArray Word8) Word8 st err ()
encode4 = do
        let x1 :: Word#
x1  = Word# -> Word# -> Word#
or# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
18#) Word#
0xf0##
            x2 :: Word#
x2  = Word# -> Word#
toContinuation (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
12#)
            x3 :: Word#
x3  = Word# -> Word#
toContinuation (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
x Int#
6#)
            x4 :: Word#
x4  = Word# -> Word#
toContinuation Word#
x
        forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x1)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x2)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x3)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall ty (state :: * -> *) err.
(PrimType ty, PrimMonad state) =>
ty -> Builder (UArray ty) (MUArray ty) ty state err ()
Vec.builderAppend (Word8# -> Word8
W8# (Word# -> Word8#
wordToWord8# Word#
x4))

    toContinuation :: Word# -> Word#
    toContinuation :: Word# -> Word#
toContinuation Word#
w = Word# -> Word# -> Word#
or# (Word# -> Word# -> Word#
and# Word#
w Word#
0x3f##) Word#
0x80##

writeUTF8Char :: PrimMonad prim => MutableString (PrimState prim) -> Offset8 -> UTF8Char -> prim ()
writeUTF8Char :: forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> UTF8Char -> prim ()
writeUTF8Char (MutableString MUArray Word8 (PrimState prim)
mba) Offset Word8
i (UTF8_1 Word8
x1) =
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba Offset Word8
i     Word8
x1
writeUTF8Char (MutableString MUArray Word8 (PrimState prim)
mba) Offset Word8
i (UTF8_2 Word8
x1 Word8
x2) = do
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba Offset Word8
i     Word8
x1
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba (Offset Word8
iforall a. Additive a => a -> a -> a
+Offset Word8
1) Word8
x2
writeUTF8Char (MutableString MUArray Word8 (PrimState prim)
mba) Offset Word8
i (UTF8_3 Word8
x1 Word8
x2 Word8
x3) = do
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba Offset Word8
i     Word8
x1
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba (Offset Word8
iforall a. Additive a => a -> a -> a
+Offset Word8
1) Word8
x2
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba (Offset Word8
iforall a. Additive a => a -> a -> a
+Offset Word8
2) Word8
x3
writeUTF8Char (MutableString MUArray Word8 (PrimState prim)
mba) Offset Word8
i (UTF8_4 Word8
x1 Word8
x2 Word8
x3 Word8
x4) = do
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba Offset Word8
i     Word8
x1
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba (Offset Word8
iforall a. Additive a => a -> a -> a
+Offset Word8
1) Word8
x2
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba (Offset Word8
iforall a. Additive a => a -> a -> a
+Offset Word8
2) Word8
x3
    forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim) -> Offset ty -> ty -> prim ()
Vec.unsafeWrite MUArray Word8 (PrimState prim)
mba (Offset Word8
iforall a. Additive a => a -> a -> a
+Offset Word8
3) Word8
x4
{-# INLINE writeUTF8Char #-}

unsafeFreezeShrink :: PrimMonad prim => MutableString (PrimState prim) -> CountOf Word8 -> prim String
unsafeFreezeShrink :: forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> CountOf Word8 -> prim String
unsafeFreezeShrink (MutableString MUArray Word8 (PrimState prim)
mba) CountOf Word8
s = UArray Word8 -> String
String forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall ty (prim :: * -> *).
(PrimType ty, PrimMonad prim) =>
MUArray ty (PrimState prim) -> CountOf ty -> prim (UArray ty)
Vec.unsafeFreezeShrink MUArray Word8 (PrimState prim)
mba CountOf Word8
s
{-# INLINE unsafeFreezeShrink #-}

------------------------------------------------------------------------
-- real functions

-- | Check if a String is null
null :: String -> Bool
null :: String -> Bool
null (String UArray Word8
ba) = forall ty. UArray ty -> CountOf ty
C.length UArray Word8
ba forall a. Eq a => a -> a -> Bool
== CountOf Word8
0

-- we don't know in constant time the count of character in string,
-- however if we estimate bounds of what N characters would
-- take in space (between N and N*4). If the count is thus bigger than
-- the number of bytes, then we know for sure that it's going to
-- be out of bounds
countCharMoreThanBytes :: CountOf Char -> UArray Word8 -> Bool
countCharMoreThanBytes :: CountOf Char -> UArray Word8 -> Bool
countCharMoreThanBytes (CountOf Int
chars) UArray Word8
ba = Int
chars forall a. Ord a => a -> a -> Bool
>= Int
bytes
  where (CountOf Int
bytes) = forall ty. UArray ty -> CountOf ty
C.length UArray Word8
ba

-- | Create a string composed of a number @n of Chars (Unicode code points).
--
-- if the input @s contains less characters than required, then the input string is returned.
take :: CountOf Char -> String -> String
take :: CountOf Char -> String -> String
take CountOf Char
n s :: String
s@(String UArray Word8
ba)
    | CountOf Char
n forall a. Ord a => a -> a -> Bool
<= CountOf Char
0                      = forall a. Monoid a => a
mempty
    | CountOf Char -> UArray Word8 -> Bool
countCharMoreThanBytes CountOf Char
n UArray Word8
ba = String
s
    | Bool
otherwise                   = UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty. CountOf ty -> UArray ty -> UArray ty
Vec.unsafeTake (forall a. Offset a -> CountOf a
offsetAsSize forall a b. (a -> b) -> a -> b
$ CountOf Char -> String -> Offset Word8
indexN CountOf Char
n String
s) UArray Word8
ba

-- | Create a string with the remaining Chars after dropping @n Chars from the beginning
drop :: CountOf Char -> String -> String
drop :: CountOf Char -> String -> String
drop CountOf Char
n s :: String
s@(String UArray Word8
ba)
    | CountOf Char
n forall a. Ord a => a -> a -> Bool
<= CountOf Char
0                      = String
s
    | CountOf Char -> UArray Word8 -> Bool
countCharMoreThanBytes CountOf Char
n UArray Word8
ba = forall a. Monoid a => a
mempty
    | Bool
otherwise                   = UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty. CountOf ty -> UArray ty -> UArray ty
Vec.drop (forall a. Offset a -> CountOf a
offsetAsSize forall a b. (a -> b) -> a -> b
$ CountOf Char -> String -> Offset Word8
indexN CountOf Char
n String
s) UArray Word8
ba

-- | Split a string at the Offset specified (in Char) returning both
-- the leading part and the remaining part.
splitAt :: CountOf Char -> String -> (String, String)
splitAt :: CountOf Char -> String -> (String, String)
splitAt CountOf Char
n s :: String
s@(String UArray Word8
ba)
    | CountOf Char
n forall a. Ord a => a -> a -> Bool
<= CountOf Char
0                      = (forall a. Monoid a => a
mempty, String
s)
    | CountOf Char -> UArray Word8 -> Bool
countCharMoreThanBytes CountOf Char
n UArray Word8
ba = (String
s, forall a. Monoid a => a
mempty)
    | Bool
otherwise                   =
        let (UArray Word8
v1,UArray Word8
v2) = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt (forall a. Offset a -> CountOf a
offsetAsSize forall a b. (a -> b) -> a -> b
$ CountOf Char -> String -> Offset Word8
indexN CountOf Char
n String
s) UArray Word8
ba
         in (UArray Word8 -> String
String UArray Word8
v1, UArray Word8 -> String
String UArray Word8
v2)

-- | Return the offset (in bytes) of the N'th sequence in an UTF8 String
indexN :: CountOf Char -> String -> Offset Word8
indexN :: CountOf Char -> String -> Offset Word8
indexN !CountOf Char
n (String UArray Word8
ba) = forall ty a s.
(Block ty -> Offset ty -> a)
-> (Ptr ty -> Offset ty -> ST s a) -> UArray ty -> a
Vec.unsafeDewrap Block Word8 -> Offset Word8 -> Offset Word8
goVec forall s. Ptr Word8 -> Offset Word8 -> ST s (Offset Word8)
goAddr UArray Word8
ba
  where
    goVec :: Block Word8 -> Offset Word8 -> Offset Word8
    goVec :: Block Word8 -> Offset Word8 -> Offset Word8
goVec (Block !ByteArray#
ma) !Offset Word8
start = Offset Word8 -> Offset Char -> Offset Word8
loop Offset Word8
start Offset Char
0
      where
        !len :: Offset Word8
len = Offset Word8
start forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` forall ty. UArray ty -> CountOf ty
Vec.length UArray Word8
ba
        loop :: Offset Word8 -> Offset Char -> Offset Word8
        loop :: Offset Word8 -> Offset Char -> Offset Word8
loop !Offset Word8
idx !Offset Char
i
            | Offset Word8
idx forall a. Ord a => a -> a -> Bool
>= Offset Word8
len Bool -> Bool -> Bool
|| Offset Char
i forall ty. Offset ty -> CountOf ty -> Bool
.==# CountOf Char
n = forall a. CountOf a -> Offset a
sizeAsOffset (Offset Word8
idx forall a. Subtractive a => a -> a -> Difference a
- Offset Word8
start)
            | Bool
otherwise              = Offset Word8 -> Offset Char -> Offset Word8
loop (Offset Word8
idx forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
d) (Offset Char
i forall a. Additive a => a -> a -> a
+ forall ty. Int -> Offset ty
Offset Int
1)
          where d :: CountOf Word8
d = Word8 -> CountOf Word8
skipNextHeaderValue (forall ty. PrimType ty => ByteArray# -> Offset ty -> ty
primBaIndex ByteArray#
ma Offset Word8
idx)
    {-# INLINE goVec #-}

    goAddr :: Ptr Word8 -> Offset Word8 -> ST s (Offset Word8)
    goAddr :: forall s. Ptr Word8 -> Offset Word8 -> ST s (Offset Word8)
goAddr (Ptr Addr#
ptr) !Offset Word8
start = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Offset Word8 -> Offset Char -> Offset Word8
loop Offset Word8
start (forall ty. Int -> Offset ty
Offset Int
0)
      where
        !len :: Offset Word8
len = Offset Word8
start forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` forall ty. UArray ty -> CountOf ty
Vec.length UArray Word8
ba
        loop :: Offset Word8 -> Offset Char -> Offset Word8
        loop :: Offset Word8 -> Offset Char -> Offset Word8
loop !Offset Word8
idx !Offset Char
i
            | Offset Word8
idx forall a. Ord a => a -> a -> Bool
>= Offset Word8
len Bool -> Bool -> Bool
|| Offset Char
i forall ty. Offset ty -> CountOf ty -> Bool
.==# CountOf Char
n = forall a. CountOf a -> Offset a
sizeAsOffset (Offset Word8
idx forall a. Subtractive a => a -> a -> Difference a
- Offset Word8
start)
            | Bool
otherwise              = Offset Word8 -> Offset Char -> Offset Word8
loop (Offset Word8
idx forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
d) (Offset Char
i forall a. Additive a => a -> a -> a
+ forall ty. Int -> Offset ty
Offset Int
1)
          where d :: CountOf Word8
d = Word8 -> CountOf Word8
skipNextHeaderValue (forall ty. PrimType ty => Addr# -> Offset ty -> ty
primAddrIndex Addr#
ptr Offset Word8
idx)
    {-# INLINE goAddr #-}
{-# INLINE indexN #-}

-- inverse a CountOf that is specified from the end (e.g. take n Chars from the end)
--
-- rev{Take,Drop,SplitAt} TODO optimise:
-- we can process the string from the end using a skipPrev instead of getting the length
countFromStart :: String -> CountOf Char -> CountOf Char
countFromStart :: String -> CountOf Char -> CountOf Char
countFromStart String
s sz :: CountOf Char
sz@(CountOf Int
sz')
    | CountOf Char
sz forall a. Ord a => a -> a -> Bool
>= CountOf Char
len = forall ty. Int -> CountOf ty
CountOf Int
0
    | Bool
otherwise = forall ty. Int -> CountOf ty
CountOf (Int
len' forall a. Subtractive a => a -> a -> Difference a
- Int
sz')
  where len :: CountOf Char
len@(CountOf Int
len') = String -> CountOf Char
length String
s

-- | Similar to 'take' but from the end
revTake :: CountOf Char -> String -> String
revTake :: CountOf Char -> String -> String
revTake CountOf Char
n String
v = CountOf Char -> String -> String
drop (String -> CountOf Char -> CountOf Char
countFromStart String
v CountOf Char
n) String
v

-- | Similar to 'drop' but from the end
revDrop :: CountOf Char -> String -> String
revDrop :: CountOf Char -> String -> String
revDrop CountOf Char
n String
v = CountOf Char -> String -> String
take (String -> CountOf Char -> CountOf Char
countFromStart String
v CountOf Char
n) String
v

-- | Similar to 'splitAt' but from the end
revSplitAt :: CountOf Char -> String -> (String, String)
revSplitAt :: CountOf Char -> String -> (String, String)
revSplitAt CountOf Char
n String
v = (CountOf Char -> String -> String
drop CountOf Char
idx String
v, CountOf Char -> String -> String
take CountOf Char
idx String
v) where idx :: CountOf Char
idx = String -> CountOf Char -> CountOf Char
countFromStart String
v CountOf Char
n

-- | Split on the input string using the predicate as separator
--
-- e.g.
--
-- > splitOn (== ',') ","          == ["",""]
-- > splitOn (== ',') ",abc,"      == ["","abc",""]
-- > splitOn (== ':') "abc"        == ["abc"]
-- > splitOn (== ':') "abc::def"   == ["abc","","def"]
-- > splitOn (== ':') "::abc::def" == ["","","abc","","def"]
--
splitOn :: (Char -> Bool) -> String -> [String]
splitOn :: (Char -> Bool) -> String -> [String]
splitOn Char -> Bool
predicate String
s
    | CountOf Word8
sz forall a. Eq a => a -> a -> Bool
== forall ty. Int -> CountOf ty
CountOf Int
0 = [forall a. Monoid a => a
mempty]
    | Bool
otherwise    = Offset Word8 -> Offset Word8 -> [String]
loop forall a. Additive a => a
azero forall a. Additive a => a
azero
  where
    !sz :: CountOf Word8
sz = String -> CountOf Word8
size String
s
    end :: Offset Word8
end = forall a. Additive a => a
azero forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz
    loop :: Offset Word8 -> Offset Word8 -> [String]
loop Offset Word8
prevIdx Offset Word8
idx
        | Offset Word8
idx forall a. Eq a => a -> a -> Bool
== Offset Word8
end = [String -> Offset Word8 -> Offset Word8 -> String
sub String
s Offset Word8
prevIdx Offset Word8
idx]
        | Bool
otherwise =
            let !(Step Char
c Offset Word8
idx') = String -> Offset Word8 -> Step
next String
s Offset Word8
idx
             in if Char -> Bool
predicate Char
c
                    then String -> Offset Word8 -> Offset Word8 -> String
sub String
s Offset Word8
prevIdx Offset Word8
idx forall a. a -> [a] -> [a]
: Offset Word8 -> Offset Word8 -> [String]
loop Offset Word8
idx' Offset Word8
idx'
                    else Offset Word8 -> Offset Word8 -> [String]
loop Offset Word8
prevIdx Offset Word8
idx'

-- | Internal call to make a substring given offset in bytes.
--
-- This is unsafe considering that one can create a substring
-- starting and/or ending on the middle of a UTF8 sequence.
sub :: String -> Offset8 -> Offset8 -> String
sub :: String -> Offset Word8 -> Offset Word8 -> String
sub (String UArray Word8
ba) Offset Word8
start Offset Word8
end = UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty.
PrimType ty =>
UArray ty -> Offset ty -> Offset ty -> UArray ty
Vec.sub UArray Word8
ba Offset Word8
start Offset Word8
end

-- | Internal call to split at a given index in offset of bytes.
--
-- This is unsafe considering that one can split in the middle of a
-- UTF8 sequence, so use with care.
splitIndex :: Offset8 -> String -> (String, String)
splitIndex :: Offset Word8 -> String -> (String, String)
splitIndex Offset Word8
idx (String UArray Word8
ba) = (UArray Word8 -> String
String UArray Word8
v1, UArray Word8 -> String
String UArray Word8
v2)
  where (UArray Word8
v1,UArray Word8
v2) = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
idx) UArray Word8
ba

-- | Break a string into 2 strings at the location where the predicate return True
break :: (Char -> Bool) -> String -> (String, String)
break :: (Char -> Bool) -> String -> (String, String)
break Char -> Bool
predicate s :: String
s@(String UArray Word8
ba) = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall (prim :: * -> *) ty a.
(PrimMonad prim, PrimType ty) =>
UArray ty -> ((Offset ty -> ty) -> prim a) -> prim a
Vec.unsafeIndexer UArray Word8
ba forall st. (Offset Word8 -> Word8) -> ST st (String, String)
go
  where
    !sz :: CountOf Word8
sz = String -> CountOf Word8
size String
s
    end :: Offset Word8
end = forall a. Additive a => a
azero forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz

    go :: (Offset Word8 -> Word8) -> ST st (String, String)
    go :: forall st. (Offset Word8 -> Word8) -> ST st (String, String)
go Offset Word8 -> Word8
getIdx = Offset Word8 -> ST st (String, String)
loop (forall ty. Int -> Offset ty
Offset Int
0)
      where
        !nextI :: Offset Word8 -> (Char, Offset Word8)
nextI = (Offset Word8 -> Word8) -> Offset Word8 -> (Char, Offset Word8)
nextWithIndexer Offset Word8 -> Word8
getIdx
        loop :: Offset Word8 -> ST st (String, String)
loop Offset Word8
idx
            | Offset Word8
idx forall a. Eq a => a -> a -> Bool
== Offset Word8
end = forall (m :: * -> *) a. Monad m => a -> m a
return (String
s, forall a. Monoid a => a
mempty)
            | Bool
otherwise  = do
                let (Char
c, Offset Word8
idx') = Offset Word8 -> (Char, Offset Word8)
nextI Offset Word8
idx
                case Char -> Bool
predicate Char
c of
                    Bool
True  -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Offset Word8 -> String -> (String, String)
splitIndex Offset Word8
idx String
s
                    Bool
False -> Offset Word8 -> ST st (String, String)
loop Offset Word8
idx'
        {-# INLINE loop #-}
{-# INLINE [2] break #-}

breakEnd :: (Char -> Bool) -> String -> (String, String)
breakEnd :: (Char -> Bool) -> String -> (String, String)
breakEnd Char -> Bool
predicate s :: String
s@(String UArray Word8
arr)
    | Offset Word8
k forall a. Eq a => a -> a -> Bool
== Offset Word8
end  = (String
s, forall a. Monoid a => a
mempty)
    | Bool
otherwise = Offset Word8 -> String -> (String, String)
splitIndex (Offset Word8
k forall a. Offset a -> Offset a -> Offset a
`offsetSub` Offset Word8
start) String
s
  where
    k :: Offset Word8
k = forall ty a s.
(Block ty -> a)
-> (FinalPtr ty -> Ptr ty -> ST s a) -> UArray ty -> a
C.onBackend Block Word8 -> Offset Word8
goVec (\FinalPtr Word8
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Ptr Word8 -> Offset Word8
goAddr) UArray Word8
arr
    (C.ValidRange !Offset Word8
start !Offset Word8
end) = forall ty. UArray ty -> ValidRange ty
offsetsValidRange UArray Word8
arr
    goVec :: Block Word8 -> Offset Word8
goVec ba :: Block Word8
ba@(Block !ByteArray#
_) = let k :: Offset Word8
k = forall container.
Indexable container Word8 =>
(Char -> Bool)
-> container -> Offset Word8 -> Offset Word8 -> Offset Word8
Alg.revFindIndexPredicate Char -> Bool
predicate Block Word8
ba Offset Word8
start Offset Word8
end
                        in if Offset Word8
k forall a. Eq a => a -> a -> Bool
== Offset Word8
end then Offset Word8
end else forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Offset Word8
UTF8.nextSkip Block Word8
ba Offset Word8
k
    goAddr :: Ptr Word8 -> Offset Word8
goAddr ptr :: Ptr Word8
ptr@(Ptr !Addr#
_) =
        let k :: Offset Word8
k = forall container.
Indexable container Word8 =>
(Char -> Bool)
-> container -> Offset Word8 -> Offset Word8 -> Offset Word8
Alg.revFindIndexPredicate Char -> Bool
predicate Ptr Word8
ptr Offset Word8
start Offset Word8
end
         in if Offset Word8
k forall a. Eq a => a -> a -> Bool
== Offset Word8
end then Offset Word8
end else forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Offset Word8
UTF8.nextSkip Ptr Word8
ptr Offset Word8
k
{-# INLINE [2] breakEnd #-}

#if MIN_VERSION_base(4,9,0)
{-# RULES "break (== 'c')" [3] forall c . break (eqChar c) = breakElem c #-}
#else
{-# RULES "break (== 'c')" [3] forall c . break (== c) = breakElem c #-}
#endif

-- | Break a string into 2 strings at the first occurence of the character
breakElem :: Char -> String -> (String, String)
breakElem :: Char -> String -> (String, String)
breakElem !Char
el s :: String
s@(String UArray Word8
ba)
    | CountOf Word8
sz forall a. Eq a => a -> a -> Bool
== CountOf Word8
0   = (forall a. Monoid a => a
mempty, forall a. Monoid a => a
mempty)
    | Bool
otherwise =
        case Char -> UTF8Char
asUTF8Char Char
el of
            UTF8_1 Word8
w -> let !(UArray Word8
v1,UArray Word8
v2) = forall ty. PrimType ty => ty -> UArray ty -> (UArray ty, UArray ty)
Vec.breakElem Word8
w UArray Word8
ba in (UArray Word8 -> String
String UArray Word8
v1, UArray Word8 -> String
String UArray Word8
v2)
            UTF8Char
_        -> forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall (prim :: * -> *) ty a.
(PrimMonad prim, PrimType ty) =>
UArray ty -> ((Offset ty -> ty) -> prim a) -> prim a
Vec.unsafeIndexer UArray Word8
ba forall st. (Offset Word8 -> Word8) -> ST st (String, String)
go
  where
    sz :: CountOf Word8
sz = String -> CountOf Word8
size String
s
    end :: Offset Word8
end = forall a. Additive a => a
azero forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz

    go :: (Offset Word8 -> Word8) -> ST st (String, String)
    go :: forall st. (Offset Word8 -> Word8) -> ST st (String, String)
go Offset Word8 -> Word8
getIdx = Offset Word8 -> ST st (String, String)
loop (forall ty. Int -> Offset ty
Offset Int
0)
      where
        !nextI :: Offset Word8 -> (Char, Offset Word8)
nextI = (Offset Word8 -> Word8) -> Offset Word8 -> (Char, Offset Word8)
nextWithIndexer Offset Word8 -> Word8
getIdx
        loop :: Offset Word8 -> ST st (String, String)
loop Offset Word8
idx
            | Offset Word8
idx forall a. Eq a => a -> a -> Bool
== Offset Word8
end = forall (m :: * -> *) a. Monad m => a -> m a
return (String
s, forall a. Monoid a => a
mempty)
            | Bool
otherwise  = do
                let (Char
c, Offset Word8
idx') = Offset Word8 -> (Char, Offset Word8)
nextI Offset Word8
idx
                case Char
el forall a. Eq a => a -> a -> Bool
== Char
c of
                    Bool
True  -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Offset Word8 -> String -> (String, String)
splitIndex Offset Word8
idx String
s
                    Bool
False -> Offset Word8 -> ST st (String, String)
loop Offset Word8
idx'

-- | Same as break but cut on a line feed with an optional carriage return.
--
-- This is the same operation as 'breakElem LF' dropping the last character of the
-- string if it's a CR.
--
-- Also for efficiency reason (streaming), it returns if the last character was a CR character.
breakLine :: String -> Either Bool (String, String)
breakLine :: String -> Either Bool (String, String)
breakLine (String UArray Word8
arr) = forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap UArray Word8 -> String
String UArray Word8 -> String
String forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UArray Word8 -> Either Bool (UArray Word8, UArray Word8)
Vec.breakLine UArray Word8
arr

-- | Apply a @predicate@ to the string to return the longest prefix that satisfy the predicate and
-- the remaining
span :: (Char -> Bool) -> String -> (String, String)
span :: (Char -> Bool) -> String -> (String, String)
span Char -> Bool
predicate String
s = (Char -> Bool) -> String -> (String, String)
break (Bool -> Bool
not forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Char -> Bool
predicate) String
s

-- | Apply a @predicate@ to the string to return the longest suffix that satisfy the predicate and
-- the remaining
spanEnd :: (Char -> Bool) -> String -> (String, String)
spanEnd :: (Char -> Bool) -> String -> (String, String)
spanEnd Char -> Bool
predicate String
s = (Char -> Bool) -> String -> (String, String)
breakEnd (Bool -> Bool
not forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Char -> Bool
predicate) String
s

-- | Drop character from the beginning while the predicate is true
dropWhile :: (Char -> Bool) -> String -> String
dropWhile :: (Char -> Bool) -> String -> String
dropWhile Char -> Bool
predicate = forall a b. (a, b) -> b
snd forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Char -> Bool) -> String -> (String, String)
break (Bool -> Bool
not forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Char -> Bool
predicate)

-- | Return whereas the string contains a specific character or not
elem :: Char -> String -> Bool
elem :: Char -> String -> Bool
elem !Char
el s :: String
s@(String UArray Word8
ba) =
    case Char -> UTF8Char
asUTF8Char Char
el of
        UTF8_1 Word8
w -> forall ty. PrimType ty => ty -> UArray ty -> Bool
Vec.elem Word8
w UArray Word8
ba
        UTF8Char
_        -> forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall (prim :: * -> *) ty a.
(PrimMonad prim, PrimType ty) =>
UArray ty -> ((Offset ty -> ty) -> prim a) -> prim a
Vec.unsafeIndexer UArray Word8
ba forall st. (Offset Word8 -> Word8) -> ST st Bool
go
  where
    sz :: CountOf Word8
sz = String -> CountOf Word8
size String
s
    end :: Offset Word8
end = forall a. Additive a => a
azero forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz

    go :: (Offset Word8 -> Word8) -> ST st Bool
    go :: forall st. (Offset Word8 -> Word8) -> ST st Bool
go Offset Word8 -> Word8
getIdx = Offset Word8 -> ST st Bool
loop (forall ty. Int -> Offset ty
Offset Int
0)
      where
        !nextI :: Offset Word8 -> (Char, Offset Word8)
nextI = (Offset Word8 -> Word8) -> Offset Word8 -> (Char, Offset Word8)
nextWithIndexer Offset Word8 -> Word8
getIdx
        loop :: Offset Word8 -> ST st Bool
loop !Offset Word8
idx
            | Offset Word8
idx forall a. Eq a => a -> a -> Bool
== Offset Word8
end = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            | Bool
otherwise  = do
                let (Char
c, Offset Word8
idx') = Offset Word8 -> (Char, Offset Word8)
nextI Offset Word8
idx
                case Char
el forall a. Eq a => a -> a -> Bool
== Char
c of
                    Bool
True  -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                    Bool
False -> Offset Word8 -> ST st Bool
loop Offset Word8
idx'

-- | Intersperse the character @sep@ between each character in the string
--
-- > intersperse ' ' "Hello Foundation"
-- "H e l l o   F o u n d a t i o n"
intersperse :: Char -> String -> String
intersperse :: Char -> String -> String
intersperse Char
sep String
src = case String -> CountOf Char
length String
src forall a. Subtractive a => a -> a -> Difference a
- CountOf Char
1 of
    Maybe (CountOf Char)
Difference (CountOf Char)
Nothing   -> String
src
    Just CountOf Char
0    -> String
src
    Just CountOf Char
gaps -> forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall s.
String
-> CountOf Word8
-> (String
    -> Offset Char
    -> Offset Word8
    -> MutableString s
    -> Offset Word8
    -> ST s (Offset Word8, Offset Word8))
-> ST s String
unsafeCopyFrom String
src CountOf Word8
dstBytes forall s.
String
-> Offset Char
-> Offset Word8
-> MutableString s
-> Offset Word8
-> ST s (Offset Word8, Offset Word8)
go
        where
          lastSrcI :: Offset Char
          lastSrcI :: Offset Char
lastSrcI = Offset Char
0 forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Char
gaps
          dstBytes :: CountOf Word8
dstBytes = (String -> CountOf Word8
size String
src :: CountOf Word8) forall a. Additive a => a -> a -> a
+ (CountOf Char
gaps forall a n. (Additive a, IsNatural n) => n -> a -> a
`scale` Int -> CountOf Word8
charToBytes (forall a. Enum a => a -> Int
fromEnum Char
sep))

          go :: String -> Offset Char -> Offset8 -> MutableString s -> Offset8 -> ST s (Offset8, Offset8)
          go :: forall s.
String
-> Offset Char
-> Offset Word8
-> MutableString s
-> Offset Word8
-> ST s (Offset Word8, Offset Word8)
go String
src' Offset Char
srcI Offset Word8
srcIdx MutableString s
dst Offset Word8
dstIdx
              | Offset Char
srcI forall a. Eq a => a -> a -> Bool
== Offset Char
lastSrcI = do
                  Offset Word8
nextDstIdx <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> Char -> prim (Offset Word8)
write MutableString s
dst Offset Word8
dstIdx Char
c
                  forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
nextSrcIdx, Offset Word8
nextDstIdx)
              | Bool
otherwise        = do
                  Offset Word8
nextDstIdx  <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> Char -> prim (Offset Word8)
write MutableString s
dst Offset Word8
dstIdx Char
c
                  Offset Word8
nextDstIdx' <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> Char -> prim (Offset Word8)
write MutableString s
dst Offset Word8
nextDstIdx Char
sep
                  forall (m :: * -> *) a. Monad m => a -> m a
return (Offset Word8
nextSrcIdx, Offset Word8
nextDstIdx')
            where
              !(Step Char
c Offset Word8
nextSrcIdx) = String -> Offset Word8 -> Step
next String
src' Offset Word8
srcIdx

-- | Allocate a new @String@ with a fill function that has access to the characters of
--   the source @String@.
unsafeCopyFrom :: String -- ^ Source string
               -> CountOf Word8  -- ^ Length of the destination string in bytes
               -> (String -> Offset Char -> Offset8 -> MutableString s -> Offset8 -> ST s (Offset8, Offset8))
               -- ^ Function called for each character in the source String
               -> ST s String -- ^ Returns the filled new string
unsafeCopyFrom :: forall s.
String
-> CountOf Word8
-> (String
    -> Offset Char
    -> Offset Word8
    -> MutableString s
    -> Offset Word8
    -> ST s (Offset Word8, Offset Word8))
-> ST s String
unsafeCopyFrom String
src CountOf Word8
dstBytes String
-> Offset Char
-> Offset Word8
-> MutableString s
-> Offset Word8
-> ST s (Offset Word8, Offset Word8)
f = forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new CountOf Word8
dstBytes forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Offset Char
-> Offset Word8
-> Offset Word8
-> (String
    -> Offset Char
    -> Offset Word8
    -> MutableString s
    -> Offset Word8
    -> ST s (Offset Word8, Offset Word8))
-> MutableString s
-> ST s (MutableString s)
fill (forall ty. Int -> Offset ty
Offset Int
0) (forall ty. Int -> Offset ty
Offset Int
0) (forall ty. Int -> Offset ty
Offset Int
0) String
-> Offset Char
-> Offset Word8
-> MutableString s
-> Offset Word8
-> ST s (Offset Word8, Offset Word8)
f forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze
  where
    srcLen :: CountOf Char
srcLen = String -> CountOf Char
length String
src
    end :: Offset Char
end = forall ty. Int -> Offset ty
Offset Int
0 forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Char
srcLen
    fill :: Offset Char
-> Offset Word8
-> Offset Word8
-> (String
    -> Offset Char
    -> Offset Word8
    -> MutableString s
    -> Offset Word8
    -> ST s (Offset Word8, Offset Word8))
-> MutableString s
-> ST s (MutableString s)
fill Offset Char
srcI Offset Word8
srcIdx Offset Word8
dstIdx String
-> Offset Char
-> Offset Word8
-> MutableString s
-> Offset Word8
-> ST s (Offset Word8, Offset Word8)
f' MutableString s
dst'
        | Offset Char
srcI forall a. Eq a => a -> a -> Bool
== Offset Char
end = forall (m :: * -> *) a. Monad m => a -> m a
return MutableString s
dst'
        | Bool
otherwise = do (Offset Word8
nextSrcIdx, Offset Word8
nextDstIdx) <- String
-> Offset Char
-> Offset Word8
-> MutableString s
-> Offset Word8
-> ST s (Offset Word8, Offset Word8)
f' String
src Offset Char
srcI Offset Word8
srcIdx MutableString s
dst' Offset Word8
dstIdx
                         Offset Char
-> Offset Word8
-> Offset Word8
-> (String
    -> Offset Char
    -> Offset Word8
    -> MutableString s
    -> Offset Word8
    -> ST s (Offset Word8, Offset Word8))
-> MutableString s
-> ST s (MutableString s)
fill (Offset Char
srcI forall a. Additive a => a -> a -> a
+ forall ty. Int -> Offset ty
Offset Int
1) Offset Word8
nextSrcIdx Offset Word8
nextDstIdx String
-> Offset Char
-> Offset Word8
-> MutableString s
-> Offset Word8
-> ST s (Offset Word8, Offset Word8)
f' MutableString s
dst'

-- | Length of a String using CountOf
--
-- this size is available in o(n)
length :: String -> CountOf Char
length :: String -> CountOf Char
length (String UArray Word8
arr)
    | Offset Word8
start forall a. Eq a => a -> a -> Bool
== Offset Word8
end = CountOf Char
0
    | Bool
otherwise    = forall ty a s.
(Block ty -> a)
-> (FinalPtr ty -> Ptr ty -> ST s a) -> UArray ty -> a
C.onBackend Block Word8 -> CountOf Char
goVec (\FinalPtr Word8
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Ptr Word8 -> CountOf Char
goAddr) UArray Word8
arr
  where
    (C.ValidRange !Offset Word8
start !Offset Word8
end) = forall ty. UArray ty -> ValidRange ty
offsetsValidRange UArray Word8
arr
    goVec :: Block Word8 -> CountOf Char
goVec Block Word8
ma = forall container.
(Indexable container Word8, Indexable container Word64) =>
container -> Offset Word8 -> Offset Word8 -> CountOf Char
UTF8.length Block Word8
ma Offset Word8
start Offset Word8
end
    goAddr :: Ptr Word8 -> CountOf Char
goAddr Ptr Word8
ptr = forall container.
(Indexable container Word8, Indexable container Word64) =>
container -> Offset Word8 -> Offset Word8 -> CountOf Char
UTF8.length Ptr Word8
ptr Offset Word8
start Offset Word8
end

-- | Replicate a character @c@ @n@ times to create a string of length @n@
replicate :: CountOf Char -> Char -> String
replicate :: CountOf Char -> Char -> String
replicate (CountOf Int
n) Char
c = forall a. (forall s. ST s a) -> a
runST (forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new CountOf Word8
nbBytes forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
fill)
  where
    nbBytes :: CountOf Word8
nbBytes   = forall a n. (Additive a, IsNatural n) => n -> a -> a
scale (forall source destination.
Cast source destination =>
source -> destination
cast Int
n :: Word) CountOf Word8
sz
    sz :: CountOf Word8
sz = Int -> CountOf Word8
charToBytes (forall a. Enum a => a -> Int
fromEnum Char
c)
    fill :: PrimMonad prim => MutableString (PrimState prim) -> prim String
    fill :: forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
fill MutableString (PrimState prim)
ms = Offset Word8 -> prim String
loop (forall ty. Int -> Offset ty
Offset Int
0)
      where
        loop :: Offset Word8 -> prim String
loop Offset Word8
idx
            | Offset Word8
idx forall ty. Offset ty -> CountOf ty -> Bool
.==# CountOf Word8
nbBytes = forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString (PrimState prim)
ms
            | Bool
otherwise        = forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> Char -> prim (Offset Word8)
write MutableString (PrimState prim)
ms Offset Word8
idx Char
c forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Offset Word8 -> prim String
loop

-- | Copy the String
--
-- The slice of memory is copied to a new slice, making the new string
-- independent from the original string..
copy :: String -> String
copy :: String -> String
copy (String UArray Word8
s) = UArray Word8 -> String
String (forall ty. PrimType ty => UArray ty -> UArray ty
Vec.copy UArray Word8
s)

-- | Create a single element String
singleton :: Char -> String
singleton :: Char -> String
singleton Char
c = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    MutableString s
ms <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new CountOf Word8
nbBytes
    Offset Word8
_  <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> Char -> prim (Offset Word8)
write MutableString s
ms (forall ty. Int -> Offset ty
Offset Int
0) Char
c
    forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString s
ms
  where
    !nbBytes :: CountOf Word8
nbBytes = Int -> CountOf Word8
charToBytes (forall a. Enum a => a -> Int
fromEnum Char
c)

-- | Unsafely create a string of up to @sz@ bytes.
--
-- The callback @f@ needs to return the number of bytes filled in the underlaying
-- bytes buffer. No check is made on the callback return values, and if it's not
-- contained without the bounds, bad things will happen.
create :: PrimMonad prim => CountOf Word8 -> (MutableString (PrimState prim) -> prim (Offset Word8)) -> prim String
create :: forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8
-> (MutableString (PrimState prim) -> prim (Offset Word8))
-> prim String
create CountOf Word8
sz MutableString (PrimState prim) -> prim (Offset Word8)
f = do
    MutableString (PrimState prim)
ms     <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new CountOf Word8
sz
    Offset Word8
filled <- MutableString (PrimState prim) -> prim (Offset Word8)
f MutableString (PrimState prim)
ms
    if Offset Word8
filled forall ty. Offset ty -> CountOf ty -> Bool
.==# CountOf Word8
sz
        then forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString (PrimState prim)
ms
        else do
            String
s <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString (PrimState prim)
ms
            let (String UArray Word8
ba) = String
s
            forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty. CountOf ty -> UArray ty -> UArray ty
C.take (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
filled) UArray Word8
ba

-- | Monomorphically map the character in a string and return the transformed one
charMap :: (Char -> Char) -> String -> String
charMap :: (Char -> Char) -> String -> String
charMap Char -> Char
f String
src
    | CountOf Word8
srcSz forall a. Eq a => a -> a -> Bool
== CountOf Word8
0 = forall a. Monoid a => a
mempty
    | Bool
otherwise  =
        let !([(String, CountOf Word8)]
elems, CountOf Word8
nbBytes) = [(String, CountOf Word8)]
-> Offset Word8
-> CountOf Word8
-> ([(String, CountOf Word8)], CountOf Word8)
allocateAndFill [] (forall ty. Int -> Offset ty
Offset Int
0) (forall ty. Int -> CountOf ty
CountOf Int
0)
         in forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
                MutableString s
dest <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new CountOf Word8
nbBytes
                forall {m :: * -> *}.
PrimMonad m =>
MutableString (PrimState m)
-> [(String, CountOf Word8)] -> Offset Word8 -> m ()
copyLoop MutableString s
dest [(String, CountOf Word8)]
elems (forall ty. Int -> Offset ty
Offset Int
0 forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
nbBytes)
                forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString s
dest
  where
    !srcSz :: CountOf Word8
srcSz = String -> CountOf Word8
size String
src
    srcEnd :: Offset Word8
srcEnd = forall a. Additive a => a
azero forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
srcSz

    allocateAndFill :: [(String, CountOf Word8)]
                    -> Offset8
                    -> CountOf Word8
                    -> ([(String,CountOf Word8)], CountOf Word8)
    allocateAndFill :: [(String, CountOf Word8)]
-> Offset Word8
-> CountOf Word8
-> ([(String, CountOf Word8)], CountOf Word8)
allocateAndFill [(String, CountOf Word8)]
acc Offset Word8
idx CountOf Word8
bytesWritten
        | Offset Word8
idx forall a. Eq a => a -> a -> Bool
== Offset Word8
srcEnd = ([(String, CountOf Word8)]
acc, CountOf Word8
bytesWritten)
        | Bool
otherwise     =
            let (el :: (String, CountOf Word8)
el@(String
_,CountOf Word8
addBytes), Offset Word8
idx') = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
                    -- make sure we allocate at least 4 bytes for the destination for the last few bytes
                    -- otherwise allocating less would bring the danger of spinning endlessly
                    -- and never succeeding.
                    let !diffBytes :: Difference (Offset Word8)
diffBytes = Offset Word8
srcEnd forall a. Subtractive a => a -> a -> Difference a
- Offset Word8
idx
                        !allocatedBytes :: CountOf Word8
allocatedBytes = if Difference (Offset Word8)
diffBytes forall a. Ord a => a -> a -> Bool
<= forall ty. Int -> CountOf ty
CountOf Int
4 then forall ty. Int -> CountOf ty
CountOf Int
4 else Difference (Offset Word8)
diffBytes
                    MutableString s
ms <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new CountOf Word8
allocatedBytes
                    (CountOf Word8
dstIdx, Offset Word8
srcIdx) <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> CountOf Word8
-> Offset Word8
-> prim (CountOf Word8, Offset Word8)
fill MutableString s
ms CountOf Word8
allocatedBytes Offset Word8
idx
                    String
s <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString s
ms
                    forall (m :: * -> *) a. Monad m => a -> m a
return ((String
s, CountOf Word8
dstIdx), Offset Word8
srcIdx)
             in [(String, CountOf Word8)]
-> Offset Word8
-> CountOf Word8
-> ([(String, CountOf Word8)], CountOf Word8)
allocateAndFill ((String, CountOf Word8)
el forall a. a -> [a] -> [a]
: [(String, CountOf Word8)]
acc) Offset Word8
idx' (CountOf Word8
bytesWritten forall a. Additive a => a -> a -> a
+ CountOf Word8
addBytes)

    fill :: PrimMonad prim
         => MutableString (PrimState prim)
         -> CountOf Word8
         -> Offset8
         -> prim (CountOf Word8, Offset8)
    fill :: forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> CountOf Word8
-> Offset Word8
-> prim (CountOf Word8, Offset Word8)
fill MutableString (PrimState prim)
mba CountOf Word8
dsz Offset Word8
srcIdxOrig =
        Offset Word8 -> Offset Word8 -> prim (CountOf Word8, Offset Word8)
loop (forall ty. Int -> Offset ty
Offset Int
0) Offset Word8
srcIdxOrig
      where
        endDst :: Offset Word8
endDst = (forall ty. Int -> Offset ty
Offset Int
0) forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
dsz
        loop :: Offset Word8 -> Offset Word8 -> prim (CountOf Word8, Offset Word8)
loop Offset Word8
dstIdx Offset Word8
srcIdx
            | Offset Word8
srcIdx forall a. Eq a => a -> a -> Bool
== Offset Word8
srcEnd = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
dstIdx, Offset Word8
srcIdx)
            | Offset Word8
dstIdx forall a. Eq a => a -> a -> Bool
== Offset Word8
endDst = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
dstIdx, Offset Word8
srcIdx)
            | Bool
otherwise        =
                let !(Step Char
c Offset Word8
srcIdx') = String -> Offset Word8 -> Step
next String
src Offset Word8
srcIdx
                    c' :: Char
c' = Char -> Char
f Char
c -- the mapped char
                    !nbBytes :: CountOf Word8
nbBytes = Int -> CountOf Word8
charToBytes (forall a. Enum a => a -> Int
fromEnum Char
c')
                 in -- check if we have room in the destination buffer
                    if Offset Word8
dstIdx forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
nbBytes forall a. Ord a => a -> a -> Bool
<= forall a. CountOf a -> Offset a
sizeAsOffset CountOf Word8
dsz
                        then do Offset Word8
dstIdx' <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> Char -> prim (Offset Word8)
write MutableString (PrimState prim)
mba Offset Word8
dstIdx Char
c'
                                Offset Word8 -> Offset Word8 -> prim (CountOf Word8, Offset Word8)
loop Offset Word8
dstIdx' Offset Word8
srcIdx'
                        else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
dstIdx, Offset Word8
srcIdx)

    copyLoop :: MutableString (PrimState m)
-> [(String, CountOf Word8)] -> Offset Word8 -> m ()
copyLoop MutableString (PrimState m)
_   []     (Offset Int
0) = forall (m :: * -> *) a. Monad m => a -> m a
return ()
    copyLoop MutableString (PrimState m)
_   []     Offset Word8
n          = forall a. HasCallStack => [Char] -> a
error ([Char]
"charMap invalid: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show Offset Word8
n)
    copyLoop ms :: MutableString (PrimState m)
ms@(MutableString MUArray Word8 (PrimState m)
mba) ((String UArray Word8
ba, CountOf Word8
sz):[(String, CountOf Word8)]
xs) Offset Word8
end = do
        let start :: Offset Word8
start = Offset Word8
end forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetMinusE` CountOf Word8
sz
        forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim)
-> Offset ty -> UArray ty -> Offset ty -> CountOf ty -> prim ()
Vec.unsafeCopyAtRO MUArray Word8 (PrimState m)
mba Offset Word8
start UArray Word8
ba (forall ty. Int -> Offset ty
Offset Int
0) CountOf Word8
sz
        MutableString (PrimState m)
-> [(String, CountOf Word8)] -> Offset Word8 -> m ()
copyLoop MutableString (PrimState m)
ms [(String, CountOf Word8)]
xs Offset Word8
start

-- | Append a Char to the end of the String and return this new String
snoc :: String -> Char -> String
snoc :: String -> Char -> String
snoc s :: String
s@(String UArray Word8
ba) Char
c
    | CountOf Word8
len forall a. Eq a => a -> a -> Bool
== forall ty. Int -> CountOf ty
CountOf Int
0 = Char -> String
singleton Char
c
    | Bool
otherwise     = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
        MutableString s
ms <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new (CountOf Word8
len forall a. Additive a => a -> a -> a
+ CountOf Word8
nbBytes)
        let (MutableString MUArray Word8 s
mba) = MutableString s
ms
        forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim)
-> Offset ty -> UArray ty -> Offset ty -> CountOf ty -> prim ()
Vec.unsafeCopyAtRO MUArray Word8 s
mba (forall ty. Int -> Offset ty
Offset Int
0) UArray Word8
ba (forall ty. Int -> Offset ty
Offset Int
0) CountOf Word8
len
        Offset Word8
_ <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> Char -> prim (Offset Word8)
write MutableString s
ms (forall a. Additive a => a
azero forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
len) Char
c
        forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString s
ms
  where
    !len :: CountOf Word8
len     = String -> CountOf Word8
size String
s
    !nbBytes :: CountOf Word8
nbBytes = Int -> CountOf Word8
charToBytes (forall a. Enum a => a -> Int
fromEnum Char
c)

-- | Prepend a Char to the beginning of the String and return this new String
cons :: Char -> String -> String
cons :: Char -> String -> String
cons Char
c s :: String
s@(String UArray Word8
ba)
  | CountOf Word8
len forall a. Eq a => a -> a -> Bool
== forall ty. Int -> CountOf ty
CountOf Int
0 = Char -> String
singleton Char
c
  | Bool
otherwise     = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
      MutableString s
ms <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new (CountOf Word8
len forall a. Additive a => a -> a -> a
+ CountOf Word8
nbBytes)
      let (MutableString MUArray Word8 s
mba) = MutableString s
ms
      Offset Word8
idx <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> Char -> prim (Offset Word8)
write MutableString s
ms (forall ty. Int -> Offset ty
Offset Int
0) Char
c
      forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim)
-> Offset ty -> UArray ty -> Offset ty -> CountOf ty -> prim ()
Vec.unsafeCopyAtRO MUArray Word8 s
mba Offset Word8
idx UArray Word8
ba (forall ty. Int -> Offset ty
Offset Int
0) CountOf Word8
len
      forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString s
ms
  where
    !len :: CountOf Word8
len     = String -> CountOf Word8
size String
s
    !nbBytes :: CountOf Word8
nbBytes = Int -> CountOf Word8
charToBytes (forall a. Enum a => a -> Int
fromEnum Char
c)

-- | Extract the String stripped of the last character and the last character if not empty
--
-- If empty, Nothing is returned
unsnoc :: String -> Maybe (String, Char)
unsnoc :: String -> Maybe (String, Char)
unsnoc s :: String
s@(String UArray Word8
arr)
    | CountOf Word8
sz forall a. Eq a => a -> a -> Bool
== CountOf Word8
0   = forall a. Maybe a
Nothing
    | Bool
otherwise =
        let !(StepBack Char
c Offset Word8
idx) = String -> Offset Word8 -> StepBack
prev String
s (forall a. CountOf a -> Offset a
sizeAsOffset CountOf Word8
sz)
         in forall a. a -> Maybe a
Just (UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty. CountOf ty -> UArray ty -> UArray ty
Vec.take (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
idx) UArray Word8
arr, Char
c)
  where
    sz :: CountOf Word8
sz = String -> CountOf Word8
size String
s

-- | Extract the First character of a string, and the String stripped of the first character.
--
-- If empty, Nothing is returned
uncons :: String -> Maybe (Char, String)
uncons :: String -> Maybe (Char, String)
uncons s :: String
s@(String UArray Word8
ba)
    | String -> Bool
null String
s    = forall a. Maybe a
Nothing
    | Bool
otherwise =
        let !(Step Char
c Offset Word8
idx) = String -> Offset Word8 -> Step
next String
s forall a. Additive a => a
azero
         in forall a. a -> Maybe a
Just (Char
c, UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty. CountOf ty -> UArray ty -> UArray ty
Vec.drop (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
idx) UArray Word8
ba)

-- | Look for a predicate in the String and return the matched character, if any.
find :: (Char -> Bool) -> String -> Maybe Char
find :: (Char -> Bool) -> String -> Maybe Char
find Char -> Bool
predicate String
s = Offset Word8 -> Maybe Char
loop (forall ty. Int -> Offset ty
Offset Int
0)
  where
    !sz :: CountOf Word8
sz = String -> CountOf Word8
size String
s
    end :: Offset Word8
end = forall ty. Int -> Offset ty
Offset Int
0 forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz
    loop :: Offset Word8 -> Maybe Char
loop Offset Word8
idx
        | Offset Word8
idx forall a. Eq a => a -> a -> Bool
== Offset Word8
end = forall a. Maybe a
Nothing
        | Bool
otherwise =
            let !(Step Char
c Offset Word8
idx') = String -> Offset Word8 -> Step
next String
s Offset Word8
idx
             in case Char -> Bool
predicate Char
c of
                    Bool
True  -> forall a. a -> Maybe a
Just Char
c
                    Bool
False -> Offset Word8 -> Maybe Char
loop Offset Word8
idx'

-- | Sort the character in a String using a specific sort function
--
-- TODO: optimise not going through a list
sortBy :: (Char -> Char -> Ordering) -> String -> String
sortBy :: (Char -> Char -> Ordering) -> String -> String
sortBy Char -> Char -> Ordering
sortF String
s = forall l. IsList l => [Item l] -> l
fromList forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> Ordering) -> [a] -> [a]
Data.List.sortBy Char -> Char -> Ordering
sortF forall a b. (a -> b) -> a -> b
$ forall l. IsList l => l -> [Item l]
toList String
s -- FIXME for tests

-- | Filter characters of a string using the predicate
filter :: (Char -> Bool) -> String -> String
filter :: (Char -> Bool) -> String -> String
filter Char -> Bool
predicate (String UArray Word8
arr) = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    (CountOf Word8
finalSize, MutableString s
dst) <- forall (prim :: * -> *) a.
PrimMonad prim =>
CountOf Word8
-> (MutableBlock Word8 (PrimState prim) -> prim a)
-> prim (a, MutableString (PrimState prim))
newNative CountOf Word8
sz forall a b. (a -> b) -> a -> b
$ \(MutableBlock MutableByteArray# (PrimState (ST s))
mba) ->
        forall (prim :: * -> *) ty a.
PrimMonad prim =>
(Block ty -> prim a)
-> (FinalPtr ty -> prim a) -> UArray ty -> prim a
C.onBackendPrim (\ba :: Block Word8
ba@(Block !ByteArray#
_) -> forall s container.
Indexable container Word8 =>
(Char -> Bool)
-> CountOf Word8
-> MutableByteArray# s
-> container
-> Offset Word8
-> ST s (CountOf Word8)
Alg.copyFilter Char -> Bool
predicate CountOf Word8
sz MutableByteArray# (PrimState (ST s))
mba Block Word8
ba Offset Word8
start)
                        (\FinalPtr Word8
fptr -> forall (prim :: * -> *) p a.
PrimMonad prim =>
FinalPtr p -> (Ptr p -> prim a) -> prim a
withFinalPtr FinalPtr Word8
fptr forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Word8
ptr@(Ptr !Addr#
_) -> forall s container.
Indexable container Word8 =>
(Char -> Bool)
-> CountOf Word8
-> MutableByteArray# s
-> container
-> Offset Word8
-> ST s (CountOf Word8)
Alg.copyFilter Char -> Bool
predicate CountOf Word8
sz MutableByteArray# (PrimState (ST s))
mba Ptr Word8
ptr Offset Word8
start)
                        UArray Word8
arr
    forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> MutableString (PrimState prim) -> prim String
freezeShrink CountOf Word8
finalSize MutableString s
dst
  where
    !sz :: CountOf Word8
sz    = forall ty. UArray ty -> CountOf ty
C.length UArray Word8
arr
    !start :: Offset Word8
start = forall ty. UArray ty -> Offset ty
C.offset UArray Word8
arr

-- | Reverse a string
reverse :: String -> String
reverse :: String -> String
reverse (String UArray Word8
arr) = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    MutableString s
s <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8
-> (MutableBlock Word8 (PrimState prim) -> prim ())
-> prim (MutableString (PrimState prim))
newNative_ (forall ty. UArray ty -> CountOf ty
C.length UArray Word8
arr) forall a b. (a -> b) -> a -> b
$ \(MutableBlock MutableByteArray# (PrimState (ST s))
mba) ->
            forall (prim :: * -> *) ty a.
PrimMonad prim =>
(Block ty -> prim a)
-> (FinalPtr ty -> prim a) -> UArray ty -> prim a
C.onBackendPrim
                (\ba :: Block Word8
ba@(Block !ByteArray#
_) -> forall (prim :: * -> *) container.
(PrimMonad prim, Indexable container Word8) =>
MutableByteArray# (PrimState prim)
-> Offset Word8
-> container
-> Offset Word8
-> Offset Word8
-> prim ()
UTF8.reverse MutableByteArray# (PrimState (ST s))
mba Offset Word8
0 Block Word8
ba Offset Word8
start Offset Word8
end)
                (\FinalPtr Word8
fptr -> forall (prim :: * -> *) p a.
PrimMonad prim =>
FinalPtr p -> (Ptr p -> prim a) -> prim a
withFinalPtr FinalPtr Word8
fptr forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Word8
ptr@(Ptr !Addr#
_) -> forall (prim :: * -> *) container.
(PrimMonad prim, Indexable container Word8) =>
MutableByteArray# (PrimState prim)
-> Offset Word8
-> container
-> Offset Word8
-> Offset Word8
-> prim ()
UTF8.reverse MutableByteArray# (PrimState (ST s))
mba Offset Word8
0 Ptr Word8
ptr Offset Word8
start Offset Word8
end)
                UArray Word8
arr
    forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze MutableString s
s
  where
    !(C.ValidRange Offset Word8
start Offset Word8
end) = forall ty. UArray ty -> ValidRange ty
C.offsetsValidRange UArray Word8
arr

-- | Finds where are the insertion points when we search for a `needle`
-- within an `haystack`.
indices :: String -> String -> [Offset8]
indices :: String -> String -> [Offset Word8]
indices (String UArray Word8
ned) (String UArray Word8
hy) = forall ty. PrimType ty => UArray ty -> UArray ty -> [Offset ty]
Vec.indices UArray Word8
ned UArray Word8
hy

-- | Replace all the occurrencies of `needle` with `replacement` in
-- the `haystack` string.
replace :: String -> String -> String -> String
replace :: String -> String -> String -> String
replace (String UArray Word8
needle) (String UArray Word8
replacement) (String UArray Word8
haystack) =
  UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty.
PrimType ty =>
UArray ty -> UArray ty -> UArray ty -> UArray ty
Vec.replace UArray Word8
needle UArray Word8
replacement UArray Word8
haystack

-- | Return the nth character in a String
--
-- Compared to an array, the string need to be scanned from the beginning
-- since the UTF8 encoding is variable.
index :: String -> Offset Char -> Maybe Char
index :: String -> Offset Char -> Maybe Char
index String
s Offset Char
n
    | Offset Word8
ofs forall a. Ord a => a -> a -> Bool
>= Offset Word8
end = forall a. Maybe a
Nothing
    | Bool
otherwise  =
        let (Step !Char
c Offset Word8
_) = String -> Offset Word8 -> Step
next String
s Offset Word8
ofs
         in forall a. a -> Maybe a
Just Char
c
  where
    !nbBytes :: CountOf Word8
nbBytes = String -> CountOf Word8
size String
s
    end :: Offset Word8
end = Offset Word8
0 forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
nbBytes
    ofs :: Offset Word8
ofs = CountOf Char -> String -> Offset Word8
indexN (forall a. Offset a -> CountOf a
offsetAsSize Offset Char
n) String
s

-- | Return the index in unit of Char of the first occurence of the predicate returning True
--
-- If not found, Nothing is returned
findIndex :: (Char -> Bool) -> String -> Maybe (Offset Char)
findIndex :: (Char -> Bool) -> String -> Maybe (Offset Char)
findIndex Char -> Bool
predicate String
s = Offset Char -> Offset Word8 -> Maybe (Offset Char)
loop Offset Char
0 Offset Word8
0
  where
    !sz :: CountOf Word8
sz = String -> CountOf Word8
size String
s
    loop :: Offset Char -> Offset Word8 -> Maybe (Offset Char)
loop Offset Char
ofs Offset Word8
idx
        | Offset Word8
idx forall ty. Offset ty -> CountOf ty -> Bool
.==# CountOf Word8
sz = forall a. Maybe a
Nothing
        | Bool
otherwise   =
            let !(Step Char
c Offset Word8
idx') = String -> Offset Word8 -> Step
next String
s Offset Word8
idx
             in case Char -> Bool
predicate Char
c of
                    Bool
True  -> forall a. a -> Maybe a
Just Offset Char
ofs
                    Bool
False -> Offset Char -> Offset Word8 -> Maybe (Offset Char)
loop (Offset Char
ofsforall a. Additive a => a -> a -> a
+Offset Char
1) Offset Word8
idx'

-- | Various String Encoding that can be use to convert to and from bytes
data Encoding
    = ASCII7
    | UTF8
    | UTF16
    | UTF32
    | ISO_8859_1
    deriving (Typeable, Typeable Encoding
Encoding -> DataType
Encoding -> Constr
(forall b. Data b => b -> b) -> Encoding -> Encoding
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Encoding -> u
forall u. (forall d. Data d => d -> u) -> Encoding -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Encoding -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Encoding -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Encoding -> m Encoding
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Encoding -> m Encoding
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Encoding
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Encoding -> c Encoding
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Encoding)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Encoding)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Encoding -> m Encoding
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Encoding -> m Encoding
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Encoding -> m Encoding
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Encoding -> m Encoding
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Encoding -> m Encoding
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Encoding -> m Encoding
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Encoding -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Encoding -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Encoding -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Encoding -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Encoding -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Encoding -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Encoding -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Encoding -> r
gmapT :: (forall b. Data b => b -> b) -> Encoding -> Encoding
$cgmapT :: (forall b. Data b => b -> b) -> Encoding -> Encoding
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Encoding)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Encoding)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Encoding)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Encoding)
dataTypeOf :: Encoding -> DataType
$cdataTypeOf :: Encoding -> DataType
toConstr :: Encoding -> Constr
$ctoConstr :: Encoding -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Encoding
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Encoding
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Encoding -> c Encoding
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Encoding -> c Encoding
Data, Encoding -> Encoding -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Encoding -> Encoding -> Bool
$c/= :: Encoding -> Encoding -> Bool
== :: Encoding -> Encoding -> Bool
$c== :: Encoding -> Encoding -> Bool
Eq, Eq Encoding
Encoding -> Encoding -> Bool
Encoding -> Encoding -> Ordering
Encoding -> Encoding -> Encoding
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Encoding -> Encoding -> Encoding
$cmin :: Encoding -> Encoding -> Encoding
max :: Encoding -> Encoding -> Encoding
$cmax :: Encoding -> Encoding -> Encoding
>= :: Encoding -> Encoding -> Bool
$c>= :: Encoding -> Encoding -> Bool
> :: Encoding -> Encoding -> Bool
$c> :: Encoding -> Encoding -> Bool
<= :: Encoding -> Encoding -> Bool
$c<= :: Encoding -> Encoding -> Bool
< :: Encoding -> Encoding -> Bool
$c< :: Encoding -> Encoding -> Bool
compare :: Encoding -> Encoding -> Ordering
$ccompare :: Encoding -> Encoding -> Ordering
Ord, Int -> Encoding -> ShowS
[Encoding] -> ShowS
Encoding -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [Encoding] -> ShowS
$cshowList :: [Encoding] -> ShowS
show :: Encoding -> [Char]
$cshow :: Encoding -> [Char]
showsPrec :: Int -> Encoding -> ShowS
$cshowsPrec :: Int -> Encoding -> ShowS
Show, Int -> Encoding
Encoding -> Int
Encoding -> [Encoding]
Encoding -> Encoding
Encoding -> Encoding -> [Encoding]
Encoding -> Encoding -> Encoding -> [Encoding]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Encoding -> Encoding -> Encoding -> [Encoding]
$cenumFromThenTo :: Encoding -> Encoding -> Encoding -> [Encoding]
enumFromTo :: Encoding -> Encoding -> [Encoding]
$cenumFromTo :: Encoding -> Encoding -> [Encoding]
enumFromThen :: Encoding -> Encoding -> [Encoding]
$cenumFromThen :: Encoding -> Encoding -> [Encoding]
enumFrom :: Encoding -> [Encoding]
$cenumFrom :: Encoding -> [Encoding]
fromEnum :: Encoding -> Int
$cfromEnum :: Encoding -> Int
toEnum :: Int -> Encoding
$ctoEnum :: Int -> Encoding
pred :: Encoding -> Encoding
$cpred :: Encoding -> Encoding
succ :: Encoding -> Encoding
$csucc :: Encoding -> Encoding
Enum, Encoding
forall a. a -> a -> Bounded a
maxBound :: Encoding
$cmaxBound :: Encoding
minBound :: Encoding
$cminBound :: Encoding
Bounded)

fromEncoderBytes :: ( Encoder.Encoding encoding
                    , PrimType (Encoder.Unit encoding)
                    )
                 => encoding
                 -> UArray Word8
                 -> (String, Maybe ValidationFailure, UArray Word8)
fromEncoderBytes :: forall encoding.
(Encoding encoding, PrimType (Unit encoding)) =>
encoding
-> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
fromEncoderBytes encoding
enc UArray Word8
bytes =
    case forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall (st :: * -> *) input output.
(PrimMonad st, Monad st, Encoding input, PrimType (Unit input),
 Encoding output, PrimType (Unit output)) =>
input
-> output
-> UArray (Unit input)
-> st
     (Either (Offset (Unit input), Error input) (UArray (Unit output)))
Encoder.convertFromTo encoding
enc EncoderUTF8
EncoderUTF8 (forall a b. (PrimType a, PrimType b) => UArray a -> UArray b
Vec.recast UArray Word8
bytes) of
        -- TODO: Don't swallow up specific error (second element of pair)
        -- TODO: Confused why all this recasting is necessary. I "typed hole"-ed my way to get this function to compile.  Feels like there should be a cleaner method.
        Left (Offset (Unit encoding)
off, Error encoding
_) ->
            let (UArray (Unit encoding)
b1, UArray (Unit encoding)
b2) = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
Vec.splitAt (forall a. Offset a -> CountOf a
offsetAsSize Offset (Unit encoding)
off) (forall a b. (PrimType a, PrimType b) => UArray a -> UArray b
Vec.recast UArray Word8
bytes)
            in (UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall a b. (PrimType a, PrimType b) => UArray a -> UArray b
Vec.recast UArray (Unit encoding)
b1, forall a. a -> Maybe a
Just ValidationFailure
BuildingFailure, forall a b. (PrimType a, PrimType b) => UArray a -> UArray b
Vec.recast UArray (Unit encoding)
b2)
        Right UArray Word8
converted -> (UArray Word8 -> String
String UArray Word8
converted, forall a. Maybe a
Nothing, forall a. Monoid a => a
mempty)

-- | Convert a ByteArray to a string assuming a specific encoding.
--
-- It returns a 3-tuple of:
--
-- * The string that has been succesfully converted without any error
-- * An optional validation error
-- * The remaining buffer that hasn't been processed (either as a result of an error, or because the encoded sequence is not fully available)
--
-- Considering a stream of data that is fetched chunk by chunk, it's valid to assume
-- that some sequence might fall in a chunk boundary. When converting chunks,
-- if the error is Nothing and the remaining buffer is not empty, then this buffer
-- need to be prepended to the next chunk
fromBytes :: Encoding -> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
fromBytes :: Encoding
-> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
fromBytes Encoding
ASCII7     UArray Word8
bytes = forall encoding.
(Encoding encoding, PrimType (Unit encoding)) =>
encoding
-> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
fromEncoderBytes ASCII7
Encoder.ASCII7     UArray Word8
bytes
fromBytes Encoding
ISO_8859_1 UArray Word8
bytes = forall encoding.
(Encoding encoding, PrimType (Unit encoding)) =>
encoding
-> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
fromEncoderBytes ISO_8859_1
Encoder.ISO_8859_1 UArray Word8
bytes
fromBytes Encoding
UTF16      UArray Word8
bytes = forall encoding.
(Encoding encoding, PrimType (Unit encoding)) =>
encoding
-> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
fromEncoderBytes UTF16
Encoder.UTF16      UArray Word8
bytes
fromBytes Encoding
UTF32      UArray Word8
bytes = forall encoding.
(Encoding encoding, PrimType (Unit encoding)) =>
encoding
-> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
fromEncoderBytes UTF32
Encoder.UTF32      UArray Word8
bytes
fromBytes Encoding
UTF8       UArray Word8
bytes
    | forall ty. UArray ty -> Bool
C.null UArray Word8
bytes = (forall a. Monoid a => a
mempty, forall a. Maybe a
Nothing, forall a. Monoid a => a
mempty)
    | Bool
otherwise    =
        case UArray Word8
-> Offset Word8
-> CountOf Word8
-> (Offset Word8, Maybe ValidationFailure)
validate UArray Word8
bytes (forall ty. Int -> Offset ty
Offset Int
0) (forall ty. UArray ty -> CountOf ty
C.length UArray Word8
bytes) of
            (Offset Word8
_, Maybe ValidationFailure
Nothing)  -> (UArray Word8 -> String
fromBytesUnsafe UArray Word8
bytes, forall a. Maybe a
Nothing, forall a. Monoid a => a
mempty)
            (Offset Word8
pos, Just ValidationFailure
vf) ->
                let (UArray Word8
b1, UArray Word8
b2) = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
pos) UArray Word8
bytes
                 in (UArray Word8 -> String
fromBytesUnsafe UArray Word8
b1, ValidationFailure -> Maybe ValidationFailure
toErr ValidationFailure
vf, UArray Word8
b2)
  where
    toErr :: ValidationFailure -> Maybe ValidationFailure
toErr ValidationFailure
MissingByte         = forall a. Maybe a
Nothing
    toErr ValidationFailure
InvalidHeader       = forall a. a -> Maybe a
Just ValidationFailure
InvalidHeader
    toErr ValidationFailure
InvalidContinuation = forall a. a -> Maybe a
Just ValidationFailure
InvalidContinuation
    toErr ValidationFailure
BuildingFailure     = forall a. a -> Maybe a
Just ValidationFailure
BuildingFailure

-- | Convert a UTF8 array of bytes to a String.
--
-- If there's any error in the stream, it will automatically
-- insert replacement bytes to replace invalid sequences.
--
-- In the case of sequence that fall in the middle of 2 chunks,
-- the remaining buffer is supposed to be preprended to the
-- next chunk, and resume the parsing.
fromBytesLenient :: UArray Word8 -> (String, UArray Word8)
fromBytesLenient :: UArray Word8 -> (String, UArray Word8)
fromBytesLenient UArray Word8
bytes
    | forall ty. UArray ty -> Bool
C.null UArray Word8
bytes = (forall a. Monoid a => a
mempty, forall a. Monoid a => a
mempty)
    | Bool
otherwise    =
        case UArray Word8
-> Offset Word8
-> CountOf Word8
-> (Offset Word8, Maybe ValidationFailure)
validate UArray Word8
bytes (forall ty. Int -> Offset ty
Offset Int
0) (forall ty. UArray ty -> CountOf ty
C.length UArray Word8
bytes) of
            (Offset Word8
_, Maybe ValidationFailure
Nothing)                   -> (UArray Word8 -> String
fromBytesUnsafe UArray Word8
bytes, forall a. Monoid a => a
mempty)
            -- TODO: Should anything be done in the 'BuildingFailure' case?
            (Offset Word8
_, Just ValidationFailure
BuildingFailure) -> forall a. HasCallStack => [Char] -> a
error [Char]
"fromBytesLenient: FIXME!"
            (Offset Word8
pos, Just ValidationFailure
MissingByte) ->
                let (UArray Word8
b1,UArray Word8
b2) = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
pos) UArray Word8
bytes
                 in (UArray Word8 -> String
fromBytesUnsafe UArray Word8
b1, UArray Word8
b2)
            (Offset Word8
pos, Just ValidationFailure
InvalidHeader) ->
                let (UArray Word8
b1,UArray Word8
b2) = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
pos) UArray Word8
bytes
                    (UArray Word8
_,UArray Word8
b3)  = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt CountOf Word8
1 UArray Word8
b2
                    (String
s3, UArray Word8
r) = UArray Word8 -> (String, UArray Word8)
fromBytesLenient UArray Word8
b3
                 in (forall a. Monoid a => [a] -> a
mconcat [UArray Word8 -> String
fromBytesUnsafe UArray Word8
b1,String
replacement, String
s3], UArray Word8
r)
            (Offset Word8
pos, Just ValidationFailure
InvalidContinuation) ->
                let (UArray Word8
b1,UArray Word8
b2) = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
pos) UArray Word8
bytes
                    (UArray Word8
_,UArray Word8
b3)  = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt CountOf Word8
1 UArray Word8
b2
                    (String
s3, UArray Word8
r) = UArray Word8 -> (String, UArray Word8)
fromBytesLenient UArray Word8
b3
                 in (forall a. Monoid a => [a] -> a
mconcat [UArray Word8 -> String
fromBytesUnsafe UArray Word8
b1,String
replacement, String
s3], UArray Word8
r)
  where
    -- This is the replacement character U+FFFD used for any invalid header or continuation
    replacement :: String
    !replacement :: String
replacement = UArray Word8 -> String
fromBytesUnsafe forall a b. (a -> b) -> a -> b
$ forall l. IsList l => [Item l] -> l
fromList [Word8
0xef,Word8
0xbf,Word8
0xbd]

-- | Decode a stream of binary chunks containing UTF8 encoding in a list of valid String
--
-- Chunk not necessarily contains a valid string, as
-- a UTF8 sequence could be split over 2 chunks.
fromChunkBytes :: [UArray Word8] -> [String]
fromChunkBytes :: [UArray Word8] -> [String]
fromChunkBytes [UArray Word8]
l = [UArray Word8] -> [String]
loop [UArray Word8]
l
  where
    loop :: [UArray Word8] -> [String]
loop []         = []
    loop [UArray Word8
bytes]    =
        case UArray Word8
-> Offset Word8
-> CountOf Word8
-> (Offset Word8, Maybe ValidationFailure)
validate UArray Word8
bytes (forall ty. Int -> Offset ty
Offset Int
0) (forall ty. UArray ty -> CountOf ty
C.length UArray Word8
bytes) of
            (Offset Word8
_, Maybe ValidationFailure
Nothing)  -> [UArray Word8 -> String
fromBytesUnsafe UArray Word8
bytes]
            (Offset Word8
_, Just ValidationFailure
err) -> forall {a} {a}. Show a => a -> a
doErr ValidationFailure
err
    loop (UArray Word8
bytes:cs :: [UArray Word8]
cs@(UArray Word8
c1:[UArray Word8]
c2)) =
        case UArray Word8
-> Offset Word8
-> CountOf Word8
-> (Offset Word8, Maybe ValidationFailure)
validate UArray Word8
bytes (forall ty. Int -> Offset ty
Offset Int
0) (forall ty. UArray ty -> CountOf ty
C.length UArray Word8
bytes) of
            (Offset Word8
_, Maybe ValidationFailure
Nothing) -> UArray Word8 -> String
fromBytesUnsafe UArray Word8
bytes forall a. a -> [a] -> [a]
: [UArray Word8] -> [String]
loop [UArray Word8]
cs
            (Offset Word8
pos, Just ValidationFailure
MissingByte) ->
                let (UArray Word8
b1,UArray Word8
b2) = forall ty. CountOf ty -> UArray ty -> (UArray ty, UArray ty)
C.splitAt (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
pos) UArray Word8
bytes
                 in UArray Word8 -> String
fromBytesUnsafe UArray Word8
b1 forall a. a -> [a] -> [a]
: [UArray Word8] -> [String]
loop ((UArray Word8
b2 forall a. Monoid a => a -> a -> a
`mappend` UArray Word8
c1) forall a. a -> [a] -> [a]
: [UArray Word8]
c2)
            (Offset Word8
_, Just ValidationFailure
err) -> forall {a} {a}. Show a => a -> a
doErr ValidationFailure
err
    doErr :: a -> a
doErr a
err = forall a. HasCallStack => [Char] -> a
error ([Char]
"fromChunkBytes: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show a
err)

-- | Convert a Byte Array representing UTF8 data directly to a string without checking for UTF8 validity
--
-- If the input contains invalid sequences, it will trigger runtime async errors when processing data.
--
-- In doubt, use 'fromBytes'
fromBytesUnsafe :: UArray Word8 -> String
fromBytesUnsafe :: UArray Word8 -> String
fromBytesUnsafe = UArray Word8 -> String
String

toEncoderBytes :: ( Encoder.Encoding encoding
                  , PrimType (Encoder.Unit encoding)
                  , Exception (Encoder.Error encoding)
                  )
               => encoding
               -> UArray Word8
               -> UArray Word8
toEncoderBytes :: forall encoding.
(Encoding encoding, PrimType (Unit encoding),
 Exception (Error encoding)) =>
encoding -> UArray Word8 -> UArray Word8
toEncoderBytes encoding
enc UArray Word8
bytes = forall a b. (PrimType a, PrimType b) => UArray a -> UArray b
Vec.recast forall a b. (a -> b) -> a -> b
$
  case forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall (st :: * -> *) input output.
(PrimMonad st, Monad st, Encoding input, PrimType (Unit input),
 Encoding output, PrimType (Unit output)) =>
input
-> output
-> UArray (Unit input)
-> st
     (Either (Offset (Unit input), Error input) (UArray (Unit output)))
Encoder.convertFromTo EncoderUTF8
EncoderUTF8 encoding
enc UArray Word8
bytes of
    Left (Offset Word8, ValidationFailure)
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"toEncoderBytes: FIXME!"
    Right UArray (Unit encoding)
converted -> UArray (Unit encoding)
converted

-- | Convert a String to a bytearray in a specific encoding
--
-- if the encoding is UTF8, the underlying buffer is returned without extra allocation or any processing
--
-- In any other encoding, some allocation and processing are done to convert.
toBytes :: Encoding -> String -> UArray Word8
toBytes :: Encoding -> String -> UArray Word8
toBytes Encoding
UTF8       (String UArray Word8
bytes) = UArray Word8
bytes
toBytes Encoding
ASCII7     (String UArray Word8
bytes) = forall encoding.
(Encoding encoding, PrimType (Unit encoding),
 Exception (Error encoding)) =>
encoding -> UArray Word8 -> UArray Word8
toEncoderBytes ASCII7
Encoder.ASCII7     UArray Word8
bytes
toBytes Encoding
ISO_8859_1 (String UArray Word8
bytes) = forall encoding.
(Encoding encoding, PrimType (Unit encoding),
 Exception (Error encoding)) =>
encoding -> UArray Word8 -> UArray Word8
toEncoderBytes ISO_8859_1
Encoder.ISO_8859_1 UArray Word8
bytes
toBytes Encoding
UTF16      (String UArray Word8
bytes) = forall encoding.
(Encoding encoding, PrimType (Unit encoding),
 Exception (Error encoding)) =>
encoding -> UArray Word8 -> UArray Word8
toEncoderBytes UTF16
Encoder.UTF16      UArray Word8
bytes
toBytes Encoding
UTF32      (String UArray Word8
bytes) = forall encoding.
(Encoding encoding, PrimType (Unit encoding),
 Exception (Error encoding)) =>
encoding -> UArray Word8 -> UArray Word8
toEncoderBytes UTF32
Encoder.UTF32      UArray Word8
bytes

-- | Split lines in a string using newline as separation.
--
-- Note that carriage return preceding a newline are also strip for
-- maximum compatibility between Windows and Unix system.
lines :: String -> [String]
lines :: String -> [String]
lines String
s =
    case String -> Either Bool (String, String)
breakLine String
s of
        Left Bool
_         -> [String
s]
        Right (String
line,String
r) -> String
line forall a. a -> [a] -> [a]
: String -> [String]
lines String
r

-- | Split words in a string using spaces as separation
--
-- > words "Hello Foundation"
-- [ "Hello", "Foundation" ]
words :: String -> [String]
words :: String -> [String]
words = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall l. IsList l => [Item l] -> l
fromList forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [Char] -> [[Char]]
Prelude.words forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall l. IsList l => l -> [Item l]
toList

-- | Append a character to a String builder
builderAppend :: PrimMonad state => Char -> Builder String MutableString Word8 state err ()
builderAppend :: forall (state :: * -> *) err.
PrimMonad state =>
Char -> Builder String MutableString Word8 state err ()
builderAppend Char
c = forall collection (mutCollection :: * -> *) step (state :: * -> *)
       err a.
State
  (Offset step,
   BuildingState collection mutCollection step (PrimState state),
   Maybe err)
  state
  a
-> Builder collection mutCollection step state err a
Builder forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) a. (s -> m (a, s)) -> State s m a
State forall a b. (a -> b) -> a -> b
$ \(Offset Word8
i, BuildingState String MutableString Word8 (PrimState state)
st, Maybe err
e) ->
    if forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
i forall a. Additive a => a -> a -> a
+ CountOf Word8
nbBytes forall a. Ord a => a -> a -> Bool
>= forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state -> CountOf step
chunkSize BuildingState String MutableString Word8 (PrimState state)
st
        then do
            String
cur      <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> CountOf Word8 -> prim String
unsafeFreezeShrink (forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state
-> mutCollection state
curChunk BuildingState String MutableString Word8 (PrimState state)
st) (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
i)
            MutableString (PrimState state)
newChunk <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new (forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state -> CountOf step
chunkSize BuildingState String MutableString Word8 (PrimState state)
st)
            forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> UTF8Char -> prim ()
writeUTF8Char MutableString (PrimState state)
newChunk (forall ty. Int -> Offset ty
Offset Int
0) UTF8Char
utf8Char
            forall (m :: * -> *) a. Monad m => a -> m a
return ((), (forall a. CountOf a -> Offset a
sizeAsOffset CountOf Word8
nbBytes, BuildingState String MutableString Word8 (PrimState state)
st { prevChunks :: [String]
prevChunks     = String
cur forall a. a -> [a] -> [a]
: forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state -> [collection]
prevChunks BuildingState String MutableString Word8 (PrimState state)
st
                                                  , prevChunksSize :: CountOf Word8
prevChunksSize = forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
i forall a. Additive a => a -> a -> a
+ forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state -> CountOf step
prevChunksSize BuildingState String MutableString Word8 (PrimState state)
st
                                                  , curChunk :: MutableString (PrimState state)
curChunk       = MutableString (PrimState state)
newChunk
                                                  }, Maybe err
e))
        else do
            forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim)
-> Offset Word8 -> UTF8Char -> prim ()
writeUTF8Char (forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state
-> mutCollection state
curChunk BuildingState String MutableString Word8 (PrimState state)
st) Offset Word8
i UTF8Char
utf8Char
            forall (m :: * -> *) a. Monad m => a -> m a
return ((), (Offset Word8
i forall a. Additive a => a -> a -> a
+ forall a. CountOf a -> Offset a
sizeAsOffset CountOf Word8
nbBytes, BuildingState String MutableString Word8 (PrimState state)
st, Maybe err
e))
  where
    utf8Char :: UTF8Char
utf8Char = Char -> UTF8Char
asUTF8Char Char
c
    nbBytes :: CountOf Word8
nbBytes  = UTF8Char -> CountOf Word8
numBytes UTF8Char
utf8Char

-- | Create a new String builder using chunks of @sizeChunksI@
builderBuild :: PrimMonad m => Int -> Builder String MutableString Word8 m err () -> m (Either err String)
builderBuild :: forall (m :: * -> *) err.
PrimMonad m =>
Int
-> Builder String MutableString Word8 m err ()
-> m (Either err String)
builderBuild Int
sizeChunksI Builder String MutableString Word8 m err ()
sb
    | Int
sizeChunksI forall a. Ord a => a -> a -> Bool
<= Int
3 = forall (m :: * -> *) err.
PrimMonad m =>
Int
-> Builder String MutableString Word8 m err ()
-> m (Either err String)
builderBuild Int
64 Builder String MutableString Word8 m err ()
sb
    | Bool
otherwise        = do
        MutableString (PrimState m)
firstChunk <- forall (prim :: * -> *).
PrimMonad prim =>
CountOf Word8 -> prim (MutableString (PrimState prim))
new CountOf Word8
sizeChunks
        (Offset Word8
i, BuildingState String MutableString Word8 (PrimState m)
st, Maybe err
e) <- forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. State s m a -> s -> m (a, s)
runState (forall collection (mutCollection :: * -> *) step (state :: * -> *)
       err a.
Builder collection mutCollection step state err a
-> State
     (Offset step,
      BuildingState collection mutCollection step (PrimState state),
      Maybe err)
     state
     a
runBuilder Builder String MutableString Word8 m err ()
sb) (forall ty. Int -> Offset ty
Offset Int
0, forall collection (mutCollection :: * -> *) step state.
[collection]
-> CountOf step
-> mutCollection state
-> CountOf step
-> BuildingState collection mutCollection step state
BuildingState [] (forall ty. Int -> CountOf ty
CountOf Int
0) MutableString (PrimState m)
firstChunk CountOf Word8
sizeChunks, forall a. Maybe a
Nothing)
        case Maybe err
e of
          Just err
err -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left err
err)
          Maybe err
Nothing -> do
            String
cur <- forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> CountOf Word8 -> prim String
unsafeFreezeShrink (forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state
-> mutCollection state
curChunk BuildingState String MutableString Word8 (PrimState m)
st) (forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
i)
            -- Build final array
            let totalSize :: CountOf Word8
totalSize = forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state -> CountOf step
prevChunksSize BuildingState String MutableString Word8 (PrimState m)
st forall a. Additive a => a -> a -> a
+ forall a. Offset a -> CountOf a
offsetAsSize Offset Word8
i
            UArray Word8
final <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
CountOf ty -> prim (MUArray ty (PrimState prim))
Vec.new CountOf Word8
totalSize forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall {m :: * -> *}.
PrimMonad m =>
CountOf Word8
-> [String]
-> MUArray Word8 (PrimState m)
-> m (MUArray Word8 (PrimState m))
fillFromEnd CountOf Word8
totalSize (String
cur forall a. a -> [a] -> [a]
: forall collection (mutCollection :: * -> *) step state.
BuildingState collection mutCollection step state -> [collection]
prevChunks BuildingState String MutableString Word8 (PrimState m)
st) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (prim :: * -> *) ty.
PrimMonad prim =>
MUArray ty (PrimState prim) -> prim (UArray ty)
Vec.unsafeFreeze
            forall (m :: * -> *) a. Monad m => a -> m a
return forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a b. b -> Either a b
Right forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ UArray Word8
final
  where
    sizeChunks :: CountOf Word8
sizeChunks = forall ty. Int -> CountOf ty
CountOf Int
sizeChunksI

    fillFromEnd :: CountOf Word8
-> [String]
-> MUArray Word8 (PrimState m)
-> m (MUArray Word8 (PrimState m))
fillFromEnd CountOf Word8
_    []            MUArray Word8 (PrimState m)
mba = forall (m :: * -> *) a. Monad m => a -> m a
return MUArray Word8 (PrimState m)
mba
    fillFromEnd !CountOf Word8
end (String UArray Word8
x:[String]
xs) MUArray Word8 (PrimState m)
mba = do
        let sz :: CountOf Word8
sz = forall ty. UArray ty -> CountOf ty
Vec.length UArray Word8
x
        let start :: CountOf Word8
start = CountOf Word8
end forall a. CountOf a -> CountOf a -> CountOf a
`sizeSub` CountOf Word8
sz
        forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MUArray ty (PrimState prim)
-> Offset ty -> UArray ty -> Offset ty -> CountOf ty -> prim ()
Vec.unsafeCopyAtRO MUArray Word8 (PrimState m)
mba (forall a. CountOf a -> Offset a
sizeAsOffset CountOf Word8
start) UArray Word8
x (forall ty. Int -> Offset ty
Offset Int
0) CountOf Word8
sz
        CountOf Word8
-> [String]
-> MUArray Word8 (PrimState m)
-> m (MUArray Word8 (PrimState m))
fillFromEnd CountOf Word8
start [String]
xs MUArray Word8 (PrimState m)
mba

builderBuild_ :: PrimMonad m => Int -> Builder String MutableString Word8 m () () -> m String
builderBuild_ :: forall (m :: * -> *).
PrimMonad m =>
Int -> Builder String MutableString Word8 m () () -> m String
builderBuild_ Int
sizeChunksI Builder String MutableString Word8 m () ()
sb = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\() -> forall a. [Char] -> a
internalError [Char]
"impossible output") forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) err.
PrimMonad m =>
Int
-> Builder String MutableString Word8 m err ()
-> m (Either err String)
builderBuild Int
sizeChunksI Builder String MutableString Word8 m () ()
sb

stringDewrap :: (Block Word8 -> Offset Word8 -> a)
             -> (Ptr Word8 -> Offset Word8 -> ST s a)
             -> String
             -> a
stringDewrap :: forall a s.
(Block Word8 -> Offset Word8 -> a)
-> (Ptr Word8 -> Offset Word8 -> ST s a) -> String -> a
stringDewrap Block Word8 -> Offset Word8 -> a
withBa Ptr Word8 -> Offset Word8 -> ST s a
withPtr (String UArray Word8
ba) = forall ty a s.
(Block ty -> Offset ty -> a)
-> (Ptr ty -> Offset ty -> ST s a) -> UArray ty -> a
C.unsafeDewrap Block Word8 -> Offset Word8 -> a
withBa Ptr Word8 -> Offset Word8 -> ST s a
withPtr UArray Word8
ba
{-# INLINE stringDewrap #-}

-- | Read an Integer from a String
--
-- Consume an optional minus sign and many digits until end of string.
readIntegral :: (HasNegation i, IntegralUpsize Word8 i, Additive i, Multiplicative i, IsIntegral i) => String -> Maybe i
readIntegral :: forall i.
(HasNegation i, IntegralUpsize Word8 i, Additive i,
 Multiplicative i, IsIntegral i) =>
String -> Maybe i
readIntegral String
str
    | CountOf Word8
sz forall a. Eq a => a -> a -> Bool
== CountOf Word8
0   = forall a. Maybe a
Nothing
    | Bool
otherwise = forall a s.
(Block Word8 -> Offset Word8 -> a)
-> (Ptr Word8 -> Offset Word8 -> ST s a) -> String -> a
stringDewrap Block Word8 -> Offset Word8 -> Maybe i
withBa (\ptr :: Ptr Word8
ptr@(Ptr !Addr#
_) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Ptr Word8 -> Offset Word8 -> Maybe i
withPtr Ptr Word8
ptr) String
str
  where
    !sz :: CountOf Word8
sz = String -> CountOf Word8
size String
str
    withBa :: Block Word8 -> Offset Word8 -> Maybe i
withBa Block Word8
ba Offset Word8
ofs =
        let negativeSign :: Bool
negativeSign = forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Word8 -> Bool
UTF8.expectAscii Block Word8
ba Offset Word8
ofs Word8
0x2d
            startOfs :: Offset Word8
startOfs     = if Bool
negativeSign then forall a. Enum a => a -> a
succ Offset Word8
ofs else Offset Word8
ofs
         in case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Block Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsBA i
0 Block Word8
ba Offset Word8
endOfs Offset Word8
startOfs of
                (# i
acc, Bool
True, Offset Word8
endOfs' #) | Offset Word8
endOfs' forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! if Bool
negativeSign then forall a. HasNegation a => a -> a
negate i
acc else i
acc
                (# i, Bool, Offset Word8 #)
_                                             -> forall a. Maybe a
Nothing
      where !endOfs :: Offset Word8
endOfs = Offset Word8
ofs forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz
    withPtr :: Ptr Word8 -> Offset Word8 -> Maybe i
withPtr Ptr Word8
addr Offset Word8
ofs =
        let negativeSign :: Bool
negativeSign = forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Word8 -> Bool
UTF8.expectAscii Ptr Word8
addr Offset Word8
ofs Word8
0x2d
            startOfs :: Offset Word8
startOfs     = if Bool
negativeSign then forall a. Enum a => a -> a
succ Offset Word8
ofs else Offset Word8
ofs
         in case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Ptr Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsPtr i
0 Ptr Word8
addr Offset Word8
endOfs Offset Word8
startOfs of
                (# i
acc, Bool
True, Offset Word8
endOfs' #) | Offset Word8
endOfs' forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! if Bool
negativeSign then forall a. HasNegation a => a -> a
negate i
acc else i
acc
                (# i, Bool, Offset Word8 #)
_                                             -> forall a. Maybe a
Nothing
      where !endOfs :: Offset Word8
endOfs = Offset Word8
ofs forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz
{-# SPECIALISE readIntegral :: String -> Maybe Integer #-}
{-# SPECIALISE readIntegral :: String -> Maybe Int #-}

readInteger :: String -> Maybe Integer
readInteger :: String -> Maybe Integer
readInteger = forall i.
(HasNegation i, IntegralUpsize Word8 i, Additive i,
 Multiplicative i, IsIntegral i) =>
String -> Maybe i
readIntegral

-- | Read a Natural from a String
--
-- Consume many digits until end of string.
readNatural :: String -> Maybe Natural
readNatural :: String -> Maybe Natural
readNatural String
str
    | CountOf Word8
sz forall a. Eq a => a -> a -> Bool
== CountOf Word8
0   = forall a. Maybe a
Nothing
    | Bool
otherwise = forall a s.
(Block Word8 -> Offset Word8 -> a)
-> (Ptr Word8 -> Offset Word8 -> ST s a) -> String -> a
stringDewrap Block Word8 -> Offset Word8 -> Maybe Natural
withBa (\ptr :: Ptr Word8
ptr@(Ptr !Addr#
_) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Ptr Word8 -> Offset Word8 -> Maybe Natural
withPtr Ptr Word8
ptr) String
str
  where
    !sz :: CountOf Word8
sz = String -> CountOf Word8
size String
str
    withBa :: Block Word8 -> Offset Word8 -> Maybe Natural
withBa Block Word8
ba Offset Word8
stringStart =
        case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Block Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsBA Natural
0 Block Word8
ba Offset Word8
eofs Offset Word8
stringStart of
            (# Natural
acc, Bool
True, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
stringStart -> forall a. a -> Maybe a
Just Natural
acc
            (# Natural, Bool, Offset Word8 #)
_                                              -> forall a. Maybe a
Nothing
      where eofs :: Offset Word8
eofs = Offset Word8
stringStart forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz
    withPtr :: Ptr Word8 -> Offset Word8 -> Maybe Natural
withPtr Ptr Word8
addr Offset Word8
stringStart =
        case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Ptr Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsPtr Natural
0 Ptr Word8
addr Offset Word8
eofs Offset Word8
stringStart of
            (# Natural
acc, Bool
True, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
stringStart -> forall a. a -> Maybe a
Just Natural
acc
            (# Natural, Bool, Offset Word8 #)
_                                              -> forall a. Maybe a
Nothing
      where eofs :: Offset Word8
eofs = Offset Word8
stringStart forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz

-- | Try to read a Double
readDouble :: String -> Maybe Double
readDouble :: String -> Maybe Double
readDouble String
s =
    forall a. String -> ReadFloatingCallback a -> Maybe a
readFloatingExact String
s forall a b. (a -> b) -> a -> b
$ \Bool
isNegative Natural
integral Word
floatingDigits Maybe Int
mExponant ->
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall {a}. HasNegation a => Bool -> a -> a
applySign Bool
isNegative forall a b. (a -> b) -> a -> b
$ case (Word
floatingDigits, Maybe Int
mExponant) of
            (Word
0, Maybe Int
Nothing)              ->                         Natural -> Double
naturalToDouble Natural
integral
            (Word
0, Just Int
exponent)        -> Int -> Double -> Double
withExponant Int
exponent forall a b. (a -> b) -> a -> b
$ Natural -> Double
naturalToDouble Natural
integral
            (Word
floating, Maybe Int
Nothing)       ->                         forall {a} {b}.
(Divisible a, Integral b, Integral a, Num a) =>
b -> a -> a
applyFloating Word
floating forall a b. (a -> b) -> a -> b
$ Natural -> Double
naturalToDouble Natural
integral
            (Word
floating, Just Int
exponent) -> Int -> Double -> Double
withExponant Int
exponent forall a b. (a -> b) -> a -> b
$ forall {a} {b}.
(Divisible a, Integral b, Integral a, Num a) =>
b -> a -> a
applyFloating Word
floating forall a b. (a -> b) -> a -> b
$ Natural -> Double
naturalToDouble Natural
integral
  where
    applySign :: Bool -> a -> a
applySign Bool
True = forall a. HasNegation a => a -> a
negate
    applySign Bool
False = forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
    withExponant :: Int -> Double -> Double
withExponant Int
e Double
v = Double
v forall a. Multiplicative a => a -> a -> a
* Double -> Int -> Double
doubleExponant Double
10 Int
e
    applyFloating :: b -> a -> a
applyFloating b
digits a
n = a
n forall a. Divisible a => a -> a -> a
/ (a
10 forall a b. (Num a, Integral b) => a -> b -> a
Prelude.^ b
digits)

-- | Try to read a floating number as a Rational
--
-- Note that for safety reason, only exponent between -10000 and 10000 is allowed
-- as otherwise DoS/OOM is very likely. if you don't want this behavior,
-- switching to a scientific type (not provided yet) that represent the
-- exponent separately is the advised solution.
readRational :: String -> Maybe Prelude.Rational
readRational :: String -> Maybe Rational
readRational String
s =
    forall a. String -> ReadFloatingCallback a -> Maybe a
readFloatingExact String
s forall a b. (a -> b) -> a -> b
$ \Bool
isNegative Natural
integral Word
floatingDigits Maybe Int
mExponant ->
        case Maybe Int
mExponant of
            Just Int
exponent
                | Int
exponent forall a. Ord a => a -> a -> Bool
< -Int
10000 Bool -> Bool -> Bool
|| Int
exponent forall a. Ord a => a -> a -> Bool
> Int
10000 -> forall a. Maybe a
Nothing
                | Bool
otherwise                             -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall {b} {a}.
(HasNegation b, IntegralUpsize a b) =>
Bool -> a -> b
modF Bool
isNegative Natural
integral forall a. Integral a => a -> a -> Ratio a
% (Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
Prelude.^ (forall source destination.
Cast source destination =>
source -> destination
cast Word
floatingDigits forall a. Subtractive a => a -> a -> Difference a
- Int
exponent))
            Maybe Int
Nothing                                     -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall {b} {a}.
(HasNegation b, IntegralUpsize a b) =>
Bool -> a -> b
modF Bool
isNegative Natural
integral forall a. Integral a => a -> a -> Ratio a
% (Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
Prelude.^ Word
floatingDigits)
  where
    modF :: Bool -> a -> b
modF Bool
True  = forall a. HasNegation a => a -> a
negate forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a b. IntegralUpsize a b => a -> b
integralUpsize
    modF Bool
False = forall a b. IntegralUpsize a b => a -> b
integralUpsize


type ReadFloatingCallback a = Bool      -- sign
                           -> Natural   -- integral part
                           -> Word      -- number of digits in floating section
                           -> Maybe Int -- optional integer representing exponent in base 10
                           -> Maybe a

-- | Read an Floating like number of the form:
--
--   [ '-' ] <numbers> [ '.' <numbers> ] [ ( 'e' | 'E' ) [ '-' ] <number> ]
--
-- Call a function with:
--
-- * A boolean representing if the number is negative
-- * The digits part represented as a single natural number (123.456 is represented as 123456)
-- * The number of digits in the fractional part (e.g. 123.456 => 3)
-- * The exponent if any
--
-- The code is structured as a simple state machine that:
--
-- * Optionally Consume a '-' sign
-- * Consume number for the integral part
-- * Optionally
--   * Consume '.'
--   * Consume remaining digits if not already end of string
-- * Optionally Consume a 'e' or 'E' follow by an optional '-' and a number
--
readFloatingExact :: String -> ReadFloatingCallback a -> Maybe a
readFloatingExact :: forall a. String -> ReadFloatingCallback a -> Maybe a
readFloatingExact String
str ReadFloatingCallback a
f
    | CountOf Word8
sz forall a. Eq a => a -> a -> Bool
== CountOf Word8
0   = forall a. Maybe a
Nothing
    | Bool
otherwise = forall a s.
(Block Word8 -> Offset Word8 -> a)
-> (Ptr Word8 -> Offset Word8 -> ST s a) -> String -> a
stringDewrap Block Word8 -> Offset Word8 -> Maybe a
withBa Ptr Word8 -> Offset Word8 -> ST Any (Maybe a)
withPtr String
str
  where
    !sz :: CountOf Word8
sz = String -> CountOf Word8
size String
str

    withBa :: Block Word8 -> Offset Word8 -> Maybe a
withBa Block Word8
ba Offset Word8
stringStart =
        let !isNegative :: Bool
isNegative = forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Word8 -> Bool
UTF8.expectAscii Block Word8
ba Offset Word8
stringStart Word8
0x2d
         in Bool -> Offset Word8 -> Maybe a
consumeIntegral Bool
isNegative (if Bool
isNegative then Offset Word8
stringStartforall a. Additive a => a -> a -> a
+Offset Word8
1 else Offset Word8
stringStart)
      where
        eofs :: Offset Word8
eofs = Offset Word8
stringStart forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz
        consumeIntegral :: Bool -> Offset Word8 -> Maybe a
consumeIntegral !Bool
isNegative Offset Word8
startOfs =
            case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Block Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsBA Natural
0 Block Word8
ba Offset Word8
eofs Offset Word8
startOfs of
                (# Natural
acc, Bool
True , Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs -> ReadFloatingCallback a
f Bool
isNegative Natural
acc Word
0 forall a. Maybe a
Nothing -- end of stream and no '.'
                (# Natural
acc, Bool
False, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs ->
                    if forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Word8 -> Bool
UTF8.expectAscii Block Word8
ba Offset Word8
endOfs Word8
0x2e
                        then Bool -> Natural -> Offset Word8 -> Maybe a
consumeFloat Bool
isNegative Natural
acc (Offset Word8
endOfs forall a. Additive a => a -> a -> a
+ Offset Word8
1)
                        else Bool -> Natural -> Word -> Offset Word8 -> Maybe a
consumeExponant Bool
isNegative Natural
acc Word
0 Offset Word8
endOfs
                (# Natural, Bool, Offset Word8 #)
_                                            -> forall a. Maybe a
Nothing

        consumeFloat :: Bool -> Natural -> Offset Word8 -> Maybe a
consumeFloat Bool
isNegative Natural
integral Offset Word8
startOfs =
            case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Block Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsBA Natural
integral Block Word8
ba Offset Word8
eofs Offset Word8
startOfs of
                (# Natural
acc, Bool
True, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs -> let (CountOf !Int
diff) = Offset Word8
endOfs forall a. Subtractive a => a -> a -> Difference a
- Offset Word8
startOfs
                                                                in ReadFloatingCallback a
f Bool
isNegative Natural
acc (forall source destination.
Cast source destination =>
source -> destination
cast Int
diff) forall a. Maybe a
Nothing
                (# Natural
acc, Bool
False, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs -> let (CountOf !Int
diff) = Offset Word8
endOfs forall a. Subtractive a => a -> a -> Difference a
- Offset Word8
startOfs
                                                                in Bool -> Natural -> Word -> Offset Word8 -> Maybe a
consumeExponant Bool
isNegative Natural
acc (forall source destination.
Cast source destination =>
source -> destination
cast Int
diff) Offset Word8
endOfs
                (# Natural, Bool, Offset Word8 #)
_                                           -> forall a. Maybe a
Nothing

        consumeExponant :: Bool -> Natural -> Word -> Offset Word8 -> Maybe a
consumeExponant !Bool
isNegative !Natural
integral !Word
floatingDigits !Offset Word8
startOfs
            | Offset Word8
startOfs forall a. Eq a => a -> a -> Bool
== Offset Word8
eofs = ReadFloatingCallback a
f Bool
isNegative Natural
integral Word
floatingDigits forall a. Maybe a
Nothing
            | Bool
otherwise        =
                -- consume 'E' or 'e'
                case forall container.
Indexable container Word8 =>
container -> Offset Word8 -> StepASCII
UTF8.nextAscii Block Word8
ba Offset Word8
startOfs of
                    StepASCII Word8
0x45 -> Offset Word8 -> Maybe a
consumeExponantSign (Offset Word8
startOfsforall a. Additive a => a -> a -> a
+Offset Word8
1)
                    StepASCII Word8
0x65 -> Offset Word8 -> Maybe a
consumeExponantSign (Offset Word8
startOfsforall a. Additive a => a -> a -> a
+Offset Word8
1)
                    StepASCII
_              -> forall a. Maybe a
Nothing
          where
            consumeExponantSign :: Offset Word8 -> Maybe a
consumeExponantSign Offset Word8
ofs
                | Offset Word8
ofs forall a. Eq a => a -> a -> Bool
== Offset Word8
eofs = forall a. Maybe a
Nothing
                | Bool
otherwise   = let exponentNegative :: Bool
exponentNegative = forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Word8 -> Bool
UTF8.expectAscii Block Word8
ba Offset Word8
ofs Word8
0x2d
                                 in Bool -> Offset Word8 -> Maybe a
consumeExponantNumber Bool
exponentNegative (if Bool
exponentNegative then Offset Word8
ofs forall a. Additive a => a -> a -> a
+ Offset Word8
1 else Offset Word8
ofs)

            consumeExponantNumber :: Bool -> Offset Word8 -> Maybe a
consumeExponantNumber Bool
exponentNegative Offset Word8
ofs =
                case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Block Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsBA Int
0 Block Word8
ba Offset Word8
eofs Offset Word8
ofs of
                    (# Int
acc, Bool
True, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
ofs -> ReadFloatingCallback a
f Bool
isNegative Natural
integral Word
floatingDigits (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! if Bool
exponentNegative then forall a. HasNegation a => a -> a
negate Int
acc else Int
acc)
                    (# Int, Bool, Offset Word8 #)
_                                      -> forall a. Maybe a
Nothing
    withPtr :: Ptr Word8 -> Offset Word8 -> ST Any (Maybe a)
withPtr ptr :: Ptr Word8
ptr@(Ptr !Addr#
_) Offset Word8
stringStart = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
        let !isNegative :: Bool
isNegative = forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Word8 -> Bool
UTF8.expectAscii Ptr Word8
ptr Offset Word8
stringStart Word8
0x2d
         in Bool -> Offset Word8 -> Maybe a
consumeIntegral Bool
isNegative (if Bool
isNegative then Offset Word8
stringStartforall a. Additive a => a -> a -> a
+Offset Word8
1 else Offset Word8
stringStart)
      where
        eofs :: Offset Word8
eofs = Offset Word8
stringStart forall ty. Offset ty -> CountOf ty -> Offset ty
`offsetPlusE` CountOf Word8
sz
        consumeIntegral :: Bool -> Offset Word8 -> Maybe a
consumeIntegral !Bool
isNegative Offset Word8
startOfs =
            case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Ptr Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsPtr Natural
0 Ptr Word8
ptr Offset Word8
eofs Offset Word8
startOfs of
                (# Natural
acc, Bool
True , Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs -> ReadFloatingCallback a
f Bool
isNegative Natural
acc Word
0 forall a. Maybe a
Nothing -- end of stream and no '.'
                (# Natural
acc, Bool
False, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs ->
                    if forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Word8 -> Bool
UTF8.expectAscii Ptr Word8
ptr Offset Word8
endOfs Word8
0x2e
                        then Bool -> Natural -> Offset Word8 -> Maybe a
consumeFloat Bool
isNegative Natural
acc (Offset Word8
endOfs forall a. Additive a => a -> a -> a
+ Offset Word8
1)
                        else Bool -> Natural -> Word -> Offset Word8 -> Maybe a
consumeExponant Bool
isNegative Natural
acc Word
0 Offset Word8
endOfs
                (# Natural, Bool, Offset Word8 #)
_                                            -> forall a. Maybe a
Nothing

        consumeFloat :: Bool -> Natural -> Offset Word8 -> Maybe a
consumeFloat Bool
isNegative Natural
integral Offset Word8
startOfs =
            case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Ptr Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsPtr Natural
integral Ptr Word8
ptr Offset Word8
eofs Offset Word8
startOfs of
                (# Natural
acc, Bool
True, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs -> let (CountOf !Int
diff) = Offset Word8
endOfs forall a. Subtractive a => a -> a -> Difference a
- Offset Word8
startOfs
                                                                in ReadFloatingCallback a
f Bool
isNegative Natural
acc (forall source destination.
Cast source destination =>
source -> destination
cast Int
diff) forall a. Maybe a
Nothing
                (# Natural
acc, Bool
False, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
startOfs -> let (CountOf !Int
diff) = Offset Word8
endOfs forall a. Subtractive a => a -> a -> Difference a
- Offset Word8
startOfs
                                                                in Bool -> Natural -> Word -> Offset Word8 -> Maybe a
consumeExponant Bool
isNegative Natural
acc (forall source destination.
Cast source destination =>
source -> destination
cast Int
diff) Offset Word8
endOfs
                (# Natural, Bool, Offset Word8 #)
_                                           -> forall a. Maybe a
Nothing

        consumeExponant :: Bool -> Natural -> Word -> Offset Word8 -> Maybe a
consumeExponant !Bool
isNegative !Natural
integral !Word
floatingDigits !Offset Word8
startOfs
            | Offset Word8
startOfs forall a. Eq a => a -> a -> Bool
== Offset Word8
eofs = ReadFloatingCallback a
f Bool
isNegative Natural
integral Word
floatingDigits forall a. Maybe a
Nothing
            | Bool
otherwise        =
                -- consume 'E' or 'e'
                case forall container.
Indexable container Word8 =>
container -> Offset Word8 -> StepASCII
UTF8.nextAscii Ptr Word8
ptr Offset Word8
startOfs of
                    StepASCII Word8
0x45 -> Offset Word8 -> Maybe a
consumeExponantSign (Offset Word8
startOfsforall a. Additive a => a -> a -> a
+Offset Word8
1)
                    StepASCII Word8
0x65 -> Offset Word8 -> Maybe a
consumeExponantSign (Offset Word8
startOfsforall a. Additive a => a -> a -> a
+Offset Word8
1)
                    StepASCII
_              -> forall a. Maybe a
Nothing
          where
            consumeExponantSign :: Offset Word8 -> Maybe a
consumeExponantSign Offset Word8
ofs
                | Offset Word8
ofs forall a. Eq a => a -> a -> Bool
== Offset Word8
eofs = forall a. Maybe a
Nothing
                | Bool
otherwise   = let exponentNegative :: Bool
exponentNegative = forall container.
Indexable container Word8 =>
container -> Offset Word8 -> Word8 -> Bool
UTF8.expectAscii Ptr Word8
ptr Offset Word8
ofs Word8
0x2d
                                 in Bool -> Offset Word8 -> Maybe a
consumeExponantNumber Bool
exponentNegative (if Bool
exponentNegative then Offset Word8
ofs forall a. Additive a => a -> a -> a
+ Offset Word8
1 else Offset Word8
ofs)

            consumeExponantNumber :: Bool -> Offset Word8 -> Maybe a
consumeExponantNumber Bool
exponentNegative Offset Word8
ofs =
                case forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Ptr Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsPtr Int
0 Ptr Word8
ptr Offset Word8
eofs Offset Word8
ofs of
                    (# Int
acc, Bool
True, Offset Word8
endOfs #) | Offset Word8
endOfs forall a. Ord a => a -> a -> Bool
> Offset Word8
ofs -> ReadFloatingCallback a
f Bool
isNegative Natural
integral Word
floatingDigits (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! if Bool
exponentNegative then forall a. HasNegation a => a -> a
negate Int
acc else Int
acc)
                    (# Int, Bool, Offset Word8 #)
_                                      -> forall a. Maybe a
Nothing

-- | Take decimal digits and accumulate it in `acc`
--
-- The loop starts at the offset specified and finish either when:
--
-- * It reach the end of the string
-- * It reach a non-ASCII character
-- * It reach an ASCII character that is not a digit (0 to 9)
--
-- Otherwise each iterations:
--
-- * Transform the ASCII digits into a number
-- * scale the accumulator by 10
-- * Add the number (between 0 and 9) to the accumulator
--
-- It then returns:
--
-- * The new accumulated value
-- * Whether it stop by end of string or not
-- * The end offset when the loop stopped
--
-- If end offset == start offset then no digits have been consumed by
-- this function
decimalDigitsBA :: (IntegralUpsize Word8 acc, Additive acc, Multiplicative acc, Integral acc)
                => acc
                -> Block Word8
                -> Offset Word8 -- end offset
                -> Offset Word8 -- start offset
                -> (# acc, Bool, Offset Word8 #)
decimalDigitsBA :: forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Block Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsBA acc
startAcc Block Word8
ba !Offset Word8
endOfs !Offset Word8
startOfs = acc -> Offset Word8 -> (# acc, Bool, Offset Word8 #)
loop acc
startAcc Offset Word8
startOfs
  where
    loop :: acc -> Offset Word8 -> (# acc, Bool, Offset Word8 #)
loop !acc
acc !Offset Word8
ofs
        | Offset Word8
ofs forall a. Eq a => a -> a -> Bool
== Offset Word8
endOfs = (# acc
acc, Bool
True, Offset Word8
ofs #)
        | Bool
otherwise     =
            case forall container.
Indexable container Word8 =>
container -> Offset Word8 -> StepDigit
UTF8.nextAsciiDigit Block Word8
ba Offset Word8
ofs of
                sg :: StepDigit
sg@(StepDigit Word8
d) | StepDigit -> Bool
isValidStepDigit StepDigit
sg -> acc -> Offset Word8 -> (# acc, Bool, Offset Word8 #)
loop (acc
10 forall a. Multiplicative a => a -> a -> a
* acc
acc forall a. Additive a => a -> a -> a
+ forall a b. IntegralUpsize a b => a -> b
integralUpsize Word8
d) (forall a. Enum a => a -> a
succ Offset Word8
ofs)
                                 | Bool
otherwise           -> (# acc
acc, Bool
False, Offset Word8
ofs #)
{-# SPECIALIZE decimalDigitsBA :: Integer -> Block Word8 -> Offset Word8 -> Offset Word8 -> (# Integer, Bool, Offset Word8 #) #-}
{-# SPECIALIZE decimalDigitsBA :: Natural -> Block Word8 -> Offset Word8 -> Offset Word8 -> (# Natural, Bool, Offset Word8 #) #-}
{-# SPECIALIZE decimalDigitsBA :: Int -> Block Word8 -> Offset Word8 -> Offset Word8 -> (# Int, Bool, Offset Word8 #) #-}
{-# SPECIALIZE decimalDigitsBA :: Word -> Block Word8 -> Offset Word8 -> Offset Word8 -> (# Word, Bool, Offset Word8 #) #-}

-- | same as decimalDigitsBA specialized for ptr #
decimalDigitsPtr :: (IntegralUpsize Word8 acc, Additive acc, Multiplicative acc, Integral acc)
                 => acc
                 -> Ptr Word8
                 -> Offset Word8 -- end offset
                 -> Offset Word8 -- start offset
                 -> (# acc, Bool, Offset Word8 #)
decimalDigitsPtr :: forall acc.
(IntegralUpsize Word8 acc, Additive acc, Multiplicative acc,
 Integral acc) =>
acc
-> Ptr Word8
-> Offset Word8
-> Offset Word8
-> (# acc, Bool, Offset Word8 #)
decimalDigitsPtr acc
startAcc Ptr Word8
ptr !Offset Word8
endOfs !Offset Word8
startOfs = acc -> Offset Word8 -> (# acc, Bool, Offset Word8 #)
loop acc
startAcc Offset Word8
startOfs
  where
    loop :: acc -> Offset Word8 -> (# acc, Bool, Offset Word8 #)
loop !acc
acc !Offset Word8
ofs
        | Offset Word8
ofs forall a. Eq a => a -> a -> Bool
== Offset Word8
endOfs = (# acc
acc, Bool
True, Offset Word8
ofs #)
        | Bool
otherwise     =
            case forall container.
Indexable container Word8 =>
container -> Offset Word8 -> StepDigit
UTF8.nextAsciiDigit Ptr Word8
ptr Offset Word8
ofs of
                sg :: StepDigit
sg@(StepDigit Word8
d) | StepDigit -> Bool
isValidStepDigit StepDigit
sg -> acc -> Offset Word8 -> (# acc, Bool, Offset Word8 #)
loop (acc
10 forall a. Multiplicative a => a -> a -> a
* acc
acc forall a. Additive a => a -> a -> a
+ forall a b. IntegralUpsize a b => a -> b
integralUpsize Word8
d) (forall a. Enum a => a -> a
succ Offset Word8
ofs)
                                 | Bool
otherwise           -> (# acc
acc, Bool
False, Offset Word8
ofs #)
{-# SPECIALIZE decimalDigitsPtr :: Integer -> Ptr Word8 -> Offset Word8 -> Offset Word8 -> (# Integer, Bool, Offset Word8 #) #-}
{-# SPECIALIZE decimalDigitsPtr :: Natural -> Ptr Word8 -> Offset Word8 -> Offset Word8 -> (# Natural, Bool, Offset Word8 #) #-}
{-# SPECIALIZE decimalDigitsPtr :: Int -> Ptr Word8 -> Offset Word8 -> Offset Word8 -> (# Int, Bool, Offset Word8 #) #-}
{-# SPECIALIZE decimalDigitsPtr :: Word -> Ptr Word8 -> Offset Word8 -> Offset Word8 -> (# Word, Bool, Offset Word8 #) #-}

-- | Convert a 'String' 'Char' by 'Char' using a case mapping function.
caseConvert :: (Char7 -> Char7) -> (Char -> CM) -> String -> String
caseConvert :: (Char7 -> Char7) -> (Char -> CM) -> String -> String
caseConvert Char7 -> Char7
opASCII Char -> CM
op s :: String
s@(String UArray Word8
arr) = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
  MutableBlock Word8 s
mba <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
CountOf ty -> prim (MutableBlock ty (PrimState prim))
MBLK.new CountOf Word8
iLen
  CountOf Word8
nL <- forall (prim :: * -> *) ty a.
PrimMonad prim =>
(Block ty -> prim a)
-> (FinalPtr ty -> prim a) -> UArray ty -> prim a
C.onBackendPrim
        (\Block Word8
blk  -> forall container (prim :: * -> *).
(Indexable container Word8, PrimMonad prim) =>
MutableBlock Word8 (PrimState prim)
-> container
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
go MutableBlock Word8 s
mba Block Word8
blk (forall ty. Int -> Offset ty
Offset Int
0) Offset Word8
start)
        (\FinalPtr Word8
fptr -> forall (prim :: * -> *) p a.
PrimMonad prim =>
FinalPtr p -> (Ptr p -> prim a) -> prim a
withFinalPtr FinalPtr Word8
fptr forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr -> forall container (prim :: * -> *).
(Indexable container Word8, PrimMonad prim) =>
MutableBlock Word8 (PrimState prim)
-> container
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
go MutableBlock Word8 s
mba Ptr Word8
ptr (forall ty. Int -> Offset ty
Offset Int
0) Offset Word8
start)
        UArray Word8
arr
  forall (prim :: * -> *).
PrimMonad prim =>
MutableString (PrimState prim) -> prim String
freeze forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall st. MUArray Word8 st -> MutableString st
MutableString forall a b. (a -> b) -> a -> b
$ forall ty st.
Offset ty -> CountOf ty -> MUArrayBackend ty st -> MUArray ty st
MVec.MUArray Offset Word8
0 CountOf Word8
nL (forall ty st. MutableBlock ty st -> MUArrayBackend ty st
C.MUArrayMBA MutableBlock Word8 s
mba)
  where
    !(C.ValidRange Offset Word8
start Offset Word8
end) = forall ty. UArray ty -> ValidRange ty
C.offsetsValidRange UArray Word8
arr
    !iLen :: CountOf Word8
iLen = CountOf Word8
1 forall a. Additive a => a -> a -> a
+ forall ty. UArray ty -> CountOf ty
C.length UArray Word8
arr
    go :: (Indexable container Word8, PrimMonad prim)
       => MutableBlock Word8 (PrimState prim)
       -> container
       -> Offset Word8
       -> Offset Word8
       -> prim (CountOf Word8)
    go :: forall container (prim :: * -> *).
(Indexable container Word8, PrimMonad prim) =>
MutableBlock Word8 (PrimState prim)
-> container
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
go !MutableBlock Word8 (PrimState prim)
dst !container
src = MutableBlock Word8 (PrimState prim)
-> CountOf Word8
-> CountOf Word8
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
loop MutableBlock Word8 (PrimState prim)
dst CountOf Word8
iLen CountOf Word8
0
      where
        eSize :: Char -> CountOf Word8
eSize !Char
e = if Char
e forall a. Eq a => a -> a -> Bool
== Char
'\0' then CountOf Word8
0 else Int -> CountOf Word8
charToBytes (forall a. Enum a => a -> Int
fromEnum Char
e)
        loop :: MutableBlock Word8 (PrimState prim)
-> CountOf Word8
-> CountOf Word8
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
loop !MutableBlock Word8 (PrimState prim)
dst !CountOf Word8
allocLen !CountOf Word8
nLen !Offset Word8
dstIdx !Offset Word8
srcIdx
          | Offset Word8
srcIdx forall a. Eq a => a -> a -> Bool
== Offset Word8
end    = forall (m :: * -> *) a. Monad m => a -> m a
return CountOf Word8
nLen
          | CountOf Word8
nLen forall a. Eq a => a -> a -> Bool
== CountOf Word8
allocLen = prim (CountOf Word8)
realloc
          | StepASCII -> Bool
headerIsAscii StepASCII
h  = do
                forall (prim :: * -> *) container.
(PrimMonad prim, RandomAccess container prim Word8) =>
container -> Offset Word8 -> Char7 -> prim ()
UTF8.writeASCII MutableBlock Word8 (PrimState prim)
dst Offset Word8
dstIdx (Char7 -> Char7
opASCII forall a b. (a -> b) -> a -> b
$ Word8 -> Char7
Char7 forall a b. (a -> b) -> a -> b
$ StepASCII -> Word8
stepAsciiRawValue StepASCII
h)
                MutableBlock Word8 (PrimState prim)
-> CountOf Word8
-> CountOf Word8
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
loop MutableBlock Word8 (PrimState prim)
dst CountOf Word8
allocLen (CountOf Word8
nLen forall a. Additive a => a -> a -> a
+ CountOf Word8
1) (Offset Word8
dstIdxforall a. Additive a => a -> a -> a
+forall ty. Int -> Offset ty
Offset Int
1) (Offset Word8
srcIdxforall a. Additive a => a -> a -> a
+forall ty. Int -> Offset ty
Offset Int
1)
          | Bool
otherwise = do
              let !(CM Char
c1 Char
c2 Char
c3) = Char -> CM
op Char
c
                  !(Step Char
c Offset Word8
nextSrcIdx) = forall container.
Indexable container Word8 =>
StepASCII -> container -> Offset Word8 -> Step
UTF8.nextWith StepASCII
h container
src (Offset Word8
srcIdxforall a. Additive a => a -> a -> a
+forall ty. Int -> Offset ty
Offset Int
1)
              Offset Word8
nextDstIdx <- forall (prim :: * -> *) container.
(PrimMonad prim, RandomAccess container prim Word8) =>
container -> Offset Word8 -> Char -> prim (Offset Word8)
UTF8.writeUTF8 MutableBlock Word8 (PrimState prim)
dst Offset Word8
dstIdx Char
c1
              if Char
c2 forall a. Eq a => a -> a -> Bool
== Char
'\0' -- We keep the most common case loop as short as possible.
                then MutableBlock Word8 (PrimState prim)
-> CountOf Word8
-> CountOf Word8
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
loop MutableBlock Word8 (PrimState prim)
dst CountOf Word8
allocLen (CountOf Word8
nLen forall a. Additive a => a -> a -> a
+ Int -> CountOf Word8
charToBytes (forall a. Enum a => a -> Int
fromEnum Char
c1)) Offset Word8
nextDstIdx Offset Word8
nextSrcIdx
                else do
                  let !cSize :: CountOf Word8
cSize = Char -> CountOf Word8
eSize Char
c1 forall a. Additive a => a -> a -> a
+ Char -> CountOf Word8
eSize Char
c2 forall a. Additive a => a -> a -> a
+ Char -> CountOf Word8
eSize Char
c3
                  Offset Word8
nextDstIdx <- forall (prim :: * -> *) container.
(PrimMonad prim, RandomAccess container prim Word8) =>
container -> Offset Word8 -> Char -> prim (Offset Word8)
UTF8.writeUTF8 MutableBlock Word8 (PrimState prim)
dst Offset Word8
nextDstIdx Char
c2
                  Offset Word8
nextDstIdx <- if Char
c3 forall a. Eq a => a -> a -> Bool
== Char
'\0' then forall (m :: * -> *) a. Monad m => a -> m a
return Offset Word8
nextDstIdx else forall (prim :: * -> *) container.
(PrimMonad prim, RandomAccess container prim Word8) =>
container -> Offset Word8 -> Char -> prim (Offset Word8)
UTF8.writeUTF8 MutableBlock Word8 (PrimState prim)
dst Offset Word8
nextDstIdx Char
c3
                  MutableBlock Word8 (PrimState prim)
-> CountOf Word8
-> CountOf Word8
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
loop MutableBlock Word8 (PrimState prim)
dst CountOf Word8
allocLen (CountOf Word8
nLen forall a. Additive a => a -> a -> a
+ CountOf Word8
cSize) Offset Word8
nextDstIdx Offset Word8
nextSrcIdx
          where
            {-# NOINLINE realloc #-}
            realloc :: prim (CountOf Word8)
realloc = do
              let nAll :: CountOf Word8
nAll = CountOf Word8
allocLen forall a. Additive a => a -> a -> a
+ CountOf Word8
allocLen forall a. Additive a => a -> a -> a
+ CountOf Word8
1
              MutableBlock Word8 (PrimState prim)
nDst <- forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
CountOf ty -> prim (MutableBlock ty (PrimState prim))
MBLK.new CountOf Word8
nAll
              forall (prim :: * -> *) ty.
(PrimMonad prim, PrimType ty) =>
MutableBlock ty (PrimState prim)
-> Offset ty
-> MutableBlock ty (PrimState prim)
-> Offset ty
-> CountOf ty
-> prim ()
MBLK.unsafeCopyElements MutableBlock Word8 (PrimState prim)
nDst Offset Word8
0 MutableBlock Word8 (PrimState prim)
dst Offset Word8
0 CountOf Word8
nLen
              MutableBlock Word8 (PrimState prim)
-> CountOf Word8
-> CountOf Word8
-> Offset Word8
-> Offset Word8
-> prim (CountOf Word8)
loop MutableBlock Word8 (PrimState prim)
nDst CountOf Word8
nAll CountOf Word8
nLen Offset Word8
dstIdx Offset Word8
srcIdx
            h :: StepASCII
h = forall container.
Indexable container Word8 =>
container -> Offset Word8 -> StepASCII
UTF8.nextAscii container
src Offset Word8
srcIdx

-- | Convert a 'String' to the upper-case equivalent.
upper :: String -> String
upper :: String -> String
upper = (Char7 -> Char7) -> (Char -> CM) -> String -> String
caseConvert Char7 -> Char7
c7Upper Char -> CM
upperMapping

-- | Convert a 'String' to the upper-case equivalent.
lower :: String -> String
lower :: String -> String
lower = (Char7 -> Char7) -> (Char -> CM) -> String -> String
caseConvert Char7 -> Char7
c7Lower Char -> CM
lowerMapping

-- | Convert a 'String' to the unicode case fold equivalent.
--
-- Case folding is mostly used for caseless comparison of strings.
caseFold :: String -> String
caseFold :: String -> String
caseFold = (Char7 -> Char7) -> (Char -> CM) -> String -> String
caseConvert Char7 -> Char7
c7Upper Char -> CM
foldMapping

-- | Check whether the first string is a prefix of the second string.
isPrefixOf :: String -> String -> Bool
isPrefixOf :: String -> String -> Bool
isPrefixOf (String UArray Word8
needle) (String UArray Word8
haystack) = forall ty. PrimType ty => UArray ty -> UArray ty -> Bool
C.isPrefixOf UArray Word8
needle UArray Word8
haystack

-- | Check whether the first string is a suffix of the second string.
isSuffixOf :: String -> String -> Bool
isSuffixOf :: String -> String -> Bool
isSuffixOf (String UArray Word8
needle) (String UArray Word8
haystack)
    | CountOf Word8
needleLen forall a. Ord a => a -> a -> Bool
> CountOf Word8
hayLen = Bool
False
    | Bool
otherwise          = UArray Word8
needle forall a. Eq a => a -> a -> Bool
== forall ty. CountOf ty -> UArray ty -> UArray ty
C.revTake CountOf Word8
needleLen UArray Word8
haystack
  where
    needleLen :: CountOf Word8
needleLen = forall ty. UArray ty -> CountOf ty
C.length UArray Word8
needle
    hayLen :: CountOf Word8
hayLen    = forall ty. UArray ty -> CountOf ty
C.length UArray Word8
haystack

-- | Check whether the first string is contains within the second string.
--
-- TODO: implemented the naive way and thus terribly inefficient, reimplement properly
isInfixOf :: String -> String -> Bool
isInfixOf :: String -> String -> Bool
isInfixOf (String UArray Word8
needle) (String UArray Word8
haystack)
    = Maybe (CountOf Word8) -> UArray Word8 -> Bool
loop (CountOf Word8
hayLen forall a. Subtractive a => a -> a -> Difference a
- CountOf Word8
needleLen) UArray Word8
haystack
    where
      needleLen :: CountOf Word8
needleLen = forall ty. UArray ty -> CountOf ty
C.length UArray Word8
needle
      hayLen :: CountOf Word8
hayLen    = forall ty. UArray ty -> CountOf ty
C.length UArray Word8
haystack
      loop :: Maybe (CountOf Word8) -> UArray Word8 -> Bool
loop Maybe (CountOf Word8)
Nothing    UArray Word8
_         = Bool
False
      loop (Just CountOf Word8
cnt) UArray Word8
haystack' = UArray Word8
needle forall a. Eq a => a -> a -> Bool
== forall ty. CountOf ty -> UArray ty -> UArray ty
C.take CountOf Word8
needleLen UArray Word8
haystack' Bool -> Bool -> Bool
|| Maybe (CountOf Word8) -> UArray Word8 -> Bool
loop (CountOf Word8
cntforall a. Subtractive a => a -> a -> Difference a
-CountOf Word8
1) (forall ty. CountOf ty -> UArray ty -> UArray ty
C.drop CountOf Word8
1 UArray Word8
haystack')

-- | Try to strip a prefix from the start of a String.
--
-- If the prefix is not starting the string, then Nothing is returned,
-- otherwise the striped string is returned
stripPrefix :: String -> String -> Maybe String
stripPrefix :: String -> String -> Maybe String
stripPrefix (String UArray Word8
suffix) (String UArray Word8
arr)
    | forall ty. PrimType ty => UArray ty -> UArray ty -> Bool
C.isPrefixOf UArray Word8
suffix UArray Word8
arr = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty. CountOf ty -> UArray ty -> UArray ty
C.drop (forall ty. UArray ty -> CountOf ty
C.length UArray Word8
suffix) UArray Word8
arr
    | Bool
otherwise               = forall a. Maybe a
Nothing

-- | Try to strip a suffix from the end of a String.
--
-- If the suffix is not ending the string, then Nothing is returned,
-- otherwise the striped string is returned
stripSuffix :: String -> String -> Maybe String
stripSuffix :: String -> String -> Maybe String
stripSuffix (String UArray Word8
prefix) (String UArray Word8
arr)
    | forall ty. PrimType ty => UArray ty -> UArray ty -> Bool
C.isSuffixOf UArray Word8
prefix UArray Word8
arr = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ UArray Word8 -> String
String forall a b. (a -> b) -> a -> b
$ forall ty. CountOf ty -> UArray ty -> UArray ty
C.revDrop (forall ty. UArray ty -> CountOf ty
C.length UArray Word8
prefix) UArray Word8
arr
    | Bool
otherwise               = forall a. Maybe a
Nothing

all :: (Char -> Bool) -> String -> Bool
all :: (Char -> Bool) -> String -> Bool
all Char -> Bool
predicate (String UArray Word8
arr) = forall ty a s.
(Block ty -> a)
-> (FinalPtr ty -> Ptr ty -> ST s a) -> UArray ty -> a
C.onBackend Block Word8 -> Bool
goBA (\FinalPtr Word8
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Ptr Word8 -> Bool
goAddr) UArray Word8
arr
  where
    !(C.ValidRange Offset Word8
start Offset Word8
end) = forall ty. UArray ty -> ValidRange ty
C.offsetsValidRange UArray Word8
arr
    goBA :: Block Word8 -> Bool
goBA Block Word8
ba   = forall container.
Indexable container Word8 =>
(Char -> Bool) -> container -> Offset Word8 -> Offset Word8 -> Bool
UTF8.all Char -> Bool
predicate Block Word8
ba Offset Word8
start Offset Word8
end
    goAddr :: Ptr Word8 -> Bool
goAddr Ptr Word8
addr = forall container.
Indexable container Word8 =>
(Char -> Bool) -> container -> Offset Word8 -> Offset Word8 -> Bool
UTF8.all Char -> Bool
predicate Ptr Word8
addr Offset Word8
start Offset Word8
end

any :: (Char -> Bool) -> String -> Bool
any :: (Char -> Bool) -> String -> Bool
any Char -> Bool
predicate (String UArray Word8
arr) = forall ty a s.
(Block ty -> a)
-> (FinalPtr ty -> Ptr ty -> ST s a) -> UArray ty -> a
C.onBackend Block Word8 -> Bool
goBA (\FinalPtr Word8
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Ptr Word8 -> Bool
goAddr) UArray Word8
arr
  where
    !(C.ValidRange Offset Word8
start Offset Word8
end) = forall ty. UArray ty -> ValidRange ty
C.offsetsValidRange UArray Word8
arr
    goBA :: Block Word8 -> Bool
goBA Block Word8
ba   = forall container.
Indexable container Word8 =>
(Char -> Bool) -> container -> Offset Word8 -> Offset Word8 -> Bool
UTF8.any Char -> Bool
predicate Block Word8
ba Offset Word8
start Offset Word8
end
    goAddr :: Ptr Word8 -> Bool
goAddr Ptr Word8
addr = forall container.
Indexable container Word8 =>
(Char -> Bool) -> container -> Offset Word8 -> Offset Word8 -> Bool
UTF8.any Char -> Bool
predicate Ptr Word8
addr Offset Word8
start Offset Word8
end

-- | Transform string @src@ to base64 binary representation.
toBase64 :: String -> String
toBase64 :: String -> String
toBase64 (String UArray Word8
src) = UArray Word8 -> String
fromBytesUnsafe forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall ty.
PrimType ty =>
Addr# -> UArray ty -> Bool -> UArray Word8
Vec.toBase64Internal Addr#
set UArray Word8
src forall a b. (a -> b) -> a -> b
$ Bool
True
  where
    !set :: Addr#
set = Addr#
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"#

-- | Transform string @src@ to URL-safe base64 binary representation.
-- The result will be either padded or unpadded, depending on the boolean
-- @padded@ argument.
toBase64URL :: Bool -> String -> String
toBase64URL :: Bool -> String -> String
toBase64URL Bool
padded (String UArray Word8
src) = UArray Word8 -> String
fromBytesUnsafe forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall ty.
PrimType ty =>
Addr# -> UArray ty -> Bool -> UArray Word8
Vec.toBase64Internal Addr#
set UArray Word8
src forall a b. (a -> b) -> a -> b
$ Bool
padded
  where
    !set :: Addr#
set = Addr#
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"#

-- | Transform string @src@ to OpenBSD base64 binary representation.
toBase64OpenBSD :: String -> String
toBase64OpenBSD :: String -> String
toBase64OpenBSD (String UArray Word8
src) = UArray Word8 -> String
fromBytesUnsafe forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall ty.
PrimType ty =>
Addr# -> UArray ty -> Bool -> UArray Word8
Vec.toBase64Internal Addr#
set UArray Word8
src forall a b. (a -> b) -> a -> b
$ Bool
False
  where
    !set :: Addr#
set = Addr#
"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"#