[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36
John Goerzen
jgoerzen at complete.org
Fri Apr 23 15:09:01 UTC 2010
The following commit has been merged in the master branch:
commit 471acc09adf0a5331cab8d2268ce9b3e82a563bb
Author: John Goerzen <jgoerzen at complete.org>
Date: Fri Apr 7 22:51:33 2006 +0100
New function escapeRe and new unit tests for it
diff --git a/MissingH/Str.hs b/MissingH/Str.hs
index ff3bca0..f46cbe3 100644
--- a/MissingH/Str.hs
+++ b/MissingH/Str.hs
@@ -40,9 +40,10 @@ module MissingH.Str
-- * Conversions
-- | Note: Some of these functions are aliases for functions
-- in "MissingH.List".
- join, split, splitWs, splitRe, replace, subRe
+ join, split, splitWs, splitRe, replace, subRe, escapeRe
) where
import MissingH.List(startswith, endswith, join, split, replace)
+import Data.Char
import Text.Regex
{-# DEPRECATED splitRe "Use Text.Regex.splitRegex instead" #-}
@@ -110,3 +111,18 @@ splitRe = splitRegex
list are automatically removed. -}
splitWs :: String -> [String]
splitWs = filter (\x -> x /= []) . splitRegex (mkRegex "[ \t\n\r\v\f]+")
+
+{- | Escape all characters in the input pattern that are not alphanumeric.
+
+Does not make special allowances for NULL, which isn't valid in a
+Haskell regular expression pattern. -}
+escapeRe :: String -> String
+escapeRe [] = []
+escapeRe (x:xs)
+ -- Chars that we never escape
+ | x `elem` ['\'', '`'] = x : escapeRe xs
+ -- General rules for chars we never escape
+ | isDigit x || (isAscii x && isAlpha x) || x `elem` ['<', '>']
+ = x : escapeRe xs
+ -- Escape everything else
+ | otherwise = '\\' : x : escapeRe xs
diff --git a/testsrc/Strtest.hs b/testsrc/Strtest.hs
index 1bd7262..1c2f3b7 100644
--- a/testsrc/Strtest.hs
+++ b/testsrc/Strtest.hs
@@ -21,6 +21,7 @@ import Test.HUnit
import MissingH.Str
import MissingH.HUnit
import Text.Regex
+import Data.Char
test_lstrip =
mapassertEqual "lstrip" lstrip
@@ -83,12 +84,24 @@ test_subRe =
,f "foo" "Test foo bar" "x\\\\x" "Test x\\x bar"
]
+test_escapeRe =
+ map (\i -> TestLabel (show $ chr i) $ TestCase $ assertEqual [chr i] (Just [])
+ (matchRegex (mkRegex $ escapeRe $ [chr i]) [chr i]))
+ [1..255]
+ ++
+ [TestCase $ assertEqual "big string"
+ (Just ([], teststr, [], []))
+ (matchRegexAll (mkRegex $ escapeRe teststr) teststr)
+ ]
+ where teststr = map chr [1..255]
+
tests = TestList [TestLabel "lstrip" (TestList test_lstrip),
TestLabel "rstrip" $ TestList test_rstrip,
TestLabel "strip" $ TestList test_strip,
TestLabel "splitWs" $ TestList test_splitWs,
TestLabel "subRe" $ TestList test_subRe,
- TestLabel "splitRe" $ TestList test_splitRe
+ TestLabel "splitRe" $ TestList test_splitRe,
+ TestLabel "escapeRe" $ TestList test_escapeRe
]
--
haskell-testpack
More information about the Pkg-haskell-commits
mailing list