Commits

Bryan O'Sullivan committed a51b74f

Add support for toLower.

  • Participants
  • Parent commits c6ae336

Comments (0)

Files changed (5)

File Data/Text.hs

 
     -- * Case conversion
     , toUpper
+    , toLower
 
     -- * Folds
     , foldl
 toUpper t = unstream (S.toUpper (stream t))
 {-# INLINE toUpper #-}
 
+-- | /O(n)/ Convert a string to lower case, using simple case
+-- conversion.  The result string may be longer than the input string.
+-- For instance, the German eszett (U+00DF) maps to the two-letter
+-- sequence SS.
+toLower :: Text -> Text
+toLower t = unstream (S.toLower (stream t))
+{-# INLINE toLower #-}
+
 -- | /O(n)/ The 'transpose' function transposes the rows and columns
 -- of its 'Text' argument.  Note that this function uses 'pack',
 -- 'unpack', and the list version of transpose, and is thus not very

File Data/Text/Fusion/CaseMapping.hs

 {-# LANGUAGE Rank2Types #-}
+-- AUTOMATICALLY GENERATED - DO NOT EDIT
+-- Generated by scripts/SpecialCasing.hs
 module Data.Text.Fusion.CaseMapping where
 import Data.Char
 import Data.Text.Fusion.Internal
 -- GREEK SMALL LETTER OMEGA WITH PERISPOMENI AND YPOGEGRAMMENI
 upperMapping '\x1ff7' s = Yield '\x03a9' (s :!: '\x0342' :!: '\x0399')
 upperMapping c s = Yield (toUpper c) (s :!: '\0' :!: '\0')
+lowerMapping :: forall s. Char -> s -> Step (PairS (PairS s Char) Char) Char
+{-# INLINE lowerMapping #-}
+-- LATIN CAPITAL LETTER I WITH DOT ABOVE
+lowerMapping '\x0130' s = Yield '\x0069' (s :!: '\x0307' :!: '\x0000')
+lowerMapping c s = Yield (toLower c) (s :!: '\0' :!: '\0')

File Data/Text/Fusion/Common.hs

-{-# LANGUAGE BangPatterns #-}
+{-# LANGUAGE BangPatterns, Rank2Types #-}
 -- |
 -- Module      : Data.Text.Fusion.Common
 -- Copyright   : (c) Bryan O'Sullivan 2009
 
     -- ** Case conversion
     , toUpper
+    , toLower
 
     -- * Folds
     , foldl
 import qualified Data.List as L
 import qualified Prelude as P
 import Data.Text.Fusion.Internal
-import Data.Text.Fusion.CaseMapping (upperMapping)
+import Data.Text.Fusion.CaseMapping (lowerMapping, upperMapping)
 
 singleton :: Char -> Stream Char
 singleton c = Stream next False 1 -- HINT maybe too low
 -- ----------------------------------------------------------------------------
 -- ** Case conversions (folds)
 
--- | /O(n)/ Convert a string to upper case, using simple case
--- conversion.  The result string may be longer than the input string.
--- For instance, the German eszett (U+00DF) maps to the two-letter
--- sequence SS.
-toUpper :: Stream Char -> Stream Char
-toUpper (Stream next0 s0 len) = Stream next (s0 :!: '\0' :!: '\0') len
+caseConvert :: (forall s. Char -> s -> Step (PairS (PairS s Char) Char) Char)
+            -> Stream Char -> Stream Char
+caseConvert remap (Stream next0 s0 len) = Stream next (s0 :!: '\0' :!: '\0') len
   where
     {-# INLINE next #-}
     next (s :!: '\0' :!: _) =
         case next0 s of
           Done       -> Done
           Skip s'    -> Skip (s' :!: '\0' :!: '\0')
-          Yield c s' -> upperMapping c s'
+          Yield c s' -> remap c s'
     next (s :!: a :!: b) = Yield a (s :!: b :!: '\0')
+
+-- | /O(n)/ Convert a string to upper case, using simple case
+-- conversion.  The result string may be longer than the input string.
+-- For instance, the German eszett (U+00DF) maps to the two-letter
+-- sequence SS.
+toUpper :: Stream Char -> Stream Char
+toUpper = caseConvert upperMapping
 {-# INLINE [0] toUpper #-}
 
+-- | /O(n)/ Convert a string to lower case, using simple case
+-- conversion.  The result string may be longer than the input string.
+-- For instance, the German eszett (U+00DF) maps to the two-letter
+-- sequence SS.
+toLower :: Stream Char -> Stream Char
+toLower = caseConvert lowerMapping
+{-# INLINE [0] toLower #-}
+
 -- ----------------------------------------------------------------------------
 -- * Reducing Streams (folds)
 

File Data/Text/Lazy.hs

 
     -- ** Case conversion
     , toUpper
+    , toLower
 
     -- * Folds
     , foldl
 toUpper t = unstream (S.toUpper (stream t))
 {-# INLINE toUpper #-}
 
+-- | /O(n)/ Convert a string to lower case, using simple case
+-- conversion.  The result string may be longer than the input string.
+-- For instance, the German eszett (U+00DF) maps to the two-letter
+-- sequence SS.
+toLower :: Text -> Text
+toLower t = unstream (S.toLower (stream t))
+{-# INLINE toLower #-}
+
 -- | /O(n)/ 'foldl', applied to a binary operator, a starting value
 -- (typically the left-identity of the operator), and a 'Text',
 -- reduces the 'Text' using the binary operator, from left to right.

File scripts/SpecialCasing.hs

 import Numeric
 import System.Environment
 import System.IO
-import Text.ParserCombinators.Parsec hiding (many, optional, upper, (<|>))
+import Text.ParserCombinators.Parsec hiding (many, optional, upper, lower, (<|>))
 import Text.ParserCombinators.Parsec.Combinator
 
 instance Applicative (GenParser s a) where
 parseFile :: FilePath -> IO (Either ParseError [Case])
 parseFile name = parse entries name <$> readFile name
 
-upperFunc ms = typ ++ (map nice . filter p $ ms) ++ [last]
+mapFunc which access twiddle ms = typ ++ (map nice . filter p $ ms) ++ [last]
   where
-    typ = ["upperMapping :: forall s. Char -> s -> Step (PairS (PairS s Char) Char) Char"
-           ,"{-# INLINE upperMapping #-}"]
-    last = "upperMapping c s = Yield (toUpper c) (s :!: '\\0' :!: '\\0')"
-    nice c = "-- " ++ name c ++ "\nupperMapping " ++ showC (code c) ++ " s = Yield " ++ x ++ " (s :!: " ++ y ++ " :!: " ++ z ++ ")"
-       where [x,y,z] = (map showC . take 3) (upper c ++ repeat '\0')
-    p c = [code c] /= upper c && null (conditions c)
+    typ = [which ++ "Mapping :: forall s. Char -> s -> Step (PairS (PairS s Char) Char) Char"
+           ,"{-# INLINE " ++ which ++ "Mapping #-}"]
+    last = which ++ "Mapping c s = Yield (to" ++ ucFirst which ++ " c) (s :!: '\\0' :!: '\\0')"
+    nice c = "-- " ++ name c ++ "\n" ++
+             which ++ "Mapping " ++ showC (code c) ++ " s = Yield " ++ x ++ " (s :!: " ++ y ++ " :!: " ++ z ++ ")"
+       where [x,y,z] = (map showC . take 3) (access c ++ repeat '\0')
+    p c = [k] /= a && a /= [twiddle k] && null (conditions c)
+        where a = access c
+              k = code c
     showC c = "'\\x" ++ d ++ "'"
         where h = showHex (ord c) ""
               d = replicate (4 - length h) '0' ++ h
 
+ucFirst (c:cs) = toUpper c : cs
+ucFirst [] = []
+
 main = do
   args <- getArgs
   let (iname, oname) = case args of
                       ,"import Data.Char"
                       ,"import Data.Text.Fusion.Internal"
                       ,""]
-  mapM_ (hPutStrLn h) (upperFunc ms)
+  mapM_ (hPutStrLn h) (mapFunc "upper" upper toUpper ms)
+  mapM_ (hPutStrLn h) (mapFunc "lower" lower toLower ms)
   hClose h