[project @ 2005-11-04 15:48:25 by simonmar]
[ghc-hetmet.git] / ghc / compiler / utils / Util.lhs
index 3f2b452..1598c12 100644 (file)
@@ -13,7 +13,7 @@ module Util (
        mapAndUnzip, mapAndUnzip3,
        nOfThem, filterOut,
        lengthExceeds, lengthIs, lengthAtLeast, listLengthCmp, atLength,
-       isSingleton, only,
+       isSingleton, only, singleton,
        notNull, snocView,
 
        isIn, isn'tIn,
@@ -64,12 +64,13 @@ module Util (
        -- Filename utils
        Suffix,
        splitFilename, suffixOf, basenameOf, joinFileExt,
-       splitFilenameDir, joinFileExt, joinFileName,
+       splitFilenameDir, joinFileName,
        splitFilename3,
        splitLongestPrefix,
        replaceFilenameSuffix, directoryOf, filenameOf,
        replaceFilenameDirectory,
        escapeSpaces, isPathSeparator,
+       parseSearchPath,
        normalisePath, platformPath, pgmPath,
     ) where
 
@@ -299,6 +300,9 @@ listLengthCmp = atLength atLen atEnd
   atLen []     = EQ
   atLen _      = GT
 
+singleton :: a -> [a]
+singleton x = [x]
+
 isSingleton :: [a] -> Bool
 isSingleton [x] = True
 isSingleton  _  = False
@@ -913,11 +917,12 @@ joinFileName dir fname = dir ++ '/':fname
 -- string is returned in the first component (and the second one is just
 -- empty).
 splitLongestPrefix :: String -> (Char -> Bool) -> (String,String)
-splitLongestPrefix s pred
-  = case pre of
-       []      -> (reverse suf, [])
-       (_:pre) -> (reverse pre, reverse suf)
-  where (suf,pre) = break pred (reverse s)
+splitLongestPrefix str pred
+  | null r_pre = (str,           [])
+  | otherwise  = (reverse (tail r_pre), reverse r_suf)
+       -- 'tail' drops the char satisfying 'pred'
+  where 
+    (r_suf, r_pre) = break pred (reverse str)
 
 replaceFilenameSuffix :: FilePath -> Suffix -> FilePath
 replaceFilenameSuffix file suf = basenameOf file `joinFileExt` suf
@@ -946,6 +951,40 @@ isPathSeparator ch =
   ch == '/'
 #endif
 
+--------------------------------------------------------------
+-- * Search path
+--------------------------------------------------------------
+
+-- | The function splits the given string to substrings
+-- using the 'searchPathSeparator'.
+parseSearchPath :: String -> [FilePath]
+parseSearchPath path = split path
+  where
+    split :: String -> [String]
+    split s =
+      case rest' of
+        []     -> [chunk] 
+        _:rest -> chunk : split rest
+      where
+        chunk = 
+          case chunk' of
+#ifdef mingw32_HOST_OS
+            ('\"':xs@(_:_)) | last xs == '\"' -> init xs
+#endif
+            _                                 -> chunk'
+
+        (chunk', rest') = break (==searchPathSeparator) s
+
+-- | A platform-specific character used to separate search path strings in 
+-- environment variables. The separator is a colon (\":\") on Unix and Macintosh, 
+-- and a semicolon (\";\") on the Windows operating system.
+searchPathSeparator :: Char
+#if mingw32_HOST_OS || mingw32_TARGET_OS
+searchPathSeparator = ';'
+#else
+searchPathSeparator = ':'
+#endif
+
 -----------------------------------------------------------------------------
 -- Convert filepath into platform / MSDOS form.