Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Data/Text/Internal/Transformation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Data.Text.Internal.Transformation
, toLowerNonEmpty
, toUpperNonEmpty
, toTitleNonEmpty
, toTitleNonEmptyFrom
, filter_
) where

Expand Down Expand Up @@ -193,20 +194,29 @@ isAsciiSpace w = w .&. 0x50 == 0 && w < 0x80 && (w == 0x20 || w - 0x09 < 5)
-- | /O(n)/ Convert a string to title case, see 'Data.Text.toTitle' for discussion.
-- Assume that the @Text@ is non-empty. The returned @Text@ is guaranteed to be non-empty.
toTitleNonEmpty :: Text -> Text
toTitleNonEmpty (Text src o l) = runST $ do
toTitleNonEmpty t = case toTitleNonEmptyFrom False t of
(t', _) -> t'

-- | /O(n)/ Like 'toTitleNonEmpty', but the first argument says whether the
-- input starts in the middle of a word, and the result also says whether it
-- ends in the middle of one. The lazy 'Data.Text.Lazy.toTitle' uses this to
-- carry the state from one chunk to the next, so that a chunk boundary inside
-- a word does not start a new word.
toTitleNonEmptyFrom :: Bool -> Text -> (Text, Bool)
toTitleNonEmptyFrom inWord (Text src o l) = runST $ do
-- Case conversion a single code point may produce up to 3 code-points,
-- each up to 4 bytes, so 12 in total.
dst <- A.new (l + 12)
outer dst l o 0 False
outer dst l o 0 inWord
where
outer :: forall s. A.MArray s -> Int -> Int -> Int -> Bool -> ST s Text
outer :: forall s. A.MArray s -> Int -> Int -> Int -> Bool -> ST s (Text, Bool)
outer !dst !dstLen = inner
where
inner !srcOff !dstOff !mode
| srcOff >= o + l = do
A.shrinkM dst dstOff
arr <- A.unsafeFreeze dst
return (Text arr 0 dstOff)
return (Text arr 0 dstOff, mode)
| dstOff + 12 > dstLen = do
-- Ensure to extend the buffer by at least 12 bytes.
let !dstLen' = dstLen + max 12 (l + o - srcOff)
Expand Down
10 changes: 8 additions & 2 deletions src/Data/Text/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks, foldrChunks
smallChunkSize, defaultChunkSize, equal, LazyText)
import Data.Text.Internal (firstf, safe, text)
import Data.Text.Internal.Reverse (reverseNonEmpty)
import Data.Text.Internal.Transformation (mapNonEmpty, toCaseFoldNonEmpty, toLowerNonEmpty, toUpperNonEmpty, filter_)
import Data.Text.Internal.Transformation (mapNonEmpty, toCaseFoldNonEmpty, toLowerNonEmpty, toUpperNonEmpty, toTitleNonEmptyFrom, filter_)
import Data.Text.Lazy.Encoding (decodeUtf8', encodeUtf8Builder)
import Data.Text.Internal.Lazy.Search (indices)
import qualified GHC.CString as GHC
Expand Down Expand Up @@ -862,7 +862,13 @@ toUpper = foldrChunks (\chnk acc -> Chunk (toUpperNonEmpty chnk) acc) Empty
--
-- @since 1.0.0.0
toTitle :: Text -> Text
toTitle = foldrChunks (\chnk acc -> Chunk (T.toTitle chnk) acc) Empty
toTitle = go False
where
-- The flag says whether the previous chunk ended in the middle of a word,
-- so that a chunk boundary inside a word doesn't start a new one.
go _ Empty = Empty
go inWord (Chunk chnk rest) = case toTitleNonEmptyFrom inWord chnk of
(chnk', inWord') -> Chunk chnk' (go inWord' rest)
{-# INLINE toTitle #-}

-- | /O(n)/ 'foldl', applied to a binary operator, a starting value
Expand Down
6 changes: 6 additions & 0 deletions tests/Tests/Properties/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ t_toTitle_1stNotLower = and . notLow . T.toTitle . T.filter stable . T.filter (n
isGeorgian c = c >= '\4256' && c < '\4352'
t_toTitle_char c = c `notElem` toTitleExceptions ==>
T.toTitle (T.singleton c) === T.singleton (C.toUpper c)
-- Lazy toTitle must not depend on how the input is split into chunks.
tl_toTitle_strict t = TL.toTitle t === TL.fromStrict (T.toTitle (TL.toStrict t))
tl_toTitle_chunk_boundary =
TL.toTitle (TL.fromChunks (map T.pack ["hel", "lo wor", "ld"])) === TL.pack "Hello World"

-- | Baseline generated with GHC 9.2 + text-1.2.5.0,
t_toTitle_exceptions = T.unpack (T.concatMap (T.toTitle . T.singleton) (T.pack toTitleExceptions)) === "Ss\700N\453\453\453\456\456\456\459\459\459J\780\498\498\498\921\776\769\933\776\769\1333\1410\4304\4305\4306\4307\4308\4309\4310\4311\4312\4313\4314\4315\4316\4317\4318\4319\4320\4321\4322\4323\4324\4325\4326\4327\4328\4329\4330\4331\4332\4333\4334\4335\4336\4337\4338\4339\4340\4341\4342\4343\4344\4345\4346\4349\4350\4351H\817T\776W\778Y\778A\702\933\787\933\787\768\933\787\769\933\787\834\8122\837\902\837\913\834\913\834\837\8138\837\905\837\919\834\919\834\837\921\776\768\921\776\769\921\834\921\776\834\933\776\768\933\776\769\929\787\933\834\933\776\834\8186\837\911\837\937\834\937\834\837FfFiFlFfiFflStSt\1348\1398\1348\1381\1348\1387\1358\1398\1348\1389"
Expand Down Expand Up @@ -406,6 +410,8 @@ testText =
testProperty "t_toTitle_title" t_toTitle_title,
testProperty "t_toTitle_1stNotLower" t_toTitle_1stNotLower,
testProperty "t_toTitle_exceptions" t_toTitle_exceptions,
testProperty "tl_toTitle_strict" tl_toTitle_strict,
testProperty "tl_toTitle_chunk_boundary" tl_toTitle_chunk_boundary,

testProperty "t_toUpper_idempotent" t_toUpper_idempotent,
testProperty "t_toLower_idempotent" t_toLower_idempotent,
Expand Down
Loading