2 % (c) The University of Glasgow, 1997-2003
4 \section{String buffers}
6 Buffers for scanning string input stored in external arrays.
13 -- * Creation/destruction
14 hGetStringBuffer, -- :: FilePath -> IO StringBuffer
15 stringToStringBuffer, -- :: String -> IO StringBuffer
18 currentChar, -- :: StringBuffer -> Char
19 prevChar, -- :: StringBuffer -> Char -> Char
20 lookAhead, -- :: StringBuffer -> Int -> Char
21 atEnd, -- :: StringBuffer -> Bool
22 difference, -- :: StringBuffer -> StringBuffer -> Int
28 lexemeToString, -- :: StringBuffer -> Int -> String
29 lexemeToFastString, -- :: StringBuffer -> Int -> FastString
32 #include "HsVersions.h"
41 #if __GLASGOW_HASKELL__ < 503
46 import GHC.IO ( slurpFile )
49 import IO ( openFile, hFileSize, IOMode(ReadMode) )
50 #if __GLASGOW_HASKELL__ >= 601
51 import System.IO ( openBinaryFile )
53 import IOExts ( openFileEx, IOModeEx(..) )
56 #if __GLASGOW_HASKELL__ < 503
57 import IArray ( listArray )
58 import ArrayBase ( UArray(..) )
60 import IOExts ( hGetBufBA )
62 import Data.Array.IArray ( listArray )
63 import Data.Array.MArray ( unsafeFreeze, newArray_ )
64 import Data.Array.Base ( UArray(..) )
65 import Data.Array.IO ( IOArray, hGetArray )
70 #if __GLASGOW_HASKELL__ < 601
71 openBinaryFile fp mode = openFileEx fp (BinaryMode mode)
73 -- -----------------------------------------------------------------------------
74 -- The StringBuffer type
76 -- A StringBuffer is a ByteArray# with a pointer into it. We also cache
77 -- the length of the ByteArray# for speed.
85 instance Show StringBuffer where
86 showsPrec _ s = showString "<stringbuffer>"
88 -- -----------------------------------------------------------------------------
89 -- Creation / Destruction
91 hGetStringBuffer :: FilePath -> IO StringBuffer
92 hGetStringBuffer fname = do
93 h <- openBinaryFile fname ReadMode
95 let size_i@(I# sz#) = fromIntegral size
96 #if __GLASGOW_HASKELL__ < 503
97 arr <- stToIO (newCharArray (0,size_i-1))
98 r <- hGetBufBA h arr size_i
100 arr <- newArray_ (0,size_i-1)
101 r <- hGetArray h arr size_i
104 then ioError (userError "short read of file")
106 #if __GLASGOW_HASKELL__ < 503
107 frozen <- stToIO (unsafeFreezeByteArray arr)
109 ByteArray _ _ bytearr# -> return (StringBuffer bytearr# sz# 0#)
111 frozen <- unsafeFreeze arr
113 UArray _ _ bytearr# -> return (StringBuffer bytearr# sz# 0#)
116 #if __GLASGOW_HASKELL__ >= 502
117 stringToStringBuffer str = do
118 let size@(I# sz#) = length str
119 arr = listArray (0,size-1) (map (fromIntegral.ord) str)
122 UArray _ _ bytearr# -> return (StringBuffer bytearr# sz# 0#)
124 stringToStringBuffer = panic "stringToStringBuffer: not implemented"
127 -- -----------------------------------------------------------------------------
130 currentChar :: StringBuffer -> Char
131 currentChar (StringBuffer arr# l# current#) =
132 ASSERT(current# <# l#)
133 C# (indexCharArray# arr# current#)
135 prevChar :: StringBuffer -> Char -> Char
136 prevChar (StringBuffer _ _ 0#) deflt = deflt
137 prevChar s deflt = lookAhead s (-1)
139 lookAhead :: StringBuffer -> Int -> Char
140 lookAhead (StringBuffer arr# l# c#) (I# i#) =
141 ASSERT(off <# l# && off >=# 0#)
142 C# (indexCharArray# arr# off)
146 difference :: StringBuffer -> StringBuffer -> Int
147 difference (StringBuffer _ _ c1#) (StringBuffer _ _ c2#) = I# (c2# -# c1#)
149 -- -----------------------------------------------------------------------------
152 stepOn :: StringBuffer -> StringBuffer
153 stepOn s = stepOnBy 1 s
155 stepOnBy :: Int -> StringBuffer -> StringBuffer
156 stepOnBy (I# i#) (StringBuffer fo# l# c#) = StringBuffer fo# l# (c# +# i#)
158 atEnd :: StringBuffer -> Bool
159 atEnd (StringBuffer _ l# c#) = l# ==# c#
161 -- -----------------------------------------------------------------------------
164 lexemeToString :: StringBuffer -> Int -> String
165 lexemeToString _ 0 = ""
166 lexemeToString (StringBuffer arr# _ current#) (I# len#) = unpack current#
168 end = current# +# len#
172 | otherwise = C# ch : unpack (nh +# 1#)
174 ch = indexCharArray# arr# nh
176 lexemeToFastString :: StringBuffer -> Int -> FastString
177 lexemeToFastString _ 0 = mkFastString ""
178 lexemeToFastString (StringBuffer fo _ current#) (I# len) =
179 mkFastSubStringBA# fo current# len