Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Forked from bigeasy/gist:584692
Created September 17, 2010 18:30
Show Gist options
  • Save jashkenas/584699 to your computer and use it in GitHub Desktop.
Save jashkenas/584699 to your computer and use it in GitHub Desktop.
write = (buffer, offset, length) ->
offset or= 0
length or= buffer.length
while machine and offset < length
pattern = machine.definition.pattern[machine.index]
if pattern.arrayed
loop
buffer[offset] = machine.value[machine.offset]
machine.offset += machine.increment
bytesWritten++
offset++
break if machine.offset is machine.terminal
return true if offset is length
else
loop
buffer[offset] = Math.floor(machine.value / Math.pow(256, machine.offset)) & 0xff
machine.offset += machine.increment
bytesWritten++
offset++
break if machine.offset is machine.terminal
return true if offset is length
if ++machine.index is machine.definition.pattern.length
machine.definition.callback.apply null, [ engine ]
machine = null
else
machine = mechanize machine.definition, machine.index, outgoing[machine.index]
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment