on master help sign in
Merge pull request #168 from alskipp/toTitle

Improvements to `toTitle` functionality

commit214598adcd7fcc575717ac745311b929c8bb9c0a authorBryan O'Sullivan <bos@serpentine.com> dateTue 6 Dec 2016, 04:37 (10 years ago) parentbb3d8620bae88d
2 files changed+14−10
Data/Text/Internal/Fusion/Common.hs+10−9 tests/Tests/Properties.hs+4−1
Data/Text/Internal/Fusion/Common.hs+10−9
@@ -107,7 +107,7 @@ import Prelude (Bool(..), Char, Eq(..), Int, Integral, Maybe(..),
107107import qualified Data.List as L
108108import qualified Prelude as P
109109import Data.Bits (shiftL)
110−import Data.Char (isLetter)
110+import Data.Char (isLetter, isSpace)
111111import Data.Int (Int64)
112112import Data.Text.Internal.Fusion.Types
113113import Data.Text.Internal.Fusion.CaseMapping (foldMapping, lowerMapping, titleMapping,
@@ -462,15 +462,16 @@ toTitle (Stream next0 s0 len) = Stream next (CC (False :*: s0) '\0' '\0') len
462462 where
463463 next (CC (letter :*: s) '\0' _) =
464464 case next0 s of
465− Done -> Done
466− Skip s' -> Skip (CC (letter :*: s') '\0' '\0')
465+ Done -> Done
466+ Skip s' -> Skip (CC (letter :*: s') '\0' '\0')
467467 Yield c s'
468− | letter' -> if letter
469− then lowerMapping c (letter' :*: s')
470− else titleMapping c (letter' :*: s')
471− | otherwise -> Yield c (CC (letter' :*: s') '\0' '\0')
472− where letter' = isLetter c
473− next (CC s a b) = Yield a (CC s b '\0')
468+ | nonSpace -> if letter
469+ then lowerMapping c (nonSpace :*: s')
470+ else titleMapping c (letter' :*: s')
471+ | otherwise -> Yield c (CC (letter' :*: s') '\0' '\0')
472+ where nonSpace = P.not (isSpace c)
473+ letter' = isLetter c
474+ next (CC s a b) = Yield a (CC s b '\0')
474475{-# INLINE [0] toTitle #-}
475476
476477data Justify i s = Just1 !i !s
tests/Tests/Properties.hs+4−1
@@ -326,6 +326,8 @@ t_toUpper_upper t = p (T.toUpper t) >= p t
326326 where p = T.length . T.filter isUpper
327327tl_toUpper_upper t = p (TL.toUpper t) >= p t
328328 where p = TL.length . TL.filter isUpper
329+t_toTitle_title t = all (<= 1) (caps t)
330+ where caps = fmap (T.length . T.filter isUpper) . T.words . T.toTitle
329331
330332justifyLeft k c xs = xs ++ L.replicate (k - length xs) c
331333justifyRight m n xs = L.replicate (m - length xs) n ++ xs
@@ -1030,7 +1032,8 @@ tests =
10301032 testProperty "tl_toLower_lower" tl_toLower_lower,
10311033 testProperty "t_toUpper_length" t_toUpper_length,
10321034 testProperty "t_toUpper_upper" t_toUpper_upper,
1033− testProperty "tl_toUpper_upper" tl_toUpper_upper
1035+ testProperty "tl_toUpper_upper" tl_toUpper_upper,
1036+ testProperty "t_toTitle_title" t_toTitle_title
10341037 ],
10351038
10361039 testGroup "justification" [