Skip to content

Commit

Permalink
Add BB2 string encoding (hex, update base64)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Nov 18, 2013
1 parent 441a312 commit befe8a2
Show file tree
Hide file tree
Showing 32 changed files with 563 additions and 138 deletions.
38 changes: 32 additions & 6 deletions ProtoBuf.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @const
* @expose
*/
ProtoBuf.VERSION = "1.5.1";
ProtoBuf.VERSION = "1.5.2";

/**
* Wire types.
Expand Down Expand Up @@ -326,7 +326,6 @@
* @exports ProtoBuf.Lang
* @type {Object.<string,string|RegExp>}
* @namespace
* @private
* @expose
*/
var Lang = { // Look, so cute!
Expand Down Expand Up @@ -1819,9 +1818,9 @@
* Encodes the message.
* @name ProtoBuf.Builder.Message#encode
* @function
* @param {ByteBuffer=} buffer ByteBuffer to encode to. Will create a new one if omitted.
* @param {!ByteBuffer=} buffer ByteBuffer to encode to. Will create a new one if omitted.
* @param {boolean=} doNotThrow Forces encoding even if required fields are missing, defaults to false
* @return {ByteBuffer} Encoded message
* @return {!ByteBuffer} Encoded message as a ByteBuffer (bb#toArrayBuffer, bb#toBuffer, bb#toHex, bb#toBase64, ...)
* @throws {Error} If required fields are missing or the message cannot be encoded for another reason
* @expose
*/
Expand Down Expand Up @@ -1874,17 +1873,32 @@
return this.encode().toBase64();
};

/**
* Directly encodes the message to a hex encoded string.
* @name ProtoBuf.Builder.Message#toHex
* @function
* @return {string} Hex encoded string
* @throws {Error} If the underlying buffer cannot be encoded
* @expose
*/
Message.prototype.toHex = function() {
return this.encode().toHex();
};

/**
* Decodes the message from the specified ByteBuffer.
* @name ProtoBuf.Builder.Message.decode
* @function
* @param {!ByteBuffer|!ArrayBuffer|!Buffer} buffer ByteBuffer to decode from
* @param {string=} enc Encoding if buffer is a string: hex, base64, defaults to utf8 which you shouldn't use
* @return {!ProtoBuf.Builder.Message} Decoded message
* @throws {Error} If the message cannot be decoded
* @expose
* @see ProtoBuf.Builder.Message.decode64
* @see ProtoBuf.Builder.Message.decodeHex
*/
Message.decode = function(buffer) {
buffer = buffer ? (buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer)) : new ByteBuffer();
Message.decode = function(buffer, enc) {
buffer = buffer ? (buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer, enc)) : new ByteBuffer();
var le = buffer.littleEndian;
try {
var msg = T.decode(buffer.LE());
Expand All @@ -1909,6 +1923,18 @@
return Message.decode(ByteBuffer.decode64(str));
};

/**
* Decodes the message from the specified hex encoded string.
* @name ProtoBuf.Builder.Message.decodeHex
* @param {string} str String to decode from
* @return {!ProtoBuf.Builder.Message} Decoded message
* @throws {Error} If the message cannot be decoded
* @expose
*/
Message.decodeHex = function(str) {
return Message.decode(ByteBuffer.decodeHex(str));
};

// Utility

/**
Expand Down
83 changes: 42 additions & 41 deletions ProtoBuf.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions ProtoBuf.min.map

Large diffs are not rendered by default.

38 changes: 32 additions & 6 deletions ProtoBuf.noparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @const
* @expose
*/
ProtoBuf.VERSION = "1.5.1";
ProtoBuf.VERSION = "1.5.2";

/**
* Wire types.
Expand Down Expand Up @@ -326,7 +326,6 @@
* @exports ProtoBuf.Lang
* @type {Object.<string,string|RegExp>}
* @namespace
* @private
* @expose
*/
var Lang = { // Look, so cute!
Expand Down Expand Up @@ -924,9 +923,9 @@
* Encodes the message.
* @name ProtoBuf.Builder.Message#encode
* @function
* @param {ByteBuffer=} buffer ByteBuffer to encode to. Will create a new one if omitted.
* @param {!ByteBuffer=} buffer ByteBuffer to encode to. Will create a new one if omitted.
* @param {boolean=} doNotThrow Forces encoding even if required fields are missing, defaults to false
* @return {ByteBuffer} Encoded message
* @return {!ByteBuffer} Encoded message as a ByteBuffer (bb#toArrayBuffer, bb#toBuffer, bb#toHex, bb#toBase64, ...)
* @throws {Error} If required fields are missing or the message cannot be encoded for another reason
* @expose
*/
Expand Down Expand Up @@ -979,17 +978,32 @@
return this.encode().toBase64();
};

/**
* Directly encodes the message to a hex encoded string.
* @name ProtoBuf.Builder.Message#toHex
* @function
* @return {string} Hex encoded string
* @throws {Error} If the underlying buffer cannot be encoded
* @expose
*/
Message.prototype.toHex = function() {
return this.encode().toHex();
};

/**
* Decodes the message from the specified ByteBuffer.
* @name ProtoBuf.Builder.Message.decode
* @function
* @param {!ByteBuffer|!ArrayBuffer|!Buffer} buffer ByteBuffer to decode from
* @param {string=} enc Encoding if buffer is a string: hex, base64, defaults to utf8 which you shouldn't use
* @return {!ProtoBuf.Builder.Message} Decoded message
* @throws {Error} If the message cannot be decoded
* @expose
* @see ProtoBuf.Builder.Message.decode64
* @see ProtoBuf.Builder.Message.decodeHex
*/
Message.decode = function(buffer) {
buffer = buffer ? (buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer)) : new ByteBuffer();
Message.decode = function(buffer, enc) {
buffer = buffer ? (buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer, enc)) : new ByteBuffer();
var le = buffer.littleEndian;
try {
var msg = T.decode(buffer.LE());
Expand All @@ -1014,6 +1028,18 @@
return Message.decode(ByteBuffer.decode64(str));
};

/**
* Decodes the message from the specified hex encoded string.
* @name ProtoBuf.Builder.Message.decodeHex
* @param {string} str String to decode from
* @return {!ProtoBuf.Builder.Message} Decoded message
* @throws {Error} If the message cannot be decoded
* @expose
*/
Message.decodeHex = function(str) {
return Message.decode(ByteBuffer.decodeHex(str));
};

// Utility

/**
Expand Down
Loading

0 comments on commit befe8a2

Please sign in to comment.