
module Utils where

import Data.Function
import Data.List
import System.Exit
import System.IO
import Text.Regex.Posix

die :: Errors -> IO a
die errs = do mapM_ (hPutStrLn stderr) errs
              exitFailure

dieOnErrors :: Either Errors a -> IO a
dieOnErrors (Left errs) = die errs
dieOnErrors (Right x) = return x

type Errors = [String]

maybeRead :: Read a => String -> Maybe a
maybeRead str = case reads str of
                [(x, "")] -> Just x
                _ -> Nothing

re :: String -> String -> Maybe [String]
re r str = case matchM r' str :: Maybe (String, String, String, [String]) of
           Just (_, _, _, ms) -> Just ms
           Nothing -> Nothing
    where r' = makeRegex r :: Regex

unSepList :: Eq a => a -> [a] -> [[a]]
unSepList x xs = case break (x ==) xs of
                 (this, _ : xs') ->
                     this : unSepList x xs'
                 (this, []) ->
                     [this]

sortByFst :: Ord a => [(a, b)] -> [(a, b)]
sortByFst = sortBy (compare `on` fst)



module Utils where

import Data.Function
import Data.List
import System.Exit
import System.IO
import Text.Regex.PCRE

die :: Errors -> IO a
die errs = do mapM_ (hPutStrLn stderr) errs
              exitFailure

dieOnErrors :: Either Errors a -> IO a
dieOnErrors (Left errs) = die errs
dieOnErrors (Right x) = return x

type Errors = [String]

maybeRead :: Read a => String -> Maybe a
maybeRead str = case reads str of
                [(x, "")] -> Just x
                _ -> Nothing

re :: String -> String -> Maybe [String]
re r str = case matchM r' str :: Maybe (String, String, String, [String]) of
           Just (_, _, _, ms) -> Just ms
           Nothing -> Nothing
    where r' = makeRegex r :: Regex

unSepList :: Eq a => a -> [a] -> [[a]]
unSepList x xs = case break (x ==) xs of
                 (this, _ : xs') ->
                     this : unSepList x xs'
                 (this, []) ->
                     [this]

sortByFst :: Ord a => [(a, b)] -> [(a, b)]
sortByFst = sortBy (compare `on` fst)


Use Test.Regex.PCRE instead of .Posix in compare
Use Test.Regex.PCRE instead of .Posix in compare

This program:

    main :: IO ()
    main = do re "[^ ]" "\207"
              re "[ ]" "\207"
              re " " "\207"

    re :: String -> String -> IO ()
    re r str = let r' = makeRegex r :: Regex
                   res = matchM r' str :: Maybe (String, String, String, [String])
               in print res

prints

    Nothing
    Nothing
    Nothing

for me with Posix, but

    Just ("","\207","",[])
    Nothing
    Nothing

with PCRE. This was causing compare to fail with

    Tar line doesn't parse: "drwxrwxr-x simonmar/GHC       0 2012-12-08 21:35 ghc-7.6.1.20121207/libraries/haskeline/tests/dummy-\206\188\206\177\207\\302\\203/"

on the GHC source tarball.
