@@ -45,7 +45,7 @@ module Data.Text.Lazy
@@ -151,6 +151,7 @@ import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
151 Read(..), Show(..),
151 Read(..), Show(..),
152 (&&), (||), (+), (-), (.), ($),
152 (&&), (||), (+), (-), (.), ($),
153 not, return, otherwise)
153 not, return, otherwise)
154+import qualified Prelude as P
154import Data.String (IsString(..))
155import Data.String (IsString(..))
155import qualified Data.Text as T
156import qualified Data.Text as T
156import qualified Data.Text.Fusion as S
157import qualified Data.Text.Fusion as S
@@ -250,6 +251,22 @@ head :: Text -> Char
250head t = S.head (stream t)
251head t = S.head (stream t)
251{-# INLINE head #-}
252{-# INLINE head #-}
254+-- | /O(1)/ Returns the last character of a 'Text', which must be
255+-- non-empty. Subject to array fusion.
257+last Empty = emptyError "last"
258+last (Chunk t ts) = go t ts
259+ where go _ (Chunk t' ts') = go t' ts'
260+ go t Empty = T.last t
261+{-# INLINE [1] last #-}
264+"LAZY TEXT last -> fused" [~1] forall t.
265+ last t = S.last (stream t)
266+"LAZY TEXT last -> unfused" [1] forall t.
267+ S.last (stream t) = last t
253-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
270-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
254-- prefix of @t@ of length @n@, and whose second is the remainder of
271-- prefix of @t@ of length @n@, and whose second is the remainder of
255-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
272-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
@@ -263,3 +280,6 @@ splitAt = loop
263 | otherwise = let (ts',ts'') = loop (n - len) ts
280 | otherwise = let (ts',ts'') = loop (n - len) ts
264 in (Chunk t ts', ts'')
281 in (Chunk t ts', ts'')
265 where len = T.length t
282 where len = T.length t
284+emptyError :: String -> a
285+emptyError fun = P.error ("Data.Text.Lazy." ++ fun ++ ": empty input")