diff --git a/source/library/Witch/Encoding.hs b/source/library/Witch/Encoding.hs index 6cfa9b8..141b6e4 100644 --- a/source/library/Witch/Encoding.hs +++ b/source/library/Witch/Encoding.hs @@ -12,3 +12,6 @@ type UTF_8 = Tagged.Tagged "UTF-8" -- | type UTF_16LE = Tagged.Tagged "UTF-16LE" + +-- | +type UTF_16BE = Tagged.Tagged "UTF-16BE" diff --git a/source/library/Witch/Instances.hs b/source/library/Witch/Instances.hs index c262e14..90de460 100644 --- a/source/library/Witch/Instances.hs +++ b/source/library/Witch/Instances.hs @@ -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 :: diff --git a/source/test-suite/Main.hs b/source/test-suite/Main.hs index 65fbe49..e1269a9 100644 --- a/source/test-suite/Main.hs +++ b/source/test-suite/Main.hs @@ -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)