{-# LANGUAGE UnliftedFFITypes #-}

module Jsonifier.Size where

import qualified Jsonifier.Ffi as Ffi
import Jsonifier.Prelude
import qualified Jsonifier.Text as Text

{-# INLINE object #-}
object :: Int -> Int -> Int
object :: Int -> Int -> Int
object Int
rowsAmount Int
contentsSize =
  forall {a}. Num a => a
curlies forall a. Num a => a -> a -> a
+ Int -> Int
commas Int
rowsAmount forall a. Num a => a -> a -> a
+ Int
colonsAndQuotes forall a. Num a => a -> a -> a
+ Int
contentsSize
  where
    curlies :: a
curlies =
      a
2
    colonsAndQuotes :: Int
colonsAndQuotes =
      Int
rowsAmount forall a. Num a => a -> a -> a
* Int
3

{-# INLINE array #-}
array :: Int -> Int -> Int
array :: Int -> Int -> Int
array Int
elementsAmount Int
contentsSize =
  forall {a}. Num a => a
brackets forall a. Num a => a -> a -> a
+ Int -> Int
commas Int
elementsAmount forall a. Num a => a -> a -> a
+ Int
contentsSize
  where
    brackets :: a
brackets =
      a
2

{-# INLINE commas #-}
commas :: Int -> Int
commas :: Int -> Int
commas Int
rowsAmount =
  if Int
rowsAmount forall a. Ord a => a -> a -> Bool
<= Int
1
    then Int
0
    else forall a. Enum a => a -> a
pred Int
rowsAmount

-- |
-- Amount of bytes required for an escaped JSON string value without quotes.
stringBody :: Text -> Int
stringBody :: Text -> Int
stringBody =
  forall x. (ByteArray# -> Int -> Int -> x) -> Text -> x
Text.destruct forall a b. (a -> b) -> a -> b
$ \ByteArray#
arr Int
off Int
len ->
    ByteArray# -> CSize -> CSize -> IO CInt
Ffi.countTextEncoding
      ByteArray#
arr
      (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
off)
      (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
      forall a b. a -> (a -> b) -> b
& forall a. IO a -> a
unsafeDupablePerformIO
      forall a b. a -> (a -> b) -> b
& forall a b. (Integral a, Num b) => a -> b
fromIntegral