@@ -143,7 +143,7 @@ module Data.Text
143143 , -- sort
144144 ) where
145145
146−import Prelude (Char, Bool, Int, Maybe(..), String,
146+import Prelude (Char, Bool, Functor(..), Int, Maybe(..), String,
147147 Eq, (==), (++), error,
148148 Show, showsPrec,
149149 Read, readsPrec,
@@ -276,9 +276,16 @@ head t = S.head (stream t)
276276-- | /O(1)/ Returns the first character and rest of a 'Text', or
277277-- 'Nothing' if empty. Subject to array fusion.
278278uncons :: Text -> Maybe (Char, Text)
279−uncons t = case S.uncons (stream t) of
280− Just (c, s) -> Just (c, unstream s)
281− Nothing -> Nothing
279+uncons (Text arr off len)
280+ | len <= 0 = Nothing
281+ | otherwise = Just (c, Text arr (off+k) (len-k))
282+ where c | single = unsafeChr m
283+ | otherwise = U16.chr2 m n
284+ k | single = 1
285+ | otherwise = 2
286+ single = m < 0xD800 || m > 0xDBFF
287+ m = A.unsafeIndex arr off
288+ n = A.unsafeIndex arr (off+1)
282289{-# INLINE uncons #-}
283290
284291-- | /O(n)/ Returns the last character of a 'Text', which must be