pcre-light-extra (empty) → 0.0.0
raw patch · 4 files changed
+213/−0 lines, 4 filesdep +basedep +bytestringdep +pcre-lightsetup-changed
Dependencies added: base, bytestring, pcre-light
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- pcre-light-extra.cabal +70/−0
- src/Text/Regex/PCRE/Light/Extra.hs +111/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2010, Steffen Siering++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.++ * Neither the name of Steffen Siering nor the names of other+ contributors may 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ pcre-light-extra.cabal view
@@ -0,0 +1,70 @@+-- pcre-ligh-extra.cabal auto-generated by cabal init. For additional+-- options, see+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.+-- The name of the package.+Name: pcre-light-extra++-- The package version. See the Haskell package versioning policy+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for+-- standards guiding when and how versions should be incremented.+Version: 0.0.0++-- A short (one-line) description of the package.+Synopsis: pcre-light extra functionality++-- A longer description of the package.+Description: + pcre-light utility functions for more user friendly usage.+ Instead of adding execution and compile options to the matching and+ compiling functions, the options are added to the regular expression to be+ compiled or run. Furthermore support for different matching return types+ (using typeclass 'MatchResult') and different regular expression types+ (compiled or uncompiled using typeclass 'RegexLike') are supported. See module+ documentation for examples.++-- The license under which the package is released.+License: BSD3++-- The file containing the license text.+License-file: LICENSE++-- The package author(s).+Author: Steffen Siering++-- An email address to which users can send suggestions, bug reports,+-- and patches.+Maintainer: steffen <dot> siering <at> gmail com++Homepage: http://github.com/urso/pcre-light-extra++-- A copyright notice.+-- Copyright: ++Category: Text++Build-type: Simple++-- Extra files to be distributed with the package, such as examples or+-- a README.+-- Extra-source-files: ++-- Constraint on the version of Cabal needed to build this package.+Cabal-version: >=1.2+++Library+ hs-source-dirs: src+ -- Modules exported by the library.+ Exposed-modules: Text.Regex.PCRE.Light.Extra+ + -- Packages needed in order to build this package.+ Build-depends: base >= 4 && <= 5, bytestring,+ pcre-light >= 0.3 && <= 0.4++ + -- Modules not exported by this package.+ -- Other-modules: + + -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.+ -- Build-tools: +
+ src/Text/Regex/PCRE/Light/Extra.hs view
@@ -0,0 +1,111 @@+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE FlexibleInstances #-}++-- | pcre-light utility functions for more user friendly usage.+-- Instead of adding execution and compile options to the matching and+-- compiling functions, the options are added to the regular expression to be+-- compiled or run. Furthermore support for different matching return types+-- (using typeclass 'MatchResult') and different regular expression types+-- (compiled or uncompiled using typeclass 'RegexLike') are supported.+--+-- examples using GHC's -XOverloadedStrings flag:+--+-- simple matching with uncompiled pattern "abc" of type ByteString:+--+-- >>> ("abc" =~ ("abc" :: ByteString)) :: Bool+-- True+--+-- case insensitive matching with uncompiled pattern of type ByteString:+--+-- >>> ("AbCasf" =~ caseSensitive False "abc") :: Bool+-- True+--+-- >>> ("AbCasf" =~ caseSensitive False "abc") :: Maybe [ByteString]+-- Just ["AbC"]+--+module Text.Regex.PCRE.Light.Extra where++import Control.Arrow (right)+import Data.ByteString (ByteString)+import Data.Maybe (isJust)++import qualified Text.Regex.PCRE.Light as R++-- | Uncompiled regular expression with compile and execution options.+-- Compiles to RegexExecPattern preserving execution options.+data RegexPattern = Regex [R.PCREOption] [R.PCREExecOption] ByteString++-- | Compiled regular expression with execution results.+data RegexExecPattern = RegexExec [R.PCREExecOption] R.Regex++-- | Typeclass defining automatic conversion of PCRE matching results+-- to user defined type. Used in typeclass 'RegexLike' and function ('=~').+class MatchResult x where+ convert :: Maybe [ByteString] -> x++instance MatchResult (Maybe [ByteString]) where + convert = id++instance MatchResult Bool where + convert = isJust++-- | RegexLike types can be compiled to regular epxression or directly used+-- as regular expression.+class RegexLike rl where+ -- | Type of compiled regular expression for instance rl.+ type RegexType rl ++ -- | compiles a perl-compatible regular expression to an executable+ -- regular expression. See 'Text.Regex.PCRE.Light.compileM'.+ compile :: rl -> Either String (RegexType rl)++ -- | matches a Bytestring with a regular expression of type rl.+ -- If rl needs to be compiled (for example when rl ~ ByteString or+ -- rl ~ RegexPattern) but is invalid, an exception will be thrown. It is+ -- recommended to use compiled patterns with match for better error handling.+ match :: (MatchResult res) => rl -> ByteString -> res++instance RegexLike R.Regex where+ type RegexType R.Regex = R.Regex++ compile = Right . id+ match regex str = convert $ R.match regex str []++instance RegexLike ByteString where+ type RegexType ByteString = R.Regex++ compile regex = R.compileM regex []+ match pattern str = either error (`match` str) $ compile pattern++instance RegexLike RegexExecPattern where+ type RegexType RegexExecPattern = RegexExecPattern++ compile = Right . id+ match (RegexExec opts regex) str = convert $ R.match regex str opts++instance RegexLike RegexPattern where+ type RegexType RegexPattern = RegexExecPattern++ compile (Regex opt execOpt pattern) = + right (RegexExec execOpt) $ R.compileM pattern opt++ match r str = either error (`match` str) $ compile r++-- | matches a ByteString with a regular expression.+(=~) :: (RegexLike regex, MatchResult ret) => ByteString -> regex -> ret+(=~) = flip match++-- | create regular expression with compile and execution options.+cfg :: [R.PCREOption] -> [R.PCREExecOption] -> ByteString -> RegexPattern+cfg = Regex++-- | Add execution options to compiled regular expression of pcre-light's +-- compiled regular expression type 'R.Regex'+withExecOpts :: [R.PCREExecOption] -> R.Regex -> RegexExecPattern+withExecOpts = RegexExec++-- | Create case sensitive (first parameter is 'True') or case insensitive regular expression from pattern.+caseSensitive :: Bool -> ByteString -> RegexPattern+caseSensitive False = cfg [R.caseless, R.no_utf8_check] []+caseSensitive True = cfg [R.no_utf8_check] []+