Skip to content

Commit

Permalink
Merge pull request haskell-hvr#32 from haskell-hvr/text-2
Browse files Browse the repository at this point in the history
Support text-2.0
  • Loading branch information
phadej authored Dec 25, 2021
2 parents 609b3b1 + ebdfe32 commit f037c38
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 22 deletions.
34 changes: 18 additions & 16 deletions .github/workflows/haskell-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#
# For more information, see https://github.com/haskell-CI/haskell-ci
#
# version: 0.13.20211029
# version: 0.13.20211116
#
# REGENDATA ("0.13.20211029",["github","cabal.project"])
# REGENDATA ("0.13.20211116",["github","cabal.project"])
#
name: Haskell-CI
on:
Expand All @@ -24,6 +24,8 @@ jobs:
linux:
name: Haskell-CI - Linux - ${{ matrix.compiler }}
runs-on: ubuntu-18.04
timeout-minutes:
60
container:
image: buildpack-deps:bionic
continue-on-error: ${{ matrix.allow-failure }}
Expand All @@ -34,7 +36,7 @@ jobs:
compilerKind: ghc
compilerVersion: 9.2.1
setup-method: ghcup
allow-failure: true
allow-failure: false
- compiler: ghc-9.0.1
compilerKind: ghc
compilerVersion: 9.0.1
Expand Down Expand Up @@ -130,7 +132,7 @@ jobs:
echo "HCNUMVER=$HCNUMVER" >> "$GITHUB_ENV"
echo "ARG_TESTS=--enable-tests" >> "$GITHUB_ENV"
echo "ARG_BENCH=--enable-benchmarks" >> "$GITHUB_ENV"
if [ $((HCNUMVER >= 90200)) -ne 0 ] ; then echo "HEADHACKAGE=true" >> "$GITHUB_ENV" ; else echo "HEADHACKAGE=false" >> "$GITHUB_ENV" ; fi
echo "HEADHACKAGE=false" >> "$GITHUB_ENV"
echo "ARG_COMPILER=--$HCKIND --with-compiler=$HC" >> "$GITHUB_ENV"
echo "GHCJSARITH=0" >> "$GITHUB_ENV"
env:
Expand Down Expand Up @@ -159,17 +161,10 @@ jobs:
repository hackage.haskell.org
url: http://hackage.haskell.org/
EOF
if $HEADHACKAGE; then
cat >> $CABAL_CONFIG <<EOF
repository head.hackage.ghc.haskell.org
url: https://ghc.gitlab.haskell.org/head.hackage/
secure: True
root-keys: 7541f32a4ccca4f97aea3b22f5e593ba2c0267546016b992dfadcd2fe944e55d
26021a13b401500c8eb2761ca95c61f2d625bfef951b939a8124ed12ecf07329
f76d08be13e9a61a377a85e2fb63f4c5435d40f8feb3e12eb05905edb8cdea89
key-threshold: 3
program-default-options
ghc-options: $GHCJOBS +RTS -M3G -RTS
EOF
fi
cat $CABAL_CONFIG
- name: versions
run: |
Expand Down Expand Up @@ -217,9 +212,6 @@ jobs:
if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo " ghc-options: -Werror=missing-methods" >> cabal.project ; fi
cat >> cabal.project <<EOF
EOF
if $HEADHACKAGE; then
echo "allow-newer: $($HCPKG list --simple-output | sed -E 's/([a-zA-Z-]+)-[0-9.]+/*:\1,/g')" >> cabal.project
fi
$HCPKG list --simple-output --names-only | perl -ne 'for (split /\s+/) { print "constraints: $_ installed\n" unless /^(text-short)$/; }' >> cabal.project.local
cat cabal.project
cat cabal.project.local
Expand Down Expand Up @@ -260,6 +252,16 @@ jobs:
- name: prepare for constraint sets
run: |
rm -f cabal.project.local
- name: constraint set text-2
run: |
if [ $((HCNUMVER >= 80000)) -ne 0 ] ; then $CABAL v2-build $ARG_COMPILER --enable-tests --disable-benchmarks --constraint='text ^>=2' --dependencies-only -j2 all ; fi
if [ $((HCNUMVER >= 80000)) -ne 0 ] ; then $CABAL v2-build $ARG_COMPILER --enable-tests --disable-benchmarks --constraint='text ^>=2' all ; fi
if [ $((HCNUMVER >= 80000)) -ne 0 ] ; then $CABAL v2-test $ARG_COMPILER --enable-tests --disable-benchmarks --constraint='text ^>=2' all ; fi
- name: constraint set text-1
run: |
$CABAL v2-build $ARG_COMPILER --enable-tests --disable-benchmarks --constraint='text ^>=1.2.3.0' --dependencies-only -j2 all
$CABAL v2-build $ARG_COMPILER --enable-tests --disable-benchmarks --constraint='text ^>=1.2.3.0' all
$CABAL v2-test $ARG_COMPILER --enable-tests --disable-benchmarks --constraint='text ^>=1.2.3.0' all
- name: constraint set bytestring-0.11
run: |
$CABAL v2-build $ARG_COMPILER --enable-tests --disable-benchmarks --constraint='bytestring ==0.11.*' --dependencies-only -j2 all
Expand Down
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.5

* text-2.0 support

## 0.1.4

* Fix `fromString` for single character strings.
Expand Down
11 changes: 11 additions & 0 deletions cabal.haskell-ci
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ constraint-set bytestring-0.11
constraints: bytestring ==0.11.*
tests: True
run-tests: True

constraint-set text-1
constraints: text ^>=1.2.3.0
tests: True
run-tests: True

constraint-set text-2
ghc: >= 8.0
constraints: text ^>=2
tests: True
run-tests: True
31 changes: 28 additions & 3 deletions src/Data/Text/Short/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ import Data.Maybe (fromMaybe, isNothing)
import Data.Semigroup
import qualified Data.String as S
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Foreign.C
import GHC.Base (assert, unsafeChr)
import qualified GHC.CString as GHC
Expand All @@ -149,6 +148,13 @@ import Text.Printf (PrintfArg, formatArg,

import qualified Language.Haskell.TH.Syntax as TH

#if MIN_VERSION_text(2,0,0)
import qualified Data.Text.Internal as TI
import qualified Data.Text.Array as TA
#else
import qualified Data.Text.Encoding as T
#endif

import qualified PrimOps

-- | A compact representation of Unicode strings.
Expand Down Expand Up @@ -653,12 +659,16 @@ foldr1 f st
--
-- prop> (toText . fromText) t == t
--
-- This is currently not \(\mathcal{O}(1)\) because currently 'T.Text' uses UTF-16 as its internal representation.
-- In the event that 'T.Text' will change its internal representation to UTF-8 this operation will become \(\mathcal{O}(1)\).
-- This is \(\mathcal{O}(1)\) with @text-2@.
-- Previously it wasn't because 'T.Text' used UTF-16 as its internal representation.
--
-- @since 0.1
toText :: ShortText -> T.Text
#if MIN_VERSION_text(2,0,0)
toText (ShortText (BSSI.SBS ba)) = TI.Text (TA.ByteArray ba) 0 (I# (GHC.Exts.sizeofByteArray# ba))
#else
toText = T.decodeUtf8 . toByteString
#endif

----

Expand Down Expand Up @@ -692,7 +702,22 @@ fromString s = case s of
--
-- @since 0.1
fromText :: T.Text -> ShortText
#if MIN_VERSION_text(2,0,0)
fromText (TI.Text (TA.ByteArray ba) off len) =
ShortText (BSSI.SBS (case sliceByteArray (TA.ByteArray ba) off len of TA.ByteArray ba' -> ba'))

sliceByteArray :: TA.Array -> Int -> Int -> TA.Array
sliceByteArray ta@(TA.ByteArray ba) 0 len
| len == I# (GHC.Exts.sizeofByteArray# ba)
= ta
sliceByteArray ta off len = TA.run $ do
ma <- TA.new len
TA.copyI len ma 0 ta off
return ma

#else
fromText = fromByteStringUnsafe . T.encodeUtf8
#endif

-- | \(\mathcal{O}(n)\) Construct 'ShortText' from UTF-8 encoded 'ShortByteString'
--
Expand Down
5 changes: 2 additions & 3 deletions text-short.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cabal-version: 1.18
name: text-short
version: 0.1.4
x-revision: 2
version: 0.1.5
synopsis: Memory-efficient representation of Unicode text strings
license: BSD3
license-file: LICENSE
Expand Down Expand Up @@ -37,7 +36,7 @@ library
, bytestring >= 0.10.4 && < 0.12
, hashable >= 1.2.6 && < 1.5
, deepseq >= 1.3 && < 1.5
, text >= 1.0 && < 1.3
, text >= 1.0 && < 1.3 || >=2.0 && <2.1
, binary >= 0.7.1 && < 0.9
, ghc-prim >= 0.3.1 && < 0.9
, template-haskell >= 2.9.0.0 && <2.19
Expand Down

0 comments on commit f037c38

Please sign in to comment.