mirror of
https://github.com/tfausak/witch.git
synced 2024-11-22 14:58:13 +03:00
Merge pull request #70 from tfausak/gh-66-utf-16be
Add UTF-16BE encoding
This commit is contained in:
commit
e5b9cbf8e2
@ -12,3 +12,6 @@ type UTF_8 = Tagged.Tagged "UTF-8"
|
||||
|
||||
-- | <https://en.wikipedia.org/wiki/UTF-16>
|
||||
type UTF_16LE = Tagged.Tagged "UTF-16LE"
|
||||
|
||||
-- | <https://en.wikipedia.org/wiki/UTF-16>
|
||||
type UTF_16BE = Tagged.Tagged "UTF-16BE"
|
||||
|
@ -1386,6 +1386,56 @@ instance From.From String (Encoding.UTF_16LE ByteString.ByteString) where
|
||||
instance From.From String (Encoding.UTF_16LE LazyByteString.ByteString) where
|
||||
from = Utility.via @LazyText.Text
|
||||
|
||||
-- UTF-16BE
|
||||
|
||||
-- | Uses 'Text.decodeUtf16BE'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16BE ByteString.ByteString) Text.Text where
|
||||
tryFrom = Utility.eitherTryFrom $ tryEvaluate @Text.UnicodeException . Text.decodeUtf16BE . From.from
|
||||
|
||||
-- | Converts via 'Text.Text'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16BE 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_16BE ByteString.ByteString) String where
|
||||
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @Text.Text
|
||||
|
||||
-- | Uses 'LazyText.decodeUtf16BE'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16BE LazyByteString.ByteString) LazyText.Text where
|
||||
tryFrom = Utility.eitherTryFrom $ tryEvaluate @Text.UnicodeException . LazyText.decodeUtf16BE . From.from
|
||||
|
||||
-- | Converts via 'LazyText.Text'.
|
||||
instance TryFrom.TryFrom (Encoding.UTF_16BE 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_16BE LazyByteString.ByteString) String where
|
||||
tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @LazyText.Text
|
||||
|
||||
-- | Uses 'Text.encodeUtf16BE'.
|
||||
instance From.From Text.Text (Encoding.UTF_16BE ByteString.ByteString) where
|
||||
from = From.from . Text.encodeUtf16BE
|
||||
|
||||
-- | Converts via 'ByteString.ByteString'.
|
||||
instance From.From Text.Text (Encoding.UTF_16BE LazyByteString.ByteString) where
|
||||
from = fmap From.from . Utility.into @(Encoding.UTF_16BE ByteString.ByteString)
|
||||
|
||||
-- | Uses 'LazyText.encodeUtf16BE'.
|
||||
instance From.From LazyText.Text (Encoding.UTF_16BE LazyByteString.ByteString) where
|
||||
from = From.from . LazyText.encodeUtf16BE
|
||||
|
||||
-- | Converts via 'LazyByteString.ByteString'.
|
||||
instance From.From LazyText.Text (Encoding.UTF_16BE ByteString.ByteString) where
|
||||
from = fmap From.from . Utility.into @(Encoding.UTF_16BE LazyByteString.ByteString)
|
||||
|
||||
-- | Converts via 'Text.Text'.
|
||||
instance From.From String (Encoding.UTF_16BE ByteString.ByteString) where
|
||||
from = Utility.via @Text.Text
|
||||
|
||||
-- | Converts via 'LazyText.Text'.
|
||||
instance From.From String (Encoding.UTF_16BE LazyByteString.ByteString) where
|
||||
from = Utility.via @LazyText.Text
|
||||
|
||||
--
|
||||
|
||||
realFloatToRational ::
|
||||
|
@ -2231,6 +2231,75 @@ spec = describe "Witch" $ do
|
||||
it "works" $ do
|
||||
f "a" `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61, 0x00])
|
||||
|
||||
describe "TryFrom (UTF_16BE ByteString) Text" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16BE ByteString.ByteString) @Text.Text
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (ByteString.pack [])) `shouldBe` Just (Text.pack "")
|
||||
f (Tagged.Tagged (ByteString.pack [0x00, 0x24])) `shouldBe` Just (Text.pack "\x24")
|
||||
f (Tagged.Tagged (ByteString.pack [0x00, 0xa3])) `shouldBe` Just (Text.pack "\xa3")
|
||||
f (Tagged.Tagged (ByteString.pack [0x20, 0xac])) `shouldBe` Just (Text.pack "\x20ac")
|
||||
f (Tagged.Tagged (ByteString.pack [0xd8, 0x00, 0xdf, 0x48])) `shouldBe` Just (Text.pack "\x10348")
|
||||
f (Tagged.Tagged (ByteString.pack [0x00])) `shouldBe` Nothing
|
||||
|
||||
describe "TryFrom (UTF_16BE ByteString) LazyText" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16BE ByteString.ByteString) @LazyText.Text
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (ByteString.pack [0x00, 0x61])) `shouldBe` Just (LazyText.pack "a")
|
||||
|
||||
describe "TryFrom (UTF_16BE ByteString) String" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16BE ByteString.ByteString) @String
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (ByteString.pack [0x00, 0x61])) `shouldBe` Just "a"
|
||||
|
||||
describe "TryFrom (UTF_16BE LazyByteString) LazyText" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16BE LazyByteString.ByteString) @LazyText.Text
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (LazyByteString.pack [0x00, 0x61])) `shouldBe` Just (LazyText.pack "a")
|
||||
|
||||
describe "TryFrom (UTF_16BE LazyByteString) Text" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16BE LazyByteString.ByteString) @Text.Text
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (LazyByteString.pack [0x00, 0x61])) `shouldBe` Just (Text.pack "a")
|
||||
|
||||
describe "TryFrom (UTF_16BE LazyByteString) String" $ do
|
||||
let f = hush . Witch.tryFrom @(Encoding.UTF_16BE LazyByteString.ByteString) @String
|
||||
it "works" $ do
|
||||
f (Tagged.Tagged (LazyByteString.pack [0x00, 0x61])) `shouldBe` Just "a"
|
||||
|
||||
describe "From Text (UTF_16BE ByteString)" $ do
|
||||
let f = Witch.from @Text.Text @(Encoding.UTF_16BE ByteString.ByteString)
|
||||
it "works" $ do
|
||||
f (Text.pack "") `shouldBe` Tagged.Tagged (ByteString.pack [])
|
||||
f (Text.pack "\x24") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x24])
|
||||
f (Text.pack "\xa3") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0xa3])
|
||||
f (Text.pack "\x20ac") `shouldBe` Tagged.Tagged (ByteString.pack [0x20, 0xac])
|
||||
f (Text.pack "\x10348") `shouldBe` Tagged.Tagged (ByteString.pack [0xd8, 0x00, 0xdf, 0x48])
|
||||
|
||||
describe "From Text (UTF_16BE LazyByteString)" $ do
|
||||
let f = Witch.from @Text.Text @(Encoding.UTF_16BE LazyByteString.ByteString)
|
||||
it "works" $ do
|
||||
f (Text.pack "a") `shouldBe` Tagged.Tagged (LazyByteString.pack [0x00, 0x61])
|
||||
|
||||
describe "From LazyText (UTF_16BE LazyByteString)" $ do
|
||||
let f = Witch.from @LazyText.Text @(Encoding.UTF_16BE LazyByteString.ByteString)
|
||||
it "works" $ do
|
||||
f (LazyText.pack "a") `shouldBe` Tagged.Tagged (LazyByteString.pack [0x00, 0x61])
|
||||
|
||||
describe "From LazyText (UTF_16BE ByteString)" $ do
|
||||
let f = Witch.from @LazyText.Text @(Encoding.UTF_16BE ByteString.ByteString)
|
||||
it "works" $ do
|
||||
f (LazyText.pack "a") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x61])
|
||||
|
||||
describe "From String (UTF_16BE ByteString)" $ do
|
||||
let f = Witch.from @String @(Encoding.UTF_16BE ByteString.ByteString)
|
||||
it "works" $ do
|
||||
f "a" `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x61])
|
||||
|
||||
describe "From String (UTF_16BE LazyByteString)" $ do
|
||||
let f = Witch.from @String @(Encoding.UTF_16BE LazyByteString.ByteString)
|
||||
it "works" $ do
|
||||
f "a" `shouldBe` Tagged.Tagged (LazyByteString.pack [0x00, 0x61])
|
||||
|
||||
newtype Age
|
||||
= Age Int.Int8
|
||||
deriving (Eq, Show)
|
||||
|
Loading…
Reference in New Issue
Block a user