From: Andy S. <And...@co...> - 2010-07-08 12:24:13
|
diffing dir... Thu Jul 8 08:20:10 EDT 2010 Andy Stewart <laz...@gm...> * Add textBufferSetByteString and textBufferGetByteString for *huge* file. Ignore-this: f7c8f643dc2cd1be2ac3c42f4e29640c { hunk ./gtk/Graphics/UI/Gtk/Multiline/TextBuffer.chs 91 + textBufferSetByteString, + textBufferGetByteString, hunk ./gtk/Graphics/UI/Gtk/Multiline/TextBuffer.chs 201 +import Data.ByteString (ByteString) +import Data.ByteString.Internal (c_strlen) +import Data.ByteString.Unsafe (unsafeUseAsCStringLen, unsafePackCStringFinalizer) hunk ./gtk/Graphics/UI/Gtk/Multiline/TextBuffer.chs 433 +-- | Same as 'textBufferSetText', but use to read *huge* file. +-- When you read content from huge file, 'ByteString' will save +-- much memory than 'String'. +-- NOTE, you need make sure 'ByteString' is a valid UTF-8 [_$_] +-- when you call this function. +-- +-- Below is example code to read huge file. +-- +-- textBufferSetByteString textBuffer =<< Data.ByteString.readFile "hugeFile" +-- +textBufferSetByteString :: TextBufferClass self => self + -> ByteString -- ^ @text@ - text to insert + -> IO () +textBufferSetByteString self text = + unsafeUseAsCStringLen text $ \(textPtr, len) -> + {# call text_buffer_set_text #} + (toTextBuffer self) + textPtr + (fromIntegral len) + +-- | Same as `textBufferGetText`, but use to get *huge* string. +textBufferGetByteString :: TextBufferClass self => self + -> TextIter -- ^ @start@ - start of a range + -> TextIter -- ^ @end@ - end of a range + -> Bool -- ^ @includeHiddenChars@ - whether to include invisible text + -> IO ByteString +textBufferGetByteString self start end includeHiddenChars = + {# call unsafe text_buffer_get_text #} + (toTextBuffer self) + start + end + (fromBool includeHiddenChars) + >>= \strPtr -> do + strLen <- c_strlen strPtr + unsafePackCStringFinalizer (castPtr strPtr) (fromIntegral strLen) ({#call unsafe g_free#} (castPtr strPtr)) + hunk ./gtk/gtk.cabal 125 - array, containers, haskell98, mtl, + array, containers, haskell98, mtl, bytestring, } |