-- Copyright (C) 2003 David Roundy
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2, or (at your option)
-- any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; see the file COPYING.  If not, write to
-- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-- Boston, MA 02110-1301, USA.


module Darcs.Patch.RegChars ( regChars,
                ) where

import Darcs.Prelude

(&&&) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
&&& :: forall a. (a -> Bool) -> (a -> Bool) -> a -> Bool
(&&&) a -> Bool
a a -> Bool
b a
c = a -> Bool
a a
c Bool -> Bool -> Bool
&& a -> Bool
b a
c

(|||) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
||| :: forall a. (a -> Bool) -> (a -> Bool) -> a -> Bool
(|||) a -> Bool
a a -> Bool
b a
c = a -> Bool
a a
c Bool -> Bool -> Bool
|| a -> Bool
b a
c

{-# INLINE regChars #-}

-- | 'regChars' returns a filter function that tells if a char is a member
-- of the regChar expression or not. The regChar expression is basically a
-- set of chars, but it can contain ranges with use of the '-' (dash), and
-- it can also be specified as a complement set by prefixing with '^'
-- (caret). The dash and caret, as well as the backslash, can all be
-- escaped with a backslash to suppress their special meaning.
--
-- NOTE: The '.' (dot) is allowed to be escaped. It has no special meaning
-- if it is not escaped, but the default 'filename_toks' in
-- Darcs.Commands.Replace uses an escaped dot (WHY?).

regChars :: String -> Char -> Bool
regChars :: String -> Char -> Bool
regChars (Char
'^':String
cs) = Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Char -> Bool
normalRegChars (String -> String
unescapeChars String
cs)
regChars (Char
'\\':Char
'^':String
cs) = String -> Char -> Bool
normalRegChars (String -> Char -> Bool) -> String -> Char -> Bool
forall a b. (a -> b) -> a -> b
$ String -> String
unescapeChars (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Char
'^'Char -> String -> String
forall a. a -> [a] -> [a]
:String
cs
regChars String
cs = String -> Char -> Bool
normalRegChars (String -> Char -> Bool) -> String -> Char -> Bool
forall a b. (a -> b) -> a -> b
$ String -> String
unescapeChars String
cs

{-# INLINE unescapeChars #-}

-- | 'unescapeChars' unescapes whitespace, which is escaped in the replace
-- patch file format. It will also unescape escaped carets, which is useful
-- for escaping a leading caret that should not invert the regChars. All
-- other escapes are left for the unescaping in 'normalRegChars'.

unescapeChars :: String -> String
unescapeChars :: String -> String
unescapeChars (Char
'\\':Char
'n':String
cs) = Char
'\n' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
unescapeChars String
cs
unescapeChars (Char
'\\':Char
't':String
cs) = Char
'\t' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
unescapeChars String
cs
unescapeChars (Char
'\\':Char
'^':String
cs) = Char
'^' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
unescapeChars String
cs
unescapeChars (Char
c:String
cs) = Char
c Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
unescapeChars String
cs
unescapeChars [] = []

{-# INLINE normalRegChars #-}

-- | 'normalRegChars' assembles the filter function. It handles special
-- chars, and also unescaping of escaped special chars. If a non-special
-- char is still escaped by now we get a failure.

normalRegChars :: String -> Char -> Bool
normalRegChars :: String -> Char -> Bool
normalRegChars (Char
'\\':Char
'.':String
cs) = (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'.') (Char -> Bool) -> (Char -> Bool) -> Char -> Bool
forall a. (a -> Bool) -> (a -> Bool) -> a -> Bool
||| String -> Char -> Bool
normalRegChars String
cs
normalRegChars (Char
'\\':Char
'-':String
cs) = (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'-') (Char -> Bool) -> (Char -> Bool) -> Char -> Bool
forall a. (a -> Bool) -> (a -> Bool) -> a -> Bool
||| String -> Char -> Bool
normalRegChars String
cs
normalRegChars (Char
'\\':Char
'\\':String
cs) = (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'\\') (Char -> Bool) -> (Char -> Bool) -> Char -> Bool
forall a. (a -> Bool) -> (a -> Bool) -> a -> Bool
||| String -> Char -> Bool
normalRegChars String
cs
normalRegChars (Char
'\\':Char
c:String
_) = String -> Char -> Bool
forall a. HasCallStack => String -> a
error (String -> Char -> Bool) -> String -> Char -> Bool
forall a b. (a -> b) -> a -> b
$ String
"'\\"String -> String -> String
forall a. [a] -> [a] -> [a]
++[Char
c]String -> String -> String
forall a. [a] -> [a] -> [a]
++String
"' not supported."
normalRegChars (Char
c1:Char
'-':Char
c2:String
cs) = ((Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
c1) (Char -> Bool) -> (Char -> Bool) -> Char -> Bool
forall a. (a -> Bool) -> (a -> Bool) -> a -> Bool
&&& (Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
c2)) (Char -> Bool) -> (Char -> Bool) -> Char -> Bool
forall a. (a -> Bool) -> (a -> Bool) -> a -> Bool
||| String -> Char -> Bool
normalRegChars String
cs
normalRegChars (Char
c:String
cs) = (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c) (Char -> Bool) -> (Char -> Bool) -> Char -> Bool
forall a. (a -> Bool) -> (a -> Bool) -> a -> Bool
||| String -> Char -> Bool
normalRegChars String
cs
normalRegChars [] = \Char
_ -> Bool
False