From ebdfe326f75d7e9b0b80bd6a96ec3b5c81026ad2 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Sat, 25 Dec 2021 17:25:21 +0200 Subject: [PATCH] Support text-2.0 --- .github/workflows/haskell-ci.yml | 34 +++++++++++++++++--------------- ChangeLog.md | 4 ++++ cabal.haskell-ci | 11 +++++++++++ src/Data/Text/Short/Internal.hs | 31 ++++++++++++++++++++++++++--- text-short.cabal | 5 ++--- 5 files changed, 63 insertions(+), 22 deletions(-) diff --git a/.github/workflows/haskell-ci.yml b/.github/workflows/haskell-ci.yml index 9529a4e..722eda9 100644 --- a/.github/workflows/haskell-ci.yml +++ b/.github/workflows/haskell-ci.yml @@ -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: @@ -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 }} @@ -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 @@ -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: @@ -159,17 +161,10 @@ jobs: repository hackage.haskell.org url: http://hackage.haskell.org/ EOF - if $HEADHACKAGE; then cat >> $CABAL_CONFIG <= 80200)) -ne 0 ] ; then echo " ghc-options: -Werror=missing-methods" >> cabal.project ; fi cat >> cabal.project <> 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 @@ -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 diff --git a/ChangeLog.md b/ChangeLog.md index 6f471f2..9c3d0e7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,7 @@ +## 0.1.5 + + * text-2.0 support + ## 0.1.4 * Fix `fromString` for single character strings. diff --git a/cabal.haskell-ci b/cabal.haskell-ci index a346432..1c91460 100644 --- a/cabal.haskell-ci +++ b/cabal.haskell-ci @@ -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 diff --git a/src/Data/Text/Short/Internal.hs b/src/Data/Text/Short/Internal.hs index 277f153..ca5417a 100644 --- a/src/Data/Text/Short/Internal.hs +++ b/src/Data/Text/Short/Internal.hs @@ -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 @@ -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. @@ -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 ---- @@ -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' -- diff --git a/text-short.cabal b/text-short.cabal index b56ac92..cf46da0 100644 --- a/text-short.cabal +++ b/text-short.cabal @@ -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 @@ -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