hummus-recipe
Version:
A powerful PDF tool for NodeJS based on HummusJS
64 lines (44 loc) • 1.51 kB
text/coffeescript
{Number:NumberT} = require './Number'
utils = require './utils'
class StringT
constructor: (, = 'ascii') ->
decode: (stream, parent) ->
length = if ?
utils.resolveLength , stream, parent
else
{buffer, length, pos} = stream
while pos < length and buffer[pos] isnt 0x00
++pos
pos - stream.pos
encoding =
if typeof encoding is 'function'
encoding = encoding.call(parent, parent) or 'ascii'
string = stream.readString(length, encoding)
if not ? and stream.pos < stream.length
stream.pos++
return string
size: (val, parent) ->
# Use the defined value if no value was given
unless val
return utils.resolveLength , null, parent
encoding =
if typeof encoding is 'function'
encoding = encoding.call(parent?.val, parent?.val) or 'ascii'
if encoding is 'utf16be'
encoding = 'utf16le'
size = Buffer.byteLength(val, encoding)
if instanceof NumberT
size += .size()
if not ?
size++
return size
encode: (stream, val, parent) ->
encoding =
if typeof encoding is 'function'
encoding = encoding.call(parent?.val, parent?.val) or 'ascii'
if instanceof NumberT
.encode(stream, Buffer.byteLength(val, encoding))
stream.writeString(val, encoding)
if not ?
stream.writeUInt8(0x00)
module.exports = StringT