[project @ 2004-08-18 17:48:44 by krasimir]
[haskell-directory.git] / Data / PackedString.hs
index cd4fad9..58c4981 100644 (file)
@@ -83,6 +83,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
 
@@ -116,7 +121,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)
@@ -332,7 +337,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__ */