summaryrefslogtreecommitdiff
path: root/testsuite/tests/codeGen/should_compile/cg005.hs
blob: a25ad4250a02a5f042cbf7dbabbe1a38fce3de23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module Bug where

import Foreign hiding ( unsafePerformIO )
import Foreign.ForeignPtr
import Data.Char
import System.IO.Unsafe

data PackedString = PS !(ForeignPtr Word8) !Int !Int

(!) :: PackedString -> Int -> Word8
(PS x s _l) ! i
    = unsafePerformIO $ withForeignPtr x $ \p -> peekElemOff p (s+i)

w2c :: Word8 -> Char
w2c = chr . fromIntegral

indexPS :: PackedString -> Int -> Char
indexPS theps i | i < 0 = error "Negative index in indexPS"
                | otherwise = w2c $ theps ! i