From d1d49170e0b7b6da0f3351e4841582c2553fd324 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Fri, 6 May 2016 10:27:16 +0000 Subject: [PATCH] Text-ByteString UTF8 conversion utility modules --- src/Data/Text/Lazy/UTF8/Local.hs | 67 ++++++++++++++++++++++++++++++++ src/Data/Text/UTF8/Local.hs | 66 +++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/Data/Text/Lazy/UTF8/Local.hs create mode 100644 src/Data/Text/UTF8/Local.hs diff --git a/src/Data/Text/Lazy/UTF8/Local.hs b/src/Data/Text/Lazy/UTF8/Local.hs new file mode 100644 index 0000000..79cc973 --- /dev/null +++ b/src/Data/Text/Lazy/UTF8/Local.hs @@ -0,0 +1,67 @@ +{- This file is part of Vervis. + - + - Written in 2016 by fr33domlover . + - + - ♡ Copying is an act of love. Please copy, reuse and share. + - + - The author(s) have dedicated all copyright and related and neighboring + - rights to this software to the public domain worldwide. This software is + - distributed without any warranty. + - + - You should have received a copy of the CC0 Public Domain Dedication along + - with this software. If not, see + - . + -} + +-- | Utilities for conversion between lazy 'Text' and UTF8-encoded lazy +-- 'ByteString' +module Data.Text.Lazy.UTF8.Local + ( encode + , decodeStrict + , decodeLenient + , encodeFilename + , encodeSource + , decodeFilename + , decodeSource + ) +where + +import Prelude () + +import Data.ByteString.Lazy (ByteString) +import Data.Text.Lazy (Text) + +import qualified Data.Text.Lazy.Encoding as TLE +import qualified Data.Text.Encoding.Error as TEE + +encode :: Text -> ByteString +encode = TLE.encodeUtf8 + +decodeStrict :: ByteString -> Text +decodeStrict = TLE.decodeUtf8With TEE.strictDecode + +decodeLenient :: ByteString -> Text +decodeLenient = TLE.decodeUtf8With TEE.lenientDecode + +-- | Encode text in a way appropriate for filenames. This is simply set to +-- 'encode'. +encodeFilename :: Text -> ByteString +encodeFilename = encode + +-- | Encode text in a way appropriate for source content. This is simply set to +-- 'encode'. +encodeSource :: Text -> ByteString +encodeSource = encode + +-- | Decode text in a way appropriate for filenames. Since these names may be +-- used for reading and writing to the file system, errors here must not be +-- ignored, therefore the conversion is strict. +decodeFilename :: ByteString -> Text +decodeFilename = decodeStrict + +-- | Encode text in a way appropriate for source content. Even in the case of +-- an encoding error, the application shouldn't fail. It should still display +-- the content, so that the valid parts are visible and the error too is +-- visible to the user. Therefore the conversion is lenient. +decodeSource :: ByteString -> Text +decodeSource = decodeLenient diff --git a/src/Data/Text/UTF8/Local.hs b/src/Data/Text/UTF8/Local.hs new file mode 100644 index 0000000..23f207d --- /dev/null +++ b/src/Data/Text/UTF8/Local.hs @@ -0,0 +1,66 @@ +{- This file is part of Vervis. + - + - Written in 2016 by fr33domlover . + - + - ♡ Copying is an act of love. Please copy, reuse and share. + - + - The author(s) have dedicated all copyright and related and neighboring + - rights to this software to the public domain worldwide. This software is + - distributed without any warranty. + - + - You should have received a copy of the CC0 Public Domain Dedication along + - with this software. If not, see + - . + -} + +-- | Utilities for conversion between 'Text' and UTF8-encoded 'ByteString' +module Data.Text.UTF8.Local + ( encode + , decodeStrict + , decodeLenient + , encodeFilename + , encodeSource + , decodeFilename + , decodeSource + ) +where + +import Prelude () + +import Data.ByteString (ByteString) +import Data.Text (Text) + +import qualified Data.Text.Encoding as TE +import qualified Data.Text.Encoding.Error as TEE + +encode :: Text -> ByteString +encode = TE.encodeUtf8 + +decodeStrict :: ByteString -> Text +decodeStrict = TE.decodeUtf8With TEE.strictDecode + +decodeLenient :: ByteString -> Text +decodeLenient = TE.decodeUtf8With TEE.lenientDecode + +-- | Encode text in a way appropriate for filenames. This is simply set to +-- 'encode'. +encodeFilename :: Text -> ByteString +encodeFilename = encode + +-- | Encode text in a way appropriate for source content. This is simply set to +-- 'encode'. +encodeSource :: Text -> ByteString +encodeSource = encode + +-- | Decode text in a way appropriate for filenames. Since these names may be +-- used for reading and writing to the file system, errors here must not be +-- ignored, therefore the conversion is strict. +decodeFilename :: ByteString -> Text +decodeFilename = decodeStrict + +-- | Encode text in a way appropriate for source content. Even in the case of +-- an encoding error, the application shouldn't fail. It should still display +-- the content, so that the valid parts are visible and the error too is +-- visible to the user. Therefore the conversion is lenient. +decodeSource :: ByteString -> Text +decodeSource = decodeLenient