From 4a366d75b59eb780a79a31587f4475b01574c951 Mon Sep 17 00:00:00 2001 From: Matt Bell <mappum@gmail.com> Date: Wed, 13 Mar 2013 17:57:18 -0700 Subject: [PATCH] Added stringArray datatype --- lib/protocol.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/protocol.js b/lib/protocol.js index fad52ea..5b0a077 100644 --- a/lib/protocol.js +++ b/lib/protocol.js @@ -497,6 +497,7 @@ var types = { 'intVector': [readIntVector, IntVectorWriter], 'byteVector': [readByteVector, ByteVectorWriter], 'byteVectorArray': [readByteVectorArray, ByteVectorArrayWriter], + 'stringArray': [readStringArray, StringArrayWriter], }; function ByteArray32Writer(value) { @@ -677,6 +678,24 @@ ByteVectorArrayWriter.prototype.write = function(buffer, offset) { }); } +function StringArrayWriter(value) { + this.value = value; + this.size = 2; + for (var i = 0; i < this.value.length; ++i) { + this.value[i] = stringWriter = new StringWriter(this.value[i]); + this.size += stringWriter.size; + } +} + +StringArrayWriter.prototype.write = function(buffer, offset) { + buffer.writeInt16BE(this.value.length, offset); + offset += 2; + this.value.forEach(function(stringWriter) { + stringWriter.write(buffer, offset); + offset += stringWriter.size; + }); +} + function readIntArray8(buffer, offset) { var results = readByte(buffer, offset); if (! results) return null; @@ -954,6 +973,26 @@ function readSlotArray (buffer, offset) { }; } +function readStringArray (buffer, offset) { + var results = readShort(buffer, offset); + if (! results) return null; + var count = results.value; + var cursor = offset + results.size; + + var stringArray = []; + for (var i = 0; i < count; ++i) { + results = readString(buffer, cursor); + if (! results) return null; + stringArray.push(results.value); + cursor += results.size; + } + + return { + value: stringArray, + size: cursor - offset, + }; +} + function readShort(buffer, offset) { if (offset + 2 > buffer.length) return null; var value = buffer.readInt16BE(offset);