double-x-encoding 1.1.0.0 → 1.1.1
raw patch · 2 files changed
+54/−12 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- DoubleXEncoding: parseHex :: Text -> Int
Files
- double-x-encoding.cabal +1/−1
- source/DoubleXEncoding.hs +53/−11
double-x-encoding.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: double-x-encoding-version: 1.1.0.0+version: 1.1.1 synopsis: Encoding scheme to encode any Unicode string with only [0-9a-zA-Z_] description: Double-X-Encoding is an encoding scheme to encode any Unicode string with only characters from [0-9a-zA-Z_].
source/DoubleXEncoding.hs view
@@ -1,6 +1,19 @@--- | Implementation of double-X-encoder and -decoder in Haskell+{- | Implementation of Double-X-Encoding encoder and decoder in Haskell++Main functions:++* `doubleXEncode`+* `doubleXEncodeGql`+* `doubleXDecode`+-}+ {- ORMOLU_DISABLE -} +{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE MultiWayIf #-}++ module DoubleXEncoding where import Data.Function ((&))@@ -13,7 +26,7 @@ import Control.Monad (join) --- | charEncode mapping in Haskell+-- | Encoder mapping for ASCII characters. charEncode :: Char -> Char charEncode = \case ' ' -> '0'@@ -61,6 +74,7 @@ _ -> '\0' +-- | Decoder mapping for ASCII characters. charDecode :: Char -> Char charDecode = \case '0' -> ' '@@ -106,6 +120,8 @@ _ -> '\0' +-- | Map hex characters to an alternative hex alphabet+-- ranging from a to p instead of 0 to f. hexShiftEncode :: Char -> Char hexShiftEncode = \case '0' -> 'a'@@ -128,6 +144,7 @@ _ -> '\0' +-- | Map alternative hex alphabet back to the original hex alphabet. hexShiftDecode :: Char -> Char hexShiftDecode = \case 'a' -> '0'@@ -150,12 +167,24 @@ _ -> '\0' +{-|+ Options for encoding:++ [`encodeLeadingDigit`]: Encode the leading digit of the input string++ [`encodeDoubleUnderscore`]: Encode double underscores `__` as `XXRXXR`++ Especially relevant for GraphQL, as+ [the spec](https://spec.graphql.org/October2021/#sec-Names)+ does not allow leading digits or double underscores for field names.+-} data EncodeOptions = EncodeOptions { encodeLeadingDigit :: Bool , encodeDoubleUnderscore :: Bool } +-- | Encode a text using the Double-X-Encoding algorithm with provided options. doubleXEncodeWithOptions :: EncodeOptions -> Text -> Text doubleXEncodeWithOptions encodeOptions text = let@@ -213,8 +242,8 @@ encodeStandard text --+-- | Default options with no leading digit encoding+-- and no double underscore encoding. defaultOptions :: EncodeOptions defaultOptions = EncodeOptions { encodeLeadingDigit = False@@ -222,11 +251,17 @@ } +-- | Encode a text using the Double-X-Encoding algorithm.+--+-- >>> doubleXEncode "id-with.special$chars!"+-- "idXXDwithXXEspecialXX4charsXX1" doubleXEncode :: Text -> Text doubleXEncode = doubleXEncodeWithOptions defaultOptions +-- | Default options for GraphQL encoding.+-- Leading digits or double underscores are not allowed for field names. gqlOptions :: EncodeOptions gqlOptions = EncodeOptions { encodeLeadingDigit = True@@ -234,21 +269,28 @@ } +-- | Encode a text using the Double-X-Encoding algorithm with GraphQL options.+--+-- >>> doubleXEncodeGql "1FileFormat__"+-- "XXZ1FileFormatXXRXXR" doubleXEncodeGql :: Text -> Text doubleXEncodeGql = doubleXEncodeWithOptions gqlOptions -parseHex :: Text -> Int-parseHex text =- case readHex (T.unpack text) of- [(int, "")] -> int- _ -> 0--+-- | Decode a Double-X-Encoding encoded text.+--+-- >>> doubleXDecode "idXXDwithXXEspecialXX4charsXX1"+-- "id-with.special$chars!" doubleXDecode :: Text -> Text doubleXDecode text = let+ parseHex :: Text -> Int+ parseHex text =+ case readHex (T.unpack text) of+ [(int, "")] -> int+ _ -> 0+ decodeWord :: Text -> (Text, Text) decodeWord word = let