mirror of
https://github.com/tfausak/witch.git
synced 2024-11-25 22:13:49 +03:00
Add UTF-16LE encoding
This commit is contained in:
parent
9512493c21
commit
2b22b915c5
@ -9,3 +9,6 @@ type ISO_8859_1 = Tagged.Tagged "ISO-8859-1"
|
||||
|
||||
-- | <https://en.wikipedia.org/wiki/UTF-8>
|
||||
type UTF_8 = Tagged.Tagged "UTF-8"
|
||||
|
||||
-- | <https://en.wikipedia.org/wiki/UTF-16>
|
||||
type UTF_16LE = Tagged.Tagged "UTF-16LE"
|
||||
|
@ -31,6 +31,7 @@ import qualified Data.Set as Set
|
||||
import qualified Data.Tagged as Tagged
|
||||
import qualified Data.Text as Text
|
||||
import qualified Data.Text.Encoding as Text
|
||||
import qualified Data.Text.Encoding.Error as Text
|
||||
import qualified Data.Text.Lazy as LazyText
|
||||
import qualified Data.Text.Lazy.Encoding as LazyText
|
||||
import qualified Data.Time as Time
|
||||
@ -41,6 +42,7 @@ import qualified Data.Word as Word
|
||||
import qualified GHC.Float as Float
|
||||
import qualified Numeric
|
||||
import qualified Numeric.Natural as Natural
|
||||
import qualified System.IO.Unsafe as Unsafe
|
||||
import qualified Witch.Encoding as Encoding
|
||||
import qualified Witch.From as From
|
||||
import qualified Witch.TryFrom as TryFrom
|
||||
@ -1334,6 +1336,59 @@ instance From.From String (Encoding.UTF_8 ByteString.ByteString) where
|
||||
instance From.From String (Encoding.UTF_8 LazyByteString.ByteString) where
|
||||
from = Utility.via @LazyText.Text
|
||||
|
||||
-- UTF-16LE
|
||||
|
||||
-- | Uses 'Text.decodeUtf16LE'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16LE ByteString.ByteString) Text.Text where
|
||||
tryFrom = Utility.eitherTryFrom $ todo @Text.UnicodeException . Text.decodeUtf16LE . From.from
|
||||
|
||||
todo :: Exception.Exception e => a -> Either e a
|
||||
todo = Unsafe.unsafePerformIO . Exception.try . Exception.evaluate
|
||||
|
||||
-- | Converts via 'Text.Text'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16LE ByteString.ByteString) LazyText.Text where
|
||||
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @LazyText.Text) . Utility.tryInto @Text.Text
|
||||
|
||||
-- | Converts via 'Text.Text'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16LE ByteString.ByteString) String where
|
||||
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @Text.Text
|
||||
|
||||
-- | Uses 'LazyText.decodeUtf16LE'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16LE LazyByteString.ByteString) LazyText.Text where
|
||||
tryFrom = Utility.eitherTryFrom $ todo @Text.UnicodeException . LazyText.decodeUtf16LE . From.from
|
||||
|
||||
-- | Converts via 'LazyText.Text'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16LE LazyByteString.ByteString) Text.Text where
|
||||
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @Text.Text) . Utility.tryInto @LazyText.Text
|
||||
|
||||
-- | Converts via 'LazyText.Text'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16LE LazyByteString.ByteString) String where
|
||||
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @LazyText.Text
|
||||
|
||||
-- | Uses 'Text.encodeUtf16LE'.
|
||||
instance From.From Text.Text (Encoding.UTF_16LE ByteString.ByteString) where
|
||||
from = From.from . Text.encodeUtf16LE
|
||||
|
||||
-- | Converts via 'ByteString.ByteString'.
|
||||
instance From.From Text.Text (Encoding.UTF_16LE LazyByteString.ByteString) where
|
||||
from = fmap From.from . Utility.into @(Encoding.UTF_16LE ByteString.ByteString)
|
||||
|
||||
-- | Uses 'LazyText.encodeUtf16LE'.
|
||||
instance From.From LazyText.Text (Encoding.UTF_16LE LazyByteString.ByteString) where
|
||||
from = From.from . LazyText.encodeUtf16LE
|
||||
|
||||
-- | Converts via 'LazyByteString.ByteString'.
|
||||
instance From.From LazyText.Text (Encoding.UTF_16LE ByteString.ByteString) where
|
||||
from = fmap From.from . Utility.into @(Encoding.UTF_16LE LazyByteString.ByteString)
|
||||
|
||||
-- | Converts via 'Text.Text'.
|
||||
instance From.From String (Encoding.UTF_16LE ByteString.ByteString) where
|
||||
from = Utility.via @Text.Text
|
||||
|
||||
-- | Converts via 'LazyText.Text'.
|
||||
instance From.From String (Encoding.UTF_16LE LazyByteString.ByteString) where
|
||||
from = Utility.via @LazyText.Text
|
||||
|
||||
--
|
||||
|
||||
realFloatToRational ::
|
||||
|
@ -2162,6 +2162,75 @@ spec = describe "Witch" $ do
|
||||
f "" `shouldBe` Tagged.Tagged (LazyByteString.pack [])
|
||||
f "a" `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61])
|
||||
|
||||
describe "TryFrom (UTF_16LE ByteString) Text" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16LE ByteString.ByteString) @Text.Text
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (ByteString.pack [])) `shouldBe` Just (Text.pack "")
|
||||
f (Tagged.Tagged (ByteString.pack [0x24, 0x00])) `shouldBe` Just (Text.pack "\x24")
|
||||
f (Tagged.Tagged (ByteString.pack [0xa3, 0x00])) `shouldBe` Just (Text.pack "\xa3")
|
||||
f (Tagged.Tagged (ByteString.pack [0xac, 0x20])) `shouldBe` Just (Text.pack "\x20ac")
|
||||
f (Tagged.Tagged (ByteString.pack [0x00, 0xd8, 0x48, 0xdf])) `shouldBe` Just (Text.pack "\x10348")
|
||||
f (Tagged.Tagged (ByteString.pack [0x00])) `shouldBe` Nothing
|
||||
|
||||
describe "TryFrom (UTF_16LE ByteString) LazyText" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16LE ByteString.ByteString) @LazyText.Text
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (ByteString.pack [0x61, 0x00])) `shouldBe` Just (LazyText.pack "a")
|
||||
|
||||
describe "TryFrom (UTF_16LE ByteString) String" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16LE ByteString.ByteString) @String
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (ByteString.pack [0x61, 0x00])) `shouldBe` Just "a"
|
||||
|
||||
describe "TryFrom (UTF_16LE LazyByteString) LazyText" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16LE LazyByteString.ByteString) @LazyText.Text
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (LazyByteString.pack [0x61, 0x00])) `shouldBe` Just (LazyText.pack "a")
|
||||
|
||||
describe "TryFrom (UTF_16LE LazyByteString) Text" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16LE LazyByteString.ByteString) @Text.Text
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (LazyByteString.pack [0x61, 0x00])) `shouldBe` Just (Text.pack "a")
|
||||
|
||||
describe "TryFrom (UTF_16LE LazyByteString) String" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16LE LazyByteString.ByteString) @String
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (LazyByteString.pack [0x61, 0x00])) `shouldBe` Just "a"
|
||||
|
||||
describe "From Text (UTF_16LE ByteString)" $ do
|
||||
let f = Witch.from @Text.Text @(Encoding.UTF_16LE ByteString.ByteString)
|
||||
it "works" $ do
|
||||
f (Text.pack "") `shouldBe` Tagged.Tagged (ByteString.pack [])
|
||||
f (Text.pack "\x24") `shouldBe` Tagged.Tagged (ByteString.pack [0x24, 0x00])
|
||||
f (Text.pack "\xa3") `shouldBe` Tagged.Tagged (ByteString.pack [0xa3, 0x00])
|
||||
f (Text.pack "\x20ac") `shouldBe` Tagged.Tagged (ByteString.pack [0xac, 0x20])
|
||||
f (Text.pack "\x10348") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0xd8, 0x48, 0xdf])
|
||||
|
||||
describe "From Text (UTF_16LE LazyByteString)" $ do
|
||||
let f = Witch.from @Text.Text @(Encoding.UTF_16LE LazyByteString.ByteString)
|
||||
it "works" $ do
|
||||
f (Text.pack "a") `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61, 0x00])
|
||||
|
||||
describe "From LazyText (UTF_16LE LazyByteString)" $ do
|
||||
let f = Witch.from @LazyText.Text @(Encoding.UTF_16LE LazyByteString.ByteString)
|
||||
it "works" $ do
|
||||
f (LazyText.pack "a") `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61, 0x00])
|
||||
|
||||
describe "From LazyText (UTF_16LE ByteString)" $ do
|
||||
let f = Witch.from @LazyText.Text @(Encoding.UTF_16LE ByteString.ByteString)
|
||||
it "works" $ do
|
||||
f (LazyText.pack "a") `shouldBe` Tagged.Tagged (ByteString.pack [0x61, 0x00])
|
||||
|
||||
describe "From String (UTF_16LE ByteString)" $ do
|
||||
let f = Witch.from @String @(Encoding.UTF_16LE ByteString.ByteString)
|
||||
it "works" $ do
|
||||
f "a" `shouldBe` Tagged.Tagged (ByteString.pack [0x61, 0x00])
|
||||
|
||||
describe "From String (UTF_16LE LazyByteString)" $ do
|
||||
let f = Witch.from @String @(Encoding.UTF_16LE LazyByteString.ByteString)
|
||||
it "works" $ do
|
||||
f "a" `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61, 0x00])
|
||||
|
||||
newtype Age
|
||||
= Age Int.Int8
|
||||
deriving (Eq, Show)
|
||||
|
Loading…
Reference in New Issue
Block a user