Skip to content

Commit

Permalink
Add aliases for encodings (#104)
Browse files Browse the repository at this point in the history
* Add aliases for encodings

* Use shorter aliases for encodings
  • Loading branch information
tfausak authored Oct 22, 2024
1 parent 309ae27 commit 7e7cd44
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 226 deletions.
31 changes: 31 additions & 0 deletions source/library/Witch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,42 @@ module Witch
Witch.TryFromException.TryFromException (..),

-- ** Encodings

-- | Witch supports decoding text and encoding byte strings using various
-- character encodings. For example to convert from a 'String' into a
-- strict 'Data.ByteString.ByteString', you can use the
-- 'Witch.Encoding.Utf8S' alias:
--
-- @
-- 'Witch.Utility.into' @'Data.ByteString.ByteString' ('Witch.Utility.into' @'Witch.Encoding.Utf8S' ("..." :: 'String'))
-- -- "..."
-- @
--
-- And to convert from a strict 'Data.ByteString.ByteString' into a
-- 'String', you can use the 'Witch.Encoding.Utf8S' alias in the other
-- direction:
--
-- @
-- 'Witch.Utility.tryInto' @'String' ('Witch.Utility.into' @'Witch.Encoding.Utf8S' ("..." :: 'Data.ByteString.ByteString'))
-- -- Right "..."
-- @
Witch.Encoding.Latin1S,
Witch.Encoding.Latin1L,
Witch.Encoding.ISO_8859_1,
Witch.Encoding.Utf8S,
Witch.Encoding.Utf8L,
Witch.Encoding.UTF_8,
Witch.Encoding.Utf16LS,
Witch.Encoding.Utf16LL,
Witch.Encoding.UTF_16LE,
Witch.Encoding.Utf16BS,
Witch.Encoding.Utf16BL,
Witch.Encoding.UTF_16BE,
Witch.Encoding.Utf32LS,
Witch.Encoding.Utf32LL,
Witch.Encoding.UTF_32LE,
Witch.Encoding.Utf32BS,
Witch.Encoding.Utf32BL,
Witch.Encoding.UTF_32BE,

-- * Utilities
Expand Down
38 changes: 38 additions & 0 deletions source/library/Witch/Encoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,60 @@

module Witch.Encoding where

import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LazyByteString
import qualified Data.Tagged as Tagged

-- | <https://en.wikipedia.org/wiki/ISO/IEC_8859-1>
type ISO_8859_1 = Tagged.Tagged "ISO-8859-1"

-- | The 'ISO_8859_1' encoding for strict 'ByteString.ByteString's.
type Latin1S = ISO_8859_1 ByteString.ByteString

-- | The 'ISO_8859_1' encoding for lazy 'LazyByteString.ByteString's.
type Latin1L = ISO_8859_1 LazyByteString.ByteString

-- | <https://en.wikipedia.org/wiki/UTF-8>
type UTF_8 = Tagged.Tagged "UTF-8"

-- | The 'UTF_8' encoding for strict 'ByteString.ByteString's.
type Utf8S = UTF_8 ByteString.ByteString

-- | The 'UTF_8' encoding for lazy 'LazyByteString.ByteString's.
type Utf8L = UTF_8 LazyByteString.ByteString

-- | <https://en.wikipedia.org/wiki/UTF-16>
type UTF_16LE = Tagged.Tagged "UTF-16LE"

-- | The 'UTF_16LE' encoding for strict 'ByteString.ByteString's.
type Utf16LS = UTF_16LE ByteString.ByteString

-- | The 'UTF_16LE' encoding for lazy 'LazyByteString.ByteString's.
type Utf16LL = UTF_16LE LazyByteString.ByteString

-- | <https://en.wikipedia.org/wiki/UTF-16>
type UTF_16BE = Tagged.Tagged "UTF-16BE"

-- | The 'UTF_16BE' encoding for strict 'ByteString.ByteString's.
type Utf16BS = UTF_16BE ByteString.ByteString

-- | The 'UTF_16BE' encoding for lazy 'LazyByteString.ByteString's.
type Utf16BL = UTF_16BE LazyByteString.ByteString

-- | <https://en.wikipedia.org/wiki/UTF-32>
type UTF_32LE = Tagged.Tagged "UTF-32LE"

-- | The 'UTF_32LE' encoding for strict 'ByteString.ByteString's.
type Utf32LS = UTF_32LE ByteString.ByteString

-- | The 'UTF_32LE' encoding for lazy 'LazyByteString.ByteString's.
type Utf32LL = UTF_32LE LazyByteString.ByteString

-- | <https://en.wikipedia.org/wiki/UTF-32>
type UTF_32BE = Tagged.Tagged "UTF-32BE"

-- | The 'UTF_32BE' encoding for strict 'ByteString.ByteString's.
type Utf32BS = UTF_32BE ByteString.ByteString

-- | The 'UTF_32BE' encoding for lazy 'LazyByteString.ByteString's.
type Utf32BL = UTF_32BE LazyByteString.ByteString
Loading

0 comments on commit 7e7cd44

Please sign in to comment.