X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=JVM%2FCommon.hs;h=422cc8b88b3a7562861c9ca63c64401c730b1fa9;hb=33dc6f7bdcdc770332723ea519df4c923eb3d933;hp=65a6c6fcf8425eb1f75e70ed2991c998e495e322;hpb=786dff755133ccc2e44445d9c47baa62010e87e4;p=hs-java.git diff --git a/JVM/Common.hs b/JVM/Common.hs index 65a6c6f..422cc8b 100644 --- a/JVM/Common.hs +++ b/JVM/Common.hs @@ -5,17 +5,16 @@ module JVM.Common poolSize, (!), showListIx, + mapFindIndex, byteString ) where -import Codec.Binary.UTF8.String hiding (encode, decode) import Data.Binary import Data.Binary.Put import qualified Data.ByteString.Lazy as B -import Data.Char -import Data.String import qualified Data.Map as M import Data.Default +import Data.List import JVM.ClassFile @@ -25,9 +24,6 @@ instance Default B.ByteString where instance Default Word16 where def = 0 -instance IsString B.ByteString where - fromString s = B.pack $ map (fromIntegral . ord) $ encodeString s - toCharList :: B.ByteString -> [Int] toCharList bstr = map fromIntegral $ B.unpack bstr @@ -37,10 +33,16 @@ poolSize = M.size (!) :: (Ord k) => M.Map k a -> k -> a (!) = (M.!) -showListIx :: (Show a) => [a] -> String -showListIx list = unlines $ zipWith s [1..] list - where s i x = show i ++ ":\t" ++ show x +showListIx :: (Show i, Show a) => [(i,a)] -> String +showListIx list = unlines $ map s list + where s (i, x) = show i ++ ":\t" ++ show x byteString :: (Binary t) => t -> B.ByteString byteString x = runPut (put x) +mapFindIndex :: (Num k) => (v -> Bool) -> M.Map k v -> Maybe k +mapFindIndex check m = + case find (check . snd) (M.assocs m) of + Nothing -> Nothing + Just (k,_) -> Just k +