Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support text-2.0 #234

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hashable.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ library
, containers >=0.4.2.1 && <0.7
, deepseq >=1.3 && <1.5
, ghc-prim
, text >=0.12 && <1.3
, text >=0.12 && <2.1

if !impl(ghc >=9.2)
build-depends: base-orphans >=0.8.6
Expand Down
18 changes: 18 additions & 0 deletions src/Data/Hashable/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,22 @@ instance Hashable BSI.ShortByteString where
hashByteArrayWithSalt ba 0 (BSI.length sbs) (hashWithSalt salt (BSI.length sbs))
#endif

#if MIN_VERSION_text(2,0,0)

instance Hashable T.Text where
hashWithSalt salt (T.Text (TA.ByteArray arr) off len) =
hashByteArrayWithSalt arr off len (hashWithSalt salt len)

instance Hashable TL.Text where
hashWithSalt salt = finalise . TL.foldlChunks step (SP salt 0)
where
finalise (SP s l) = hashWithSalt s l
step (SP s l) (T.Text (TA.ByteArray arr) off len) = SP
(hashByteArrayWithSalt arr off len s)
(l + len)

#else

instance Hashable T.Text where
hashWithSalt salt (T.Text arr off len) =
hashByteArrayWithSalt (TA.aBA arr) (off `shiftL` 1) (len `shiftL` 1)
Expand All @@ -735,6 +751,8 @@ instance Hashable TL.Text where
(hashByteArrayWithSalt (TA.aBA arr) (off `shiftL` 1) (len `shiftL` 1) s)
(l + len)

#endif

-- | Compute the hash of a ThreadId.
hashThreadId :: ThreadId -> Int
hashThreadId (ThreadId t) = hash (fromIntegral (getThreadId t) :: Int)
Expand Down
7 changes: 6 additions & 1 deletion tests/Regress.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ regressions = [] ++
hs @=? nub hs
#if WORD_SIZE_IN_BITS == 64
, testCase "64 bit Text" $ do
hash ("hello world" :: Text) @=? -3875242662334356092
hash ("hello world" :: Text) @=?
#if MIN_VERSION_text(2,0,0)
4078614214911247440
#else
-3875242662334356092
#endif
#endif
, F.testGroup "concatenation"
[ testCase "String" $ do
Expand Down