Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #255 from hnakamur/add-Buffer-toString
add Buffer:toString(i, j)
  • Loading branch information
rphillips committed Jun 26, 2012
2 parents 3c834e1 + b2b4ee8 commit ab6fc9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/luvit/buffer.lua
Expand Up @@ -142,4 +142,9 @@ function Buffer:readInt32BE(offset)
return bit.tobit(self:readUInt32BE(offset))
end

function Buffer:toString(i, j)
local offset = i and i - 1 or 0
return ffi.string(self.ctype + offset, (j or self.length) - offset)
end

return buffer
6 changes: 6 additions & 0 deletions tests/test-buffer.lua
Expand Up @@ -52,3 +52,9 @@ assert(buf:readUInt32LE(1) == 0x422304FB)
assert(buf:readInt32BE(1) == -0x04FBDCBE)
assert(buf:readInt32LE(1) == 0x422304FB)
local buf2 = Buffer:new('abcd')
assert(tostring(buf2) == 'abcd')
assert(buf2:toString(1, 2) == 'ab')
assert(buf2:toString(2, 3) == 'bc')
assert(buf2:toString(3) == 'cd')
assert(buf2:toString() == 'abcd')

0 comments on commit ab6fc9e

Please sign in to comment.