----------------------------------------------------------------------------
-- |
-- Module      :  Data.Emacs.Module.SymbolName.Internal
-- Copyright   :  (c) Sergey Vinokurov 2018
-- License     :  Apache-2.0 (see LICENSE)
-- Maintainer  :  serg.foo@gmail.com
----------------------------------------------------------------------------

{-# LANGUAGE ImportQualifiedPost #-}

{-# OPTIONS_HADDOCK not-home #-}

module Data.Emacs.Module.SymbolName.Internal
  ( SymbolName(..)
  , mkSymbolName
  , mkSymbolNameShortByteString
  , useSymbolNameAsCString
  ) where

import Data.ByteString.Char8 qualified as C8
import Data.ByteString.Short qualified as BSS
import Data.ByteString.Unsafe qualified as C8.Unsafe
import Data.Text.Encoding qualified as TE
import Data.Text.Encoding.Error qualified as TE
import Foreign.C.String
import Prettyprinter

newtype SymbolName = SymbolName { SymbolName -> ByteString
unSymbolName :: C8.ByteString }
  deriving (SymbolName -> SymbolName -> Bool
(SymbolName -> SymbolName -> Bool)
-> (SymbolName -> SymbolName -> Bool) -> Eq SymbolName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SymbolName -> SymbolName -> Bool
$c/= :: SymbolName -> SymbolName -> Bool
== :: SymbolName -> SymbolName -> Bool
$c== :: SymbolName -> SymbolName -> Bool
Eq, Eq SymbolName
Eq SymbolName
-> (SymbolName -> SymbolName -> Ordering)
-> (SymbolName -> SymbolName -> Bool)
-> (SymbolName -> SymbolName -> Bool)
-> (SymbolName -> SymbolName -> Bool)
-> (SymbolName -> SymbolName -> Bool)
-> (SymbolName -> SymbolName -> SymbolName)
-> (SymbolName -> SymbolName -> SymbolName)
-> Ord SymbolName
SymbolName -> SymbolName -> Bool
SymbolName -> SymbolName -> Ordering
SymbolName -> SymbolName -> SymbolName
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 :: SymbolName -> SymbolName -> SymbolName
$cmin :: SymbolName -> SymbolName -> SymbolName
max :: SymbolName -> SymbolName -> SymbolName
$cmax :: SymbolName -> SymbolName -> SymbolName
>= :: SymbolName -> SymbolName -> Bool
$c>= :: SymbolName -> SymbolName -> Bool
> :: SymbolName -> SymbolName -> Bool
$c> :: SymbolName -> SymbolName -> Bool
<= :: SymbolName -> SymbolName -> Bool
$c<= :: SymbolName -> SymbolName -> Bool
< :: SymbolName -> SymbolName -> Bool
$c< :: SymbolName -> SymbolName -> Bool
compare :: SymbolName -> SymbolName -> Ordering
$ccompare :: SymbolName -> SymbolName -> Ordering
$cp1Ord :: Eq SymbolName
Ord, Int -> SymbolName -> ShowS
[SymbolName] -> ShowS
SymbolName -> String
(Int -> SymbolName -> ShowS)
-> (SymbolName -> String)
-> ([SymbolName] -> ShowS)
-> Show SymbolName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SymbolName] -> ShowS
$cshowList :: [SymbolName] -> ShowS
show :: SymbolName -> String
$cshow :: SymbolName -> String
showsPrec :: Int -> SymbolName -> ShowS
$cshowsPrec :: Int -> SymbolName -> ShowS
Show)

instance Pretty SymbolName where
  pretty :: SymbolName -> Doc ann
pretty = Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Text -> Doc ann) -> (SymbolName -> Text) -> SymbolName -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnDecodeError -> ByteString -> Text
TE.decodeUtf8With OnDecodeError
TE.lenientDecode (ByteString -> Text)
-> (SymbolName -> ByteString) -> SymbolName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
C8.init (ByteString -> ByteString)
-> (SymbolName -> ByteString) -> SymbolName -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymbolName -> ByteString
unSymbolName

{-# INLINE mkSymbolName #-}
mkSymbolName :: C8.ByteString -> SymbolName
mkSymbolName :: ByteString -> SymbolName
mkSymbolName = ByteString -> SymbolName
SymbolName (ByteString -> SymbolName)
-> (ByteString -> ByteString) -> ByteString -> SymbolName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Char -> ByteString
`C8.snoc` Char
'\0')

{-# INLINE mkSymbolNameShortByteString #-}
mkSymbolNameShortByteString :: BSS.ShortByteString -> SymbolName
mkSymbolNameShortByteString :: ShortByteString -> SymbolName
mkSymbolNameShortByteString = ByteString -> SymbolName
mkSymbolName (ByteString -> SymbolName)
-> (ShortByteString -> ByteString) -> ShortByteString -> SymbolName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
BSS.fromShort

{-# INLINE useSymbolNameAsCString #-}
useSymbolNameAsCString :: SymbolName -> (CString -> IO a) -> IO a
useSymbolNameAsCString :: SymbolName -> (CString -> IO a) -> IO a
useSymbolNameAsCString = ByteString -> (CString -> IO a) -> IO a
forall a. ByteString -> (CString -> IO a) -> IO a
C8.Unsafe.unsafeUseAsCString (ByteString -> (CString -> IO a) -> IO a)
-> (SymbolName -> ByteString)
-> SymbolName
-> (CString -> IO a)
-> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymbolName -> ByteString
unSymbolName