[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:45:06 UTC 2010


The following commit has been merged in the master branch:
commit 82c24fe7207480a709f0467007dbac84fc34afed
Author: John Goerzen <jgoerzen at complete.org>
Date:   Thu Oct 21 20:06:53 2004 +0100

    Added Path.hs and various utilities
    
    Keywords:
    
    
    (jgoerzen at complete.org--projects/missingh--head--1.0--patch-91)

diff --git a/ChangeLog b/ChangeLog
index 3231e8a..291d55b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,22 @@
 # arch-tag: automatic-ChangeLog--jgoerzen at complete.org--projects/missingh--head--1.0
 #
 
+2004-10-21 14:06:53 GMT	John Goerzen <jgoerzen at complete.org>	patch-91
+
+    Summary:
+      Added Path.hs and various utilities
+    Revision:
+      missingh--head--1.0--patch-91
+
+
+    new files:
+     libsrc/MissingH/Path.hs testsrc/Pathtest.hs
+
+    modified files:
+     ChangeLog Setup.description libsrc/MissingH/List.hs
+     testsrc/Listtest.hs testsrc/Tests.hs
+
+
 2004-10-21 11:30:50 GMT	John Goerzen <jgoerzen at complete.org>	patch-90
 
     Summary:
diff --git a/Setup.description b/Setup.description
index ca6e04c..df3ad11 100644
--- a/Setup.description
+++ b/Setup.description
@@ -14,6 +14,7 @@ Modules: MissingH.IO, MissingH.IO.Binary, MissingH.List,
     MissingH.Hsemail.Rfc2822,
   MissingH.Str,
   MissingH.Cmd,
+  MissingH.FiniteMap, MissingH.Path,
   MissingH.Wash.Mail.Email,
     MissingH.Wash.Mail.EmailConfig,
     MissingH.Wash.Mail.HeaderField,
diff --git a/libsrc/MissingH/List.hs b/libsrc/MissingH/List.hs
index 328f07a..a4faf4f 100644
--- a/libsrc/MissingH/List.hs
+++ b/libsrc/MissingH/List.hs
@@ -42,11 +42,12 @@ module MissingH.List(-- * Tests
                      -- * Conversions
                      split, join, genericJoin, trunc,
                      -- * Miscellaneous
-                     countElem
+                     countElem, elemRIndex, alwaysElemRIndex
                      -- -- * Sub-List Selection
                      -- sub,
                     ) where
-import Data.List(intersperse, concat, isPrefixOf, isSuffixOf, elemIndices)
+import Data.List(intersperse, concat, isPrefixOf, isSuffixOf, elemIndices,
+                elemIndex, elemIndices)
 import IO
 import System.IO.Unsafe
 
@@ -178,3 +179,18 @@ flipAL oldl =
 given list. -}
 countElem :: Eq a => a -> [a] -> Int
 countElem i l = length (elemIndices i l)
+
+{- | Returns the rightmost index of the given element in the
+given list. -}
+elemRIndex :: Eq a => a -> [a] -> Maybe Int
+elemRIndex item l =
+    case reverse $ elemIndices item l of
+                                   [] -> Nothing
+                                   (x:_) -> Just x
+{- | Like elemRIndex, but returns -1 if there is nothing
+found. -}
+alwaysElemRIndex :: Eq a => a -> [a] -> Int
+alwaysElemRIndex item list =
+    case elemRIndex item list of
+                              Nothing -> -1
+                              Just x -> x
diff --git a/libsrc/MissingH/Threads.hs b/libsrc/MissingH/Path.hs
similarity index 61%
copy from libsrc/MissingH/Threads.hs
copy to libsrc/MissingH/Path.hs
index 5332e61..83e1c07 100644
--- a/libsrc/MissingH/Threads.hs
+++ b/libsrc/MissingH/Path.hs
@@ -1,4 +1,4 @@
-{- arch-tag: Thread utilities main file
+{- arch-tag: Path utilities main file
 Copyright (C) 2004 John Goerzen <jgoerzen at complete.org>
 
 This program is free software; you can redistribute it and/or modify
@@ -17,7 +17,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 -}
 
 {- |
-   Module     : MissingH.Threads
+   Module     : MissingH.Path
    Copyright  : Copyright (C) 2004 John Goerzen
    License    : GNU GPL, version 2 or above
 
@@ -26,28 +26,26 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    Stability  : provisional
    Portability: portable
 
-This module provides various helpful utilities for dealing with threads.
+This module provides various helpful utilities for dealing with path and file
+names.
 
 Written by John Goerzen, jgoerzen\@complete.org
 -}
 
-module MissingH.Threads(-- * I\/O utilities
-                        runInThread
-                       )
+module MissingH.Path(splitext
+                    )
 where
-
-import Control.Concurrent
-
-{- | Takes a IO action and a function.  The IO action will be called in a 
-separate thread.  When it is completed, the specified function is called with
-its result.  This is a simple way of doing callbacks. -}
-
-runInThread :: IO a -> (a -> IO b) -> IO ThreadId
-runInThread action callback = 
-    let computation :: IO ()
-        computation = do
-                      x <- action
-                      callback x
-                      return ()
+import Data.List
+import MissingH.List
+
+{- | Splits a pathname into a tuple representing the root of the name and
+the extension.  The extension is considered to be all characters from the last
+dot after the last slash to the end.  Either returned string may be empty. -}
+splitext :: String -> (String, String)
+splitext path = 
+    let dotindex = alwaysElemRIndex '.' path
+        slashindex = alwaysElemRIndex '/' path
         in
-        forkIO computation
+        if dotindex <= slashindex
+           then (path, "")
+           else ((take dotindex path), (drop dotindex path))
\ No newline at end of file
diff --git a/testsrc/Listtest.hs b/testsrc/Listtest.hs
index abe1a5c..872a398 100644
--- a/testsrc/Listtest.hs
+++ b/testsrc/Listtest.hs
@@ -105,6 +105,25 @@ test_contains =
         f "t10" "a" "Hello" False
         f "t11" "e" "Hello" True
 
+test_elemRIndex =
+    let f item inp exp = exp @=? elemRIndex item inp in
+        do
+        f "foo" [] Nothing
+        f "foo" ["bar", "baz"] Nothing
+        f "foo" ["foo"] (Just 0)
+        f "foo" ["foo", "bar"] (Just 0)
+        f "foo" ["bar", "foo"] (Just 1)
+        f "foo" ["foo", "bar", "foo", "bar", "foo"] (Just 4)
+        f 'f' ['f', 'b', 'f', 'f', 'b'] (Just 3)
+        f 'f' ['b', 'b', 'f'] (Just 2)
+
+test_alwaysElemRIndex =
+    let f item inp exp = exp @=? alwaysElemRIndex item inp in
+        do
+        f "foo" [] (-1)
+        f 'f' ['b', 'q'] (-1)
+        f 'f' ['f', 'b', 'f', 'f', 'b'] 3
+
 tests = TestList [TestLabel "delFromAL" (TestCase test_delFromAL),
                   TestLabel "addToAL" (TestCase test_addToAL),
                   TestLabel "split" (TestCase test_split),
@@ -112,6 +131,8 @@ tests = TestList [TestLabel "delFromAL" (TestCase test_delFromAL),
                   TestLabel "genericJoin" (TestCase test_genericJoin),
                   TestLabel "trunc" (TestCase test_trunc),
                   TestLabel "flipAL" (TestCase test_flipAL),
+                  TestLabel "elemRIndex" (TestCase test_elemRIndex),
+                  TestLabel "alwaysElemRIndex" (TestCase test_alwaysElemRIndex),
                   TestLabel "contains" (TestCase test_contains)]
 
 
diff --git a/testsrc/Testutil.hs b/testsrc/Pathtest.hs
similarity index 60%
copy from testsrc/Testutil.hs
copy to testsrc/Pathtest.hs
index 5eceef9..882b3b7 100644
--- a/testsrc/Testutil.hs
+++ b/testsrc/Pathtest.hs
@@ -1,4 +1,4 @@
-{- arch-tag: Test utilities
+{- arch-tag: Path tests main file
 Copyright (C) 2004 John Goerzen <jgoerzen at complete.org>
 
 This program is free software; you can redistribute it and/or modify
@@ -16,13 +16,19 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 -}
 
-module Testutil(mapassertEqual) where
+module Pathtest(tests) where
 import HUnit
+import MissingH.Path
 
-mapassertEqual :: (Show b, Eq b) => String -> (a -> b) -> [(a, b)] -> Assertion
-mapassertEqual descrip func [] = return ()
-mapassertEqual descrip func ((inp,result):xs) = 
-    do
-    assertEqual descrip result (func inp)
-    mapassertEqual descrip func xs
+test_splitext =
+    let f inp exp = exp @=? splitext inp in
+        do
+        f "" ("", "")
+        f "/usr/local" ("/usr/local", "")
+        f "../foo.txt" ("../foo", ".txt")
+        f "../bar.txt.gz" ("../bar.txt", ".gz")
+        f "foo.txt/bar" ("foo.txt/bar", "")
+        f "foo.txt/bar.bz" ("foo.txt/bar", ".bz")
 
+tests = TestList [TestLabel "splitext" (TestCase test_splitext)
+                 ]
\ No newline at end of file
diff --git a/testsrc/Tests.hs b/testsrc/Tests.hs
index 76907c6..2e890d1 100644
--- a/testsrc/Tests.hs
+++ b/testsrc/Tests.hs
@@ -20,6 +20,7 @@ module Tests(tests) where
 import HUnit
 import qualified Listtest
 import qualified FiniteMaptest
+import qualified Pathtest
 import qualified Strtest
 import qualified IOtest
 
@@ -28,6 +29,7 @@ test1 = TestCase ("x" @=? "x")
 tests = TestList [TestLabel "test1" test1,
                  TestLabel "List" Listtest.tests,
                  TestLabel "Str" Strtest.tests,
-                 TestLabel "FiniteMap" FiniteMaptest.tests]
+                 TestLabel "FiniteMap" FiniteMaptest.tests,
+                 TestLabel "Path" Pathtest.tests]
 
 

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list