module Main where

--import GHC.IO.Encoding (getForeignEncoding)
import Data.Bits (Bits((.&.)))
import Foreign.C.Types (CChar, CInt(..))
import Foreign.C.String
import Foreign.Ptr
import Text.Regex.PCRE
import qualified Data.ByteString.Char8 as B

foreign import ccall "seestr"
        seestr :: Ptr CChar -> IO CInt

seeStr :: String -> IO Int
seeStr s = do ci <- withCString s seestr
              return $ fromInteger (toInteger ci)

seeChar8 :: B.ByteString -> IO Int
seeChar8 s = do ci <- B.useAsCString s seestr
                return $ fromInteger (toInteger ci)

test :: String -> IO ()
test s = do putStrLn "testing with String"
            x <- seeStr s
            putStrLn ("result: " ++ show x)
            putStrLn ""
            putStrLn "testing with ByteString"
            x <- seeChar8 (B.pack s)
            putStrLn ("result: " ++ show x)

testpcre re text = do putStrLn ("regex            : " ++ re)
                      putStrLn ("text             : " ++ text)
                      putStrLn ("String match     : " ++ show m1)
                      putStrLn ("ByteString match : " ++ show m2)
  where
    c = defaultCompOpt .&. compUTF8
    e = defaultExecOpt
    m1 = match (makeRegexOpts c e re :: Regex) text :: [[String]]
    m2 = match (makeRegexOpts c e (B.pack re) :: Regex) (B.pack text) :: [[B.ByteString]]

main = do --enc <- getForeignEncoding
          --putStrLn ("getForeignEncoding: " ++ show enc)
          --putStrLn ""
          test "país"
          putStrLn ""
          testpcre "país:(.*)" "país:Brasil"

