Commit 351f320e authored by Toralf Wittner's avatar Toralf Wittner
Browse files

Add `Semigroup` instance for `Builder`.

See issue #1.
parent 3a7bc8e1
Loading
Loading
Loading
Loading
+18 −15
Original line number Diff line number Diff line
variables:
  STACK_ROOT: "${CI_PROJECT_DIR}/.stack"

cache:
  key: "$CI_JOB_NAME"
  paths:
    - .stack

before_script:
  - apt-get update
  - apt-get install -y libstdc++-4.9-dev g++
  - cabal update
  - apt -qq update
  - apt -qq install libstdc++-4.9-dev g++
  - stack upgrade && hash -d stack && stack --version
  - stack clean --full

test:7.10:
  image: haskell:7.10
test:8.2:
  image: haskell:8.2
  script:
    - cabal install --enable-test --enable-bench --only-dep -j
    - cabal build
    - cabal test
    - cabal bench
    - stack -j 1 --resolver lts-11.1 test --fast

test:7.8:
  image: haskell:7.8
test:8.0:
  image: haskell:8.0
  script:
    - cabal install --enable-test --enable-bench --only-dep -j
    - cabal build
    - cabal test
    - cabal bench
    - stack -j 1 --resolver lts-9.21 test --fast
+27 −5
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}

@@ -29,10 +30,17 @@ module System.Logger.Message
    , render
    ) where

#if MIN_VERSION_base(4,9,0)
import Data.Semigroup as Sem
#endif

#if !(MIN_VERSION_base(4,8,0))
import Data.Monoid
#endif

import Data.ByteString (ByteString)
import Data.Double.Conversion.Text
import Data.Int
import Data.Monoid
import Data.String
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
@@ -49,13 +57,27 @@ import qualified Data.Text.Lazy.Encoding as TL

data Builder = Builder !Int B.Builder

instance Monoid Builder where
    mempty = Builder 0 mempty
    (Builder x a) `mappend` (Builder y b) = Builder (x + y) (a <> b)

instance IsString Builder where
    fromString = bytes

appendBuilder:: Builder -> Builder -> Builder
appendBuilder (Builder x a) (Builder y b) = Builder (x + y) (a <> b)

#if MIN_VERSION_base(4,9,0)
instance Sem.Semigroup Builder where
    (<>) = appendBuilder
#endif

instance Monoid Builder where
    mempty = Builder 0 mempty
#if MIN_VERSION_base(4,11,0)
    -- mappend definitions are redundant now
#elif MIN_VERSION_base(4,9,0)
    mappend = (Sem.<>)
#else
    mappend = appendBuilder
#endif

eval :: Builder -> L.ByteString
eval (Builder n b) = B.toLazyByteStringWith (B.safeStrategy n 256) L.empty b