{-# OPTIONS_GHC -Wall -fwarn-tabs #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module IsSpace (main) where
import qualified Data.Char as C
import Data.Word (Word8)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Foreign.C.Types (CInt)
import Criterion (bench, nf)
import Criterion.Main (defaultMain)
isPerlSpace :: Char -> Bool
isPerlSpace c
= (' ' == c)
|| ('\t' <= c && c <= '\r' && c /= '\v')
|| ('\x85' == c)
|| ('\x2028' == c)
|| ('\x2029' == c)
{-# INLINE isPerlSpace #-}
isSpace :: Char -> Bool
isSpace c = c `B8.elem` spaces
where
spaces = B8.pack " \n\r\t\v\f"
{-# NOINLINE spaces #-}
{-# INLINE isSpace #-}
isSpace_Char8 :: Char -> Bool
isSpace_Char8 c = (' ' == c) || ('\t' <= c && c <= '\r')
{-# INLINE isSpace_Char8 #-}
isSpace_Char :: Char -> Bool
isSpace_Char c
= (' ' == c)
|| ('\t' <= c && c <= '\r')
|| ('\xA0' == c)
|| (iswspace (fromIntegral (C.ord c)) /= 0)
{-# INLINE isSpace_Char #-}
foreign import ccall unsafe "u_iswspace"
iswspace :: CInt -> CInt
isSpace_DataChar :: Char -> Bool
isSpace_DataChar c =
c == ' ' ||
c == '\t' ||
c == '\n' ||
c == '\r' ||
c == '\f' ||
c == '\v' ||
c == '\xa0' ||
iswspace (fromIntegral (C.ord c)) /= 0
{-# INLINE isSpace_DataChar #-}
isSpace_w8 :: Word8 -> Bool
isSpace_w8 w = (w == 32) || (9 <= w && w <= 13)
{-# INLINE isSpace_w8 #-}
main :: IO ()
main = defaultMain
[ bench "Data.Char.isSpace" $ nf (map C.isSpace) ['\x0'..'\255']
, bench "isSpace_DataChar" $ nf (map isSpace_DataChar) ['\x0'..'\255']
, bench "isSpace_Char" $ nf (map isSpace_Char) ['\x0'..'\255']
, bench "isPerlSpace" $ nf (map isPerlSpace) ['\x0'..'\255']
, bench "Data.Attoparsec.Char8.isSpace"
$ nf (map isSpace) ['\x0'..'\255']
, bench "isSpace_Char8" $ nf (map isSpace_Char8) ['\x0'..'\255']
, bench "isSpace_w8" $ nf (map isSpace_w8) [0..255]
]