Make Control.Exception buildable by nhc98.
[haskell-directory.git] / Data / PackedString.hs
index 8d15561..1160d34 100644 (file)
@@ -8,14 +8,20 @@
 -- Stability   :  experimental
 -- Portability :  portable
 --
--- An efficient implementation of strings.
+-- This API is deprecated.  You might be able to use "Data.ByteString"
+-- or "Data.ByteString.Char8", provided you don't need full Unicode support.
+-- The long term aim is to provide a Unicode layer on "Data.ByteString",
+-- and then to provide a replacement for this "Data.PackedString" API based on
+-- that.
 --
 -----------------------------------------------------------------------------
 
 -- Original GHC implementation by Bryan O\'Sullivan, 
 -- rewritten to use UArray by Simon Marlow.
 
-module Data.PackedString (
+module Data.PackedString 
+  {-# DEPRECATED "use Data.ByteString, Data.ByteString.Char8, or plain String." #-}
+  (
        -- * The @PackedString@ type
         PackedString,      -- abstract, instances: Eq, Ord, Show, Typeable
 
@@ -71,7 +77,7 @@ import Prelude
 
 import Data.Array.Unboxed
 import Data.Array.IO
-import Data.Dynamic
+import Data.Typeable
 import Data.Char
 
 import System.IO
@@ -83,6 +89,11 @@ import System.IO
 -- efficient operations.  A 'PackedString' contains full Unicode 'Char's.
 newtype PackedString = PS (UArray Int Char)
 
+-- ToDo: we could support "slices", i.e. include offset and length fields into
+-- the string, so that operations like take/drop could be O(1).  Perhaps making
+-- a slice should be conditional on the ratio of the slice/string size to
+-- limit memory leaks.
+
 instance Eq PackedString where
    (PS x) == (PS y)  =  x == y
 
@@ -94,7 +105,7 @@ instance Ord PackedString where
 instance Show PackedString where
     showsPrec p ps r = showsPrec p (unpackPS ps) r
 
-#include "Dynamic.h"
+#include "Typeable.h"
 INSTANCE_TYPEABLE0(PackedString,packedStringTc,"PackedString")
 
 -- -----------------------------------------------------------------------------
@@ -116,7 +127,7 @@ packString str = packNChars (length str) str
 -- | The 'packNChars' function creates a 'PackedString' out of the
 -- first @len@ elements of the given 'String'.
 packNChars :: Int -> [Char] -> PackedString
-packNChars len str = PS (array (0,len-1) (zip [0..] str))
+packNChars len str = PS (listArray (0,len-1) str)
 
 -- -----------------------------------------------------------------------------
 -- Destructor functions (taking PackedStrings apart)
@@ -261,7 +272,7 @@ joinPS filler pss = concatPS (splice pss)
   * joinPS (packString [x]) (splitPS x ls) = ls
 -}
 
--- | The 'splitPS' function splits the input string on each occurance of the given 'Char'.
+-- | The 'splitPS' function splits the input string on each occurrence of the given 'Char'.
 splitPS :: Char -> PackedString -> [PackedString]
 splitPS c = splitWithPS (== c)
 
@@ -309,7 +320,7 @@ substrPS (PS ps) begin end = packString [ ps ! i | i <- [begin..end] ]
 -- | Outputs a 'PackedString' to the specified 'Handle'.
 --
 -- NOTE: the representation of the 'PackedString' in the file is assumed to
--- be in the ISO-8859-1 encoding.  In other words, only the least signficant
+-- be in the ISO-8859-1 encoding.  In other words, only the least significant
 -- byte is taken from each character in the 'PackedString'.
 hPutPS :: Handle -> PackedString -> IO ()
 hPutPS h (PS ps) = do
@@ -332,7 +343,7 @@ hGetPS h i = do
   arr <- newArray_ (0, i-1)
   l <- hGetArray h arr i
   chars <- mapM (\i -> readArray arr i >>= return.chr.fromIntegral) [0..l-1]
-  return (packString chars)
+  return (packNChars l chars)
 
 #else  /* __NHC__ */