[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 14:46:17 UTC 2010


The following commit has been merged in the master branch:
commit 3c04646f688f2cc78fcc4b9134c2a3a39557f9b1
Author: John Goerzen <jgoerzen at complete.org>
Date:   Wed Oct 27 19:40:32 2004 +0100

    Added splitRe, subRe
    
    Keywords:
    
    
    (jgoerzen at complete.org--projects/missingh--head--0.5--patch-19)

diff --git a/ChangeLog b/ChangeLog
index b4cf046..5b3f197 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,19 @@
 # arch-tag: automatic-ChangeLog--jgoerzen at complete.org--projects/missingh--head--0.5
 #
 
+2004-10-27 13:40:32 GMT	John Goerzen <jgoerzen at complete.org>	patch-19
+
+    Summary:
+      Added splitRe, subRe
+    Revision:
+      missingh--head--0.5--patch-19
+
+
+    modified files:
+     ChangeLog debian/changelog libsrc/MissingH/Str.hs notes.txt
+     testsrc/Strtest.hs
+
+
 2004-10-27 01:48:49 GMT	John Goerzen <jgoerzen at complete.org>	patch-18
 
     Summary:
diff --git a/debian/changelog b/debian/changelog
index cbd9f6a..d25bf1e 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -2,6 +2,7 @@ missingh (0.5.2) unstable; urgency=low
 
   * New module: sendmail
   * New popen functions in MissingH.Cmd.
+  * Added splitRe, subRe to Str.hs.
 
  -- John Goerzen <jgoerzen at complete.org>  Tue, 26 Oct 2004 16:38:01 -0500
 
diff --git a/libsrc/MissingH/Str.hs b/libsrc/MissingH/Str.hs
index 969074b..20205c4 100644
--- a/libsrc/MissingH/Str.hs
+++ b/libsrc/MissingH/Str.hs
@@ -40,7 +40,7 @@ module MissingH.Str(-- * Whitespace Removal
                         -- * Conversions
                         -- | Note: Some of these functions are aliases for functions
                         -- in "MissingH.List".
-                        join, split, replace
+                        join, split, splitRe, replace, subRe
                        ) where
 import MissingH.List(startswith, endswith, join, split, replace)
 import Text.Regex
@@ -78,9 +78,43 @@ lstrip s = case s of
 rstrip :: String -> String
 rstrip = reverse . lstrip . reverse
 
-{-
--- | Splits a string based on a regular expression.  The regular express
--- should identify one delimiter.
+{- | Replaces every occurance of the given regexp with the replacement string.
+
+In the replacement string, @\"\\1\"@ refers to the first substring; 
+@\"\\2\"@ to the second, etc; and @\"\\0\"@ to the entire match.
+@\"\\\\\\\\\"@ will insert a literal backslash.
+
+-}
+subRe :: Regex                          -- ^ Search pattern
+      -> String                         -- ^ Input string
+      -> String                         -- ^ Replacement text
+      -> String                         -- ^ Output string
+subRe _ "" _ = ""
+subRe regexp inp repl = 
+    let bre = mkRegex "\\\\(\\\\||[0-9]+)"
+        lookup _ [] _ = []
+        lookup [] _ _ = []
+        lookup match repl groups =
+            case matchRegexAll bre repl of
+                Nothing -> repl
+                Just (lead, _, trail, bgroups) ->
+                    let newval = if (head bgroups) == "\\"
+                                 then "\\"
+                                 else let index = (read (head bgroups)) - 1
+                                          in
+                                          if index == -1
+                                             then match
+                                             else groups !! index
+                        in
+                        lead ++ newval ++ lookup match trail groups
+        in
+        case matchRegexAll regexp inp of
+            Nothing -> inp
+            Just (lead, match, trail, groups) ->
+              lead ++ lookup match repl groups ++ (subRe regexp trail repl)
+
+{- | Splits a string based on a regular expression.  The regular expression
+should identify one delimiter.
 -}
 
 splitRe :: Regex -> String -> [String]
@@ -92,6 +126,7 @@ splitRe delim str =
            if remainder == ""
               then firstline : [] : []
               else firstline : splitRe delim remainder
+
 {-
 
 splitRe :: Regex -> String -> [String]
diff --git a/notes.txt b/notes.txt
index 36eb581..7b3c675 100644
--- a/notes.txt
+++ b/notes.txt
@@ -1,5 +1,3 @@
- see if splitre works
- 
  basename p = reverse $ takeWhile (/= '/') $ reverse p
 
  dirname p  = case reverse $ dropWhile (/= '/') $ reverse p of
diff --git a/testsrc/Strtest.hs b/testsrc/Strtest.hs
index 05606f7..7b6750b 100644
--- a/testsrc/Strtest.hs
+++ b/testsrc/Strtest.hs
@@ -20,6 +20,7 @@ module Strtest(tests) where
 import HUnit
 import MissingH.Str
 import Testutil
+import Text.Regex
 
 test_lstrip =
     mapassertEqual "lstrip" lstrip
@@ -50,9 +51,31 @@ test_strip =
                     ("\nbas", "bas"),
                     ("abc def", "abc def")]
 
+test_splitRe =
+    let f re inp exp = exp @=? splitRe (mkRegex re) inp
+        in do
+           f "foo" "" []
+           f "foo" "foo" ["", ""]
+           f "," "foo,bar,,baz," ["foo", "bar", "", "baz", ""]
+           f "ba" ",foo,bar,,baz," [",foo,","r,,","z,"]
+           f "," "" []
+           f "," "," ["", ""]
+
+test_subRe =
+    let f re inp repl exp = exp @=? subRe (mkRegex re) inp repl
+        in do
+           f "foo" "" "bar" ""
+           f "foo" "This is a foo test bar" "bar" "This is a bar test bar"
+           f "foo" "Test foo bar" "x\\0x" "Test xfoox bar"
+           f "(f)(o)o" "Test foo bar" "\\2\\1" "Test of bar"
+           f "foo" "Test foo then foo bar" "" "Test  then  bar"
+           f "foo" "Test foo bar" "x\\\\x" "Test x\\x bar"
+
 tests = TestList [TestLabel "lstrip" (TestCase test_lstrip),
                   TestCase test_rstrip,
-                  TestCase test_strip
+                  TestCase test_strip,
+                  TestCase test_subRe,
+                  TestCase test_splitRe
                   ]
 
 

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list