regex-pcre-text (empty) → 0.94.0.0
raw patch · 6 files changed
+412/−0 lines, 6 filesdep +arraydep +basedep +bytestringsetup-changed
Dependencies added: array, base, bytestring, regex-base, regex-pcre-builtin, regex-tdfa-text, text
Files
- LICENSE +12/−0
- README.markdown +16/−0
- Setup.hs +2/−0
- Text/Regex/PCRE/Text.hs +164/−0
- Text/Regex/PCRE/Text/Lazy.hs +165/−0
- regex-pcre-text.cabal +53/−0
+ LICENSE view
@@ -0,0 +1,12 @@+This modile is under this "3 clause" BSD license:++Copyright (c) 2007-2017, The Authors+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.+ * The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.markdown view
@@ -0,0 +1,16 @@+# regex-pcre-text++This package extends regex-base and `regex-pcre` to work with `Text`+in the same way that `regex-tdfa-text` does. It uses the `regex-base`+`Text` instances provided by `regex-tdfa-text`.+++## usage with regex++To use it with `regex` just make sure you are using `regex-with-pcre`+`1.0.1.0` or later.+++## credits++This code is based on the code for `regex-pcre` by Christopher Kuklewicz.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Text/Regex/PCRE/Text.hs view
@@ -0,0 +1,164 @@+{-# LANGUAGE MultiParamTypeClasses #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++module Text.Regex.PCRE.Text+ -- ** Types+ ( Regex+ , MatchOffset+ , MatchLength+ , CompOption(CompOption)+ , ExecOption(ExecOption)+ , ReturnCode+ , WrapError+ -- ** Miscellaneous+ , unusedOffset+ , getVersion+ -- ** Medium level API functions+ , compile+ , execute+ , regexec+ -- ** CompOption flags+ , compBlank+ , compAnchored+ , compAutoCallout+ , compCaseless+ , compDollarEndOnly+ , compDotAll+ , compExtended+ , compExtra+ , compFirstLine+ , compMultiline+ , compNoAutoCapture+ , compUngreedy+ , compUTF8+ , compNoUTF8Check+ -- ** ExecOption flags+ , execBlank+ , execAnchored+ , execNotBOL+ , execNotEOL+ , execNotEmpty+ , execNoUTF8Check+ , execPartial+ ) where++import Data.Array+import qualified Data.ByteString as B+import qualified Data.ByteString.Unsafe as B+import qualified Data.Text as T+import qualified Data.Text.Encoding as T+import Foreign.C.String+import Foreign+import System.IO.Unsafe+import Text.Regex.Base.Impl+import Text.Regex.Base.RegexLike+import Text.Regex.PCRE.Wrap+import Text.Regex.TDFA.Text()+++instance RegexContext Regex T.Text T.Text where+ match = polymatch+ matchM = polymatchM++instance RegexMaker Regex CompOption ExecOption T.Text where+ makeRegexOpts c e pat = unsafePerformIO $+ compile c e pat >>= unwrap++ makeRegexOptsM c e pat = either (fail.show) return $ unsafePerformIO $+ compile c e pat++instance RegexLike Regex T.Text where+ matchTest re tx = unsafePerformIO $+ asCStringLen tx (wrapTest 0 re) >>= unwrap++ matchOnce re tx = unsafePerformIO $+ execute re tx >>= unwrap++ matchAll re tx = unsafePerformIO $+ asCStringLen tx (wrapMatchAll re) >>= unwrap++ matchCount re tx = unsafePerformIO $+ asCStringLen tx (wrapCount re) >>= unwrap+++-- ---------------------------------------------------------------------+-- | Compiles a regular expression+compile :: CompOption -- ^ (summed together)+ -> ExecOption -- ^ (summed together)+ -> T.Text -- ^ The regular expression to compile+ -> IO (Either (MatchOffset,String) Regex) -- ^ Returns: the compiled regular expression+compile c e pat =+ -- PCRE does not allow one to specify a length for the regular expression, it must by 0 terminated+ asCString pat $ wrapCompile c e+++-- ---------------------------------------------------------------------+-- | Matches a regular expression against a string+execute :: Regex -- ^ Compiled regular expression+ -> T.Text -- ^ Text to match against+ -> IO (Either WrapError (Maybe (Array Int (MatchOffset,MatchLength))))+ -- ^ Returns: 'Nothing' if the regex did not match the+ -- string, or 'Just' an array of (offset,length) pairs+ -- where index 0 is whole match, and the rest are the+ -- captured subexpressions.+execute re tx = do+ maybeStartEnd <- asCStringLen tx (wrapMatch 0 re)+ case maybeStartEnd of+ Right Nothing -> return $ Right Nothing+ Right (Just parts) ->+ return $ Right $ Just $ listArray (0,pred $ length parts)+ [ (s,e-s) | (s,e) <- parts ]+ Left err -> return $ Left err+++-- ---------------------------------------------------------------------+-- | Matches a regular expression against a string+regexec :: Regex -- ^ Compiled regular expression+ -> T.Text -- ^ Text to match against+ -> IO (Either WrapError (Maybe (T.Text, T.Text, T.Text, [T.Text])))+ -- ^ Returns: 'Nothing' if the regex did not match the+ -- string, or 'Just' text including before and after text+regexec re tx = do+ mb <- asCStringLen tx $ wrapMatch 0 re+ case mb of+ Right Nothing -> return (Right Nothing)+ Right (Just parts) -> return . Right . Just . matchedParts $ parts+ Left err -> return (Left err)+ where+ matchedParts [] = (T.empty,T.empty,tx,[]) -- no information+ matchedParts (mtchd@(start,stop):rst) =+ ( T.take start tx+ , getSub mtchd+ , T.drop stop tx+ , map getSub rst+ )++ getSub (start,stop)+ | start == unusedOffset = T.empty+ | otherwise = T.take (stop-start) . T.drop start $ tx+++-- ---------------------------------------------------------------------+-- helpers++unwrap :: (Show e) => Either e v -> IO v+unwrap x = case x of+ Left e -> fail $ "Text.Regex.PCRE.Text died: " ++ show e+ Right v -> return v++{-# INLINE asCString #-}+asCString :: T.Text -> (CString->IO a) -> IO a+asCString = B.unsafeUseAsCString . T.encodeUtf8++{-# INLINE asCStringLen #-}+asCStringLen :: T.Text -> (CStringLen->IO a) -> IO a+asCStringLen s op = B.unsafeUseAsCStringLen (T.encodeUtf8 s) checked+ where+ checked cs@(ptr,_)+ | ptr == nullPtr = B.unsafeUseAsCStringLen myEmpty $ op . trim+ | otherwise = op cs++ trim (ptr,_) = (ptr,0)++myEmpty :: B.ByteString+myEmpty = B.pack [0]
+ Text/Regex/PCRE/Text/Lazy.hs view
@@ -0,0 +1,165 @@+{-# LANGUAGE MultiParamTypeClasses #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++module Text.Regex.PCRE.Text.Lazy+ -- ** Types+ ( Regex+ , MatchOffset+ , MatchLength+ , CompOption(CompOption)+ , ExecOption(ExecOption)+ , ReturnCode+ , WrapError+ -- ** Miscellaneous+ , unusedOffset+ , getVersion+ -- ** Medium level API functions+ , compile+ , execute+ , regexec+ -- ** CompOption flags+ , compBlank+ , compAnchored+ , compAutoCallout+ , compCaseless+ , compDollarEndOnly+ , compDotAll+ , compExtended+ , compExtra+ , compFirstLine+ , compMultiline+ , compNoAutoCapture+ , compUngreedy+ , compUTF8+ , compNoUTF8Check+ -- ** ExecOption flags+ , execBlank+ , execAnchored+ , execNotBOL+ , execNotEOL+ , execNotEmpty+ , execNoUTF8Check+ , execPartial+ ) where++import Data.Array+import qualified Data.ByteString as B+import qualified Data.ByteString.Unsafe as B+import qualified Data.Text.Encoding as T+import qualified Data.Text.Lazy as TL+import Foreign.C.String+import Foreign+import System.IO.Unsafe+import Text.Regex.Base.Impl+import Text.Regex.Base.RegexLike+import Text.Regex.PCRE.Wrap+import Text.Regex.TDFA.Text()+import Text.Regex.TDFA.Text.Lazy()+++instance RegexContext Regex TL.Text TL.Text where+ match = polymatch+ matchM = polymatchM++instance RegexMaker Regex CompOption ExecOption TL.Text where+ makeRegexOpts c e pat = unsafePerformIO $+ compile c e pat >>= unwrap++ makeRegexOptsM c e pat = either (fail.show) return $ unsafePerformIO $+ compile c e pat++instance RegexLike Regex TL.Text where+ matchTest re tx = unsafePerformIO $+ asCStringLen tx (wrapTest 0 re) >>= unwrap++ matchOnce re tx = unsafePerformIO $+ execute re tx >>= unwrap++ matchAll re tx = unsafePerformIO $+ asCStringLen tx (wrapMatchAll re) >>= unwrap++ matchCount re tx = unsafePerformIO $+ asCStringLen tx (wrapCount re) >>= unwrap+++-- ---------------------------------------------------------------------+-- | Compiles a regular expression+compile :: CompOption -- ^ (summed together)+ -> ExecOption -- ^ (summed together)+ -> TL.Text -- ^ The regular expression to compile+ -> IO (Either (MatchOffset,String) Regex) -- ^ Returns: the compiled regular expression+compile c e pat =+ -- PCRE does not allow one to specify a length for the regular expression, it must by 0 terminated+ asCString pat $ wrapCompile c e+++-- ---------------------------------------------------------------------+-- | Matches a regular expression against a string+execute :: Regex -- ^ Compiled regular expression+ -> TL.Text -- ^ Text to match against+ -> IO (Either WrapError (Maybe (Array Int (MatchOffset,MatchLength))))+ -- ^ Returns: 'Nothing' if the regex did not match the+ -- string, or 'Just' an array of (offset,length) pairs+ -- where index 0 is whole match, and the rest are the+ -- captured subexpressions.+execute re tx = do+ maybeStartEnd <- asCStringLen tx $ wrapMatch 0 re+ case maybeStartEnd of+ Right Nothing -> return $ Right Nothing+ Right (Just parts) ->+ return $ Right $ Just $ listArray (0,pred $ length parts)+ [ (s,e-s) | (s,e) <- parts ]+ Left err -> return $ Left err++-- ---------------------------------------------------------------------+-- | Matches a regular expression against a string+regexec :: Regex -- ^ Compiled regular expression+ -> TL.Text -- ^ Text to match against+ -> IO (Either WrapError (Maybe (TL.Text, TL.Text, TL.Text, [TL.Text])))+ -- ^ Returns: 'Nothing' if the regex did not match the+ -- string, or 'Just' text including before and after text+regexec re tx = do+ mb <- asCStringLen tx $ wrapMatch 0 re+ case mb of+ Right Nothing -> return $ Right Nothing+ Right (Just parts) -> return $ Right $ Just $ matchedParts parts+ Left err -> return $ Left err+ where+ matchedParts [] = (TL.empty,TL.empty,tx,[]) -- no information+ matchedParts (mtchd@(start,stop):rst) =+ ( TL.take (fromIntegral start) tx+ , getSub mtchd+ , TL.drop (fromIntegral stop) tx+ , map getSub rst+ )++ getSub (start,stop)+ | start == unusedOffset = TL.empty+ | otherwise = TL.take (fromIntegral $ stop-start) $+ TL.drop (fromIntegral start) tx+++-- ---------------------------------------------------------------------+-- helpers++unwrap :: (Show e) => Either e v -> IO v+unwrap x = case x of+ Left e -> fail $ "Text.Regex.PCRE.Text died: " ++ show e+ Right v -> return v++{-# INLINE asCString #-}+asCString :: TL.Text -> (CString->IO a) -> IO a+asCString = B.unsafeUseAsCString . T.encodeUtf8 . TL.toStrict++{-# INLINE asCStringLen #-}+asCStringLen :: TL.Text -> (CStringLen->IO a) -> IO a+asCStringLen s op = B.unsafeUseAsCStringLen (T.encodeUtf8 $ TL.toStrict s) checked+ where+ checked cs@(ptr,_)+ | ptr == nullPtr = B.unsafeUseAsCStringLen myEmpty $ op . trim+ | otherwise = op cs++ trim (ptr,_) = (ptr,0)++myEmpty :: B.ByteString+myEmpty = B.pack [0]
+ regex-pcre-text.cabal view
@@ -0,0 +1,53 @@+Name: regex-pcre-text+Version: 0.94.0.0+Synopsis: Text-based PCRE API for regex-base+Description: The PCRE/Text backend to accompany regex-base;+ needs regex-pcre and regex-tdfa-text+Homepage: https://github.com/cdornan/regex-pcre-text+Author: Chris Dornan and Christopher Kuklewicz+License: BSD3+License-File: LICENSE+Maintainer: Chris Dornan <chris@chrisdornan.com>+Copyright: Copyright (c) 2006-2017, Chris Dornan and Christopher Kuklewicz+Category: Text+Build-Type: Simple+Stability: Stable+bug-reports: https://github.com/cdornan/regex-pcre-text/issues++Extra-Source-Files:+ README.markdown++Cabal-Version: >= 1.10++Source-Repository head+ type: git+ location: https://github.com/cdornan/regex-pcre-text.git++Source-Repository this+ Type: git+ Location: https://github.com/cdornan/regex-pcre-text.git+ Tag: 0.94.0.0++Library+ Hs-Source-Dirs: .+ Exposed-Modules:+ Text.Regex.PCRE.Text+ Text.Regex.PCRE.Text.Lazy++ Default-Language: Haskell2010++ Other-Extensions:+ MultiParamTypeClasses++ GHC-Options:+ -Wall+ -fwarn-tabs++ Build-depends:+ base >= 4 && < 5+ , array >= 0.4 && < 0.6+ , bytestring == 0.10.*+ , regex-base == 0.93.*+ , regex-pcre-builtin == 0.94.*+ , regex-tdfa-text == 1.0.*+ , text == 1.2.*