diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b79aae..8fea4b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Revision history for base62 +## 0.1.1.0 -- 2023-08-08 + +* Add `shortText128`. +* Add `text128`. +* Drop support for `text` earlier than 2.0. + ## 0.1.0.3 -- 2023-07-25 * Support GHC 9.4. Drop support for all versions of GHC before 9.4. diff --git a/base62.cabal b/base62.cabal index e91fc11..02934b7 100644 --- a/base62.cabal +++ b/base62.cabal @@ -1,6 +1,6 @@ cabal-version: 2.2 name: base62 -version: 0.1.0.3 +version: 0.1.1.0 synopsis: Base62 encoding and decoding description: Encode and decode using the base62 encoding scheme. @@ -19,10 +19,13 @@ library Data.Word.Base62 build-depends: , base >=4.17 && <5 + , bytebuild >=0.3.4 && <0.4 , byteslice >=0.2 && <0.3 + , bytestring >=0.11.4 , natural-arithmetic >=0.1 && <0.2 , primitive >=0.7 && <0.10 - , bytebuild >=0.3.4 && <0.4 + , text >=2.0.2 + , text-short >=0.1.5 , wide-word >=0.1.0.8 && <0.2 hs-source-dirs: src default-language: Haskell2010 diff --git a/src/Data/Word/Base62.hs b/src/Data/Word/Base62.hs index 5dacae2..04afb16 100644 --- a/src/Data/Word/Base62.hs +++ b/src/Data/Word/Base62.hs @@ -24,18 +24,23 @@ module Data.Word.Base62 , decode64 -- * 128-bit Word , encode128 + , shortText128 + , text128 , builder128 , decode128 ) where +import Data.ByteString.Short.Internal (ShortByteString(SBS)) import Data.Bytes.Builder.Bounded.Unsafe (Builder(..)) import Data.Bytes.Types (Bytes(Bytes)) import Data.Char (ord) import Data.Primitive (ByteArray(..),readByteArray,writeByteArray) import Data.Primitive (MutableByteArray(MutableByteArray)) +import Data.Text (Text) +import Data.Text.Short (ShortText) import Data.WideWord.Word128 (Word128(Word128)) -import GHC.Exts (Char(C#),Word64#,Word8#,quotRemWord#,indexCharArray#) import GHC.Exts (ByteArray#,Int#,Int(I#),(+#),(-#)) +import GHC.Exts (Char(C#),Word64#,Word8#,quotRemWord#,indexCharArray#) import GHC.Exts (isTrue#,(>#)) import GHC.Exts (wordToWord64#,word64ToWord#) import GHC.ST (ST(ST)) @@ -45,6 +50,8 @@ import qualified Arithmetic.Nat as Nat import qualified Data.Bytes as Bytes import qualified Data.Bytes.Builder.Bounded as Builder import qualified GHC.Exts as Exts +import qualified Data.Text.Short as TS +import qualified Data.Text.Short.Unsafe as TS -- $setup -- >>> :set -XNumericUnderscores @@ -71,8 +78,20 @@ builder64 (W64# w) = builder64# w -- >>> putStrLn (Bytes.toLatinString (Bytes.fromByteArray (encode128 octillion))) -- 1IdHllabYuAOlNK4 encode128 :: Word128 -> ByteArray +{-# inline encode128 #-} encode128 = Builder.run Nat.constant . builder128 +-- | Base62 encode a 128-bit word as @ShortText@. +shortText128 :: Word128 -> ShortText +{-# inline shortText128 #-} +shortText128 !w = case encode128 w of + ByteArray x -> TS.fromShortByteStringUnsafe (SBS x) + +-- | Base62 encode a 128-bit word as @Text@. +text128 :: Word128 -> Text +{-# inline text128 #-} +text128 = TS.toText . shortText128 + -- | Base62 encode a 128-bit word as a builder. builder128 :: Word128 -> Builder 22 {-# inline builder128 #-}