Modifies inflate() to return a discardable bool. Refs #1048.

This commit is contained in:
Grzegorz Adam Hankiewicz 2014-03-29 12:23:21 +01:00
commit d41988d8b2

View file

@ -293,26 +293,17 @@ proc uncompress*(sourceBuf: cstring, sourceLen: int): string =
result = decompressed result = decompressed
proc inflate*(buffer: var string) = proc inflate*(buffer: var string): bool {.discardable.} =
## Convenience proc which inflates a string containing compressed data. ## Convenience proc which inflates a string containing compressed data.
## ##
## Passing a nil string will crash this proc in release mode and assert in ## Passing a nil string will crash this proc in release mode and assert in
## debug mode. It is ok to pass a buffer which doesn't contain deflated data, ## debug mode. It is ok to pass a buffer which doesn't contain deflated data,
## in this case the proc won't modify the buffer. To check if data was ## in this case the proc won't modify the buffer.
## inflated compare the final length of the `buffer` with its original value.
## Example:
## ##
## .. code-block:: nimrod ## Returns true if `buffer` was successfully inflated.
## var data: string
## # Put something into data.
## let originalLen = len(data)
## data.inflate()
## if originalLen != len(data):
## echo "Data was inflated!"
## else:
## echo "Nothing to inflate"
assert (not buffer.isNil) assert (not buffer.isNil)
if buffer.len < 1: return if buffer.len < 1: return
let temp = uncompress(addr(buffer[0]), buffer.len) let temp = uncompress(addr(buffer[0]), buffer.len)
if not temp.isNil: if not temp.isNil:
buffer = temp buffer = temp
result = true