Skip to content

Commit

Permalink
Add (#<|) and deprecate its alias (<|#)
Browse files Browse the repository at this point in the history
Previously the documentation stated that `(#<|)` was not supported by the
parser. It turns out it is a silly interaction with `-XUnboxedTuples`.
Adding extra spacing between the parentheses and the function fixes the
issue.

This commit adds `(#<|)` and makes `(<|#)` its deprecated alias.
  • Loading branch information
wismill committed Sep 5, 2023
1 parent d8ae7d8 commit b833a7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/Data/Text/Builder/Linear/Buffer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Data.Text.Builder.Linear.Buffer (

-- ** Raw 'Addr#'
(|>#),
( #<| ), -- NOTE: extra spaces required because of -XUnboxedTuples
(<|#),

-- * Number formatting
Expand Down Expand Up @@ -75,7 +76,7 @@ buffer |> (Text src srcOff srcLen) =
buffer

-- | Prepend 'Text' prefix to a 'Buffer' by mutating it.
-- If a prefix is statically known, consider using '(<|#)' for optimal performance.
-- If a prefix is statically known, consider using '(#<|)' for optimal performance.
--
-- >>> :set -XOverloadedStrings -XLinearTypes
-- >>> runBuffer (\b -> "foo" <| "bar" <| b)
Expand All @@ -98,8 +99,6 @@ Text src srcOff srcLen <| buffer =
--
-- The literal string must not contain zero bytes @\\0@ and must be a valid UTF-8,
-- these conditions are not checked.
--
-- Note the inconsistency in naming: unfortunately, GHC parser does not allow for @#<|@.
(|>#) Buffer Addr# Buffer

infixl 6 |>#
Expand All @@ -115,22 +114,34 @@ buffer |># addr# =
-- to a 'Buffer' by mutating it. E. g.,
--
-- >>> :set -XOverloadedStrings -XLinearTypes -XMagicHash
-- >>> runBuffer (\b -> "foo"# <|# "bar"# <|# b)
-- >>> runBuffer (\b -> "foo"# #<| "bar"# #<| b)
-- "foobar"
--
-- The literal string must not contain zero bytes @\\0@ and must be a valid UTF-8,
-- these conditions are not checked.
(<|#) Addr# Buffer Buffer

infixr 6 <|#
addr# <|# buffer =
--
-- /Note:/ When the syntactic extensions @UnboxedTuples@ or @UnboxedSums@ are
-- enabled, extra spaces are required when using parentheses: i.e. use @( '#<|' )@
-- instead of @('#<|')@. See the GHC User Guide chapter
-- “[Unboxed types and primitive operations](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/primitives.html#unboxed-tuples)”
-- for futher information.
( #<| ) Addr# Buffer Buffer

infixr 6 #<|, <|#
addr# #<| buffer =
prependExact
srcLen
(\dst dstOff A.copyFromPointer dst dstOff (Ptr addr#) srcLen)
buffer
where
srcLen = I# (cstringLength# addr#)

-- | Alias for @'(#<|)'@.
{-# DEPRECATED (<|#) "Use '(#<|)' instead" #-}
(<|#) Addr# Buffer Buffer
(<|#) = ( #<| ) -- NOTE: extra spaces required because of -XUnboxedTuples
{-# INLINE (<|#) #-}

-- | Append given number of spaces.
(|>…) Buffer Word Buffer

Expand Down
2 changes: 1 addition & 1 deletion test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ prop4 acts = runBuffer f1 === runBuffer f2
where
addr# = "foo"#
f1, f2 :: Buffer Buffer
f1 = \b addr# <|# interpretOnBuffer acts b
f1 = \b addr# #<| interpretOnBuffer acts b
f2 = \b T.pack "foo" <| interpretOnBuffer acts b

prop5 [Action] Property
Expand Down

0 comments on commit b833a7f

Please sign in to comment.