Skip to content

Commit

Permalink
Add text128 and shortText128
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewthad committed Aug 8, 2023
1 parent 11f407e commit c75b657
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
7 changes: 5 additions & 2 deletions base62.cabal
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
21 changes: 20 additions & 1 deletion src/Data/Word/Base62.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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 #-}
Expand Down

0 comments on commit c75b657

Please sign in to comment.