Permalink
Browse files

Writing zero terminated arrays.

  • Loading branch information...
1 parent 535ee39 commit 57dad1a6619ea2a688489f98c30515b721b4813d @bigeasy bigeasy committed Sep 21, 2010
Showing with 27 additions and 0 deletions.
  1. +11 −0 src/packet.coffee
  2. +16 −0 vows/packet-test.js
View
@@ -283,6 +283,7 @@ module.exports.Serializer = class Serializer extends Packet
@callback = callback
@patternIndex = 0
@repeat = pattern[@patternIndex].repeat
+ @terminated = not pattern[@patternIndex].terminator
@element = 0
@index = 0
@outgoing = shiftable
@@ -322,6 +323,16 @@ module.exports.Serializer = class Serializer extends Packet
break if @offset is @terminal
return if offset is length
+ if not @terminated
+ if @pattern[@patternIndex].terminator.charCodeAt(0) == @value
+ @terminated = true
+ if @repeat == Number.MAX_VALUE
+ @repeat = @index + 1
+ else
+ @skipping = (@repeat - (++@index)) * @pattern[@patternIndex].bytes
+ @repeat = @index + 1
+ continue
+
# If we are reading an arrayed pattern and we have not read all of the
# array elements, we repeat the current field type.
if ++@index < @repeat
View
@@ -246,6 +246,22 @@ vows.describe('Packet').addBatch({
assert.isTrue(invoked);
assert.deepEqual(buffer, bytes);
+ },
+ 'an zero terminated array of bytes': function (topic) {
+ var buffer = [];
+
+ var invoked = false;
+ var bytes = [ 0x01, 0x02, 0x03, 0x00 ]
+ topic.reset();
+ topic.serialize("n8z", bytes, function (engine) {
+ assert.equal(engine.getBytesWritten(), 4);
+ invoked = true;
+ });
+
+ topic.write(buffer, 0, 10);
+
+ assert.isTrue(invoked);
+ assert.deepEqual(buffer, bytes);
}
}
}).export(module);

0 comments on commit 57dad1a

Please sign in to comment.