Skip to content

Commit

Permalink
Minimized base64 tables
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 11, 2016
1 parent e789367 commit ecbb4a5
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 46 deletions.
42 changes: 22 additions & 20 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js"
},
"dependencies": {
"@protobufjs/base64": "1.0.1",
"@protobufjs/base64": "1.0.2",
"@protobufjs/codegen": "1.0.1",
"@protobufjs/eventemitter": "1.0.1",
"@protobufjs/pool": "1.0.1",
Expand Down
22 changes: 12 additions & 10 deletions src/encode.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use strict";
module.exports = encode;

var Enum = require("./enum"),
Writer = require("./writer"),
types = require("./types"),
util = require("./util");
var Enum = require("./enum"),
Writer = require("./writer"),
types = require("./types"),
util = require("./util");
var safeProp = util.safeProp;

/**
* General purpose message encoder.
Expand Down Expand Up @@ -99,17 +100,18 @@ function encode(message, writer) {
*/
/**/
encode.generate = function generate(mtype) {
/* eslint-disable no-unexpected-multiline */
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
var fields = mtype.getFieldsArray();
var oneofs = mtype.getOneofsArray();
var gen = util.codegen("m", "w")
("w||(w=Writer.create())");

var i;
for (var i = 0; i < fields.length; ++i) {
var field = fields[i].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
wireType = types.basic[type],
prop = util.safeProp(field.name);
prop = safeProp(field.name);

// Map fields
if (field.map) {
Expand Down Expand Up @@ -185,17 +187,17 @@ encode.generate = function generate(mtype) {

}
}
for (var i = 0; i < oneofs.length; ++i) { gen
for (var i = 0; i < oneofs.length; ++i) {
var oneof = oneofs[i],
prop = util.safeProp(oneof.name);
prop = safeProp(oneof.name);
gen
("switch(m%s){", prop);
var oneofFields = oneof.getFieldsArray();
for (var j = 0; j < oneofFields.length; ++j) {
var field = oneofFields[j],
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
wireType = types.basic[type],
prop = util.safeProp(field.name);
prop = safeProp(field.name);
gen
("case%j:", field.name);

Expand All @@ -219,5 +221,5 @@ encode.generate = function generate(mtype) {

return gen
("return w");
/* eslint-enable no-unexpected-multiline */
/* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
};
18 changes: 9 additions & 9 deletions src/util/base64/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ base64.length = function length(string) {
};

// Base64 encoding table
var b64 = [
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102,
103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47
];
var b64 = [];

// Base64 decoding table
var s64 = [];

// 65..90, 97..122, 48..57, 43, 47
for (var i = 0; i < 64;)
s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;

/**
* Encodes a buffer to a base64 encoded string.
Expand All @@ -38,7 +40,7 @@ var b64 = [
* @returns {string} Base64 encoded string
*/
base64.encode = function encode(buffer, start, end) {
var string = new Array(Math.ceil((end - start) / 3) * 4);
var string = []; // alt: new Array(Math.ceil((end - start) / 3) * 4);
var i = 0, // output index
j = 0, // goto index
t; // temporary
Expand Down Expand Up @@ -71,8 +73,6 @@ base64.encode = function encode(buffer, start, end) {
return String.fromCharCode.apply(String, string);
};

// Base64 decoding table
var s64 = []; for (var i = 0; i < b64.length; ++i) s64[b64[i]] = i;
var invalidEncoding = "invalid encoding";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/util/base64/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@protobufjs/base64",
"description": "A minimal base64 implementation for number arrays.",
"version": "1.0.1",
"version": "1.0.2",
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
"repository": {
"type": "git",
Expand Down

0 comments on commit ecbb4a5

Please sign in to comment.