2020-03-12 23:02:45 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
{-|
|
|
|
|
Atom implementation with fast conversions between bytestrings
|
|
|
|
and atoms.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Urbit.Atom
|
2020-03-07 01:10:58 +03:00
|
|
|
( Atom
|
|
|
|
, atomBytes
|
|
|
|
, bytesAtom
|
|
|
|
, atomWords
|
|
|
|
, wordsAtom
|
|
|
|
, utf8Atom
|
|
|
|
, atomUtf8
|
|
|
|
, atomUtf8Exn
|
|
|
|
, atomUtf8Lenient
|
|
|
|
)
|
|
|
|
where
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import Data.Vector.Primitive (Vector)
|
|
|
|
import GHC.Natural (Natural)
|
|
|
|
|
2020-03-12 23:02:45 +03:00
|
|
|
#if defined(__GHCJS__)
|
|
|
|
import Urbit.Atom.Slow (atomBytes, bytesAtom)
|
|
|
|
#else
|
|
|
|
import Urbit.Atom.Fast (atomBytes, bytesAtom)
|
|
|
|
#endif
|
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as T
|
|
|
|
import qualified Data.Text.Encoding.Error as T
|
2020-03-12 23:02:45 +03:00
|
|
|
|
|
|
|
#if defined(__GHCJS__)
|
|
|
|
import qualified Urbit.Atom.Slow as A
|
|
|
|
#else
|
|
|
|
import qualified Urbit.Atom.Fast as A
|
|
|
|
#endif
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Atom = Natural
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Cast an atom to a vector. Does not copy.
|
2020-03-07 01:10:58 +03:00
|
|
|
atomWords :: Atom -> Vector Word
|
2020-03-12 23:02:45 +03:00
|
|
|
atomWords = A.atomWords
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
-- | Cast a vector to an atom. Does not copy unless given a slice.
|
2020-03-07 01:10:58 +03:00
|
|
|
wordsAtom :: Vector Word -> Atom
|
2020-03-12 23:02:45 +03:00
|
|
|
wordsAtom = A.wordsAtom
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
-- | Encode a utf8-encoded atom from text.
|
2020-03-07 01:10:58 +03:00
|
|
|
utf8Atom :: T.Text -> Atom
|
2020-03-12 23:02:45 +03:00
|
|
|
utf8Atom = A.bytesAtom . T.encodeUtf8
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
-- | Interpret an atom as utf8 text.
|
2020-03-07 01:10:58 +03:00
|
|
|
atomUtf8 :: Atom -> Either T.UnicodeException T.Text
|
2020-01-23 12:22:30 +03:00
|
|
|
atomUtf8 = T.decodeUtf8' . atomBytes
|
|
|
|
|
|
|
|
-- | Interpret an atom as utf8 text, throwing an exception on bad unicode.
|
2020-03-07 01:10:58 +03:00
|
|
|
atomUtf8Exn :: Atom -> T.Text
|
2020-01-23 12:22:30 +03:00
|
|
|
atomUtf8Exn = T.decodeUtf8 . atomBytes
|
|
|
|
|
|
|
|
-- | Interpret an atom as utf8 text, replacing bad unicode characters.
|
2020-03-07 01:10:58 +03:00
|
|
|
atomUtf8Lenient :: Atom -> T.Text
|
2020-01-23 12:22:30 +03:00
|
|
|
atomUtf8Lenient = T.decodeUtf8With T.lenientDecode . atomBytes
|