Skip to content

Commit

Permalink
Clean up some of the vars and loops.
Browse files Browse the repository at this point in the history
There's zero reason in 2015 to use a loop, or "var" - this cleans some of them up.

There's still a bunch left, but I figured I'd tackle just a little bit at a time.
  • Loading branch information
ljharb committed Oct 25, 2015
1 parent a4d5e4e commit e370f0b
Showing 1 changed file with 21 additions and 46 deletions.
67 changes: 21 additions & 46 deletions RegExp.make.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ RegExp.make = (function () {
if (0x20 <= n && n <= 0x7e) {
return String.fromCharCode(n).replace(UNSAFE_CHARS_CHARSET, '\\$&');
}
var hex = n.toString(16);
/** @type {string} */
const hex = n.toString(16);
return '\\u0000'.substring(0, 6 - hex.length) + hex;
}

Expand Down Expand Up @@ -87,23 +88,16 @@ RegExp.make = (function () {
* @this {!CharRanges}
*/
CharRanges.prototype.toString = function () {
var s = '';
/** @type {!Array.<number>}. */
const ranges = this.ranges;
/** @type {number} */
const n = ranges.length;
for (var i = 0; i < n; ++i) {
/** @type {number} */
const leftAndSpan = ranges[i];
return this.ranges.reduce(function (s, leftAndSpan) {
const left = leftAndSpan >> 16;
const span = leftAndSpan & 0xffff;
s += encodeRangeEndPoint(left);
if (span) {
if (span !== 1) { s += '-'; }
s += encodeRangeEndPoint(left + span);
}
}
return s;
return s;
}, '');
};
/**
* The minimum code-point matched or NaN.
Expand All @@ -125,9 +119,8 @@ RegExp.make = (function () {
* @return {!CharRanges} this to allow chaining.
*/
CharRanges.prototype.addRange = function (left, opt_right) {
var right = opt_right || left;
const right = +(opt_right || left);
left = +left;
right = +right;
if ('number' !== typeof left
|| left < 0 || right > MAX_CHAR_IN_RANGE || left > right
|| left % 1 || right % 1) {
Expand Down Expand Up @@ -160,12 +153,9 @@ RegExp.make = (function () {
/** @type {!Array.<number>} */
const ranges = this.ranges;
/** @type {number} */
const n = ranges.length;
var pastLastRight = 0;
let pastLastRight = 0;
const invertedRanges = [];
for (var i = 0; i < n; ++i) {
/** @type {number} */
const leftAndSpan = ranges[i];
ranges.forEach(function (leftAndSpan) {
const left = leftAndSpan >> 16;
const span = leftAndSpan & 0xFFFF;
if (pastLastRight < left) {
Expand All @@ -175,7 +165,7 @@ RegExp.make = (function () {
);
}
pastLastRight = left + span + 1;
}
});
if (pastLastRight <= MAX_CHAR_IN_RANGE) {
invertedRanges.push(
(pastLastRight << 16)
Expand Down Expand Up @@ -229,14 +219,8 @@ RegExp.make = (function () {
* @return {!CharRanges} a newly allocated output. Not modified in place.
*/
CharRanges.prototype.intersectionWithRange = function (min, max) {
/** @type {!Array.<number>} */
const ranges = this.ranges;
const intersection = new CharRanges();
/** @type {number} */
const n = ranges.length;
for (var i = 0; i < n; ++i) {
/** @type {number} */
const leftAndSpan = ranges[i];
this.ranges.forEach(function (leftAndSpan) {
const left = leftAndSpan >> 16;
const span = leftAndSpan & 0xFFFF;
/** @type {number} */
Expand All @@ -245,7 +229,7 @@ RegExp.make = (function () {
if (!(left > max || right < min)) {
intersection.addRange(Math.max(min, left), Math.min(max, right));
}
}
});
return intersection;
};
/**
Expand All @@ -265,19 +249,13 @@ RegExp.make = (function () {
* @this {!CharRanges}
*/
CharRanges.prototype.forEachRange = function (callback) {
/** @type {!Array.<number>} */
const ranges = this.ranges;
/** @type {number} */
const n = ranges.length;
for (var i = 0; i < n; ++i) {
/** @type {number} */
const leftAndSpan = ranges[i];
this.ranges.forEach(function (leftAndSpan) {
const left = leftAndSpan >> 16;
const span = leftAndSpan & 0xFFFF;
/** @type {number} */
const right = left + span;
callback(left, right);
}
});
};
CharRanges.prototype.clear = function () {
this.ranges.length = 0;
Expand Down Expand Up @@ -380,9 +358,9 @@ RegExp.make = (function () {
if (operators) {
tokens.push(
'[' + operatorChars.replace(UNSAFE_CHARS_CHARSET, '\\$&') + ']');
for (var i = 0, nOpChars = operatorChars.length; i < nOpChars; ++i) {
careChars.addRange(operatorChars.charCodeAt(i));
}
operatorChars.split('').forEach(function (c) {
careChars.addRange(c);
});
}

// I really wish we had a nice way of composing regular expressions.
Expand Down Expand Up @@ -441,20 +419,17 @@ RegExp.make = (function () {
const nSourceTokens = sourceTokens ? sourceTokens.length : 0;

// Assert that our tokenizer matched the whole input.
var totalSourceTokenLength = 0;
for (var i = 0; i < nSourceTokens; ++i) {
totalSourceTokenLength += sourceTokens[i].length;
}
var totalSourceTokenLength = sourceTokens.reduce(function (length, token) {
return length + token.length;
}, 0);
if (blockSource.length !== totalSourceTokenLength) {
throw new Error(
'Failed to tokenize ' + blockSource + ' with ' + tokenizer + '. Got '
+ JSON.stringify(sourceTokens) + ' which have a length delta of '
+ (blockSource.length - totalSourceTokenLength));
}

for (var i = 0; i < nSourceTokens; ++i) {
/** @type {string} */
const sourceToken = sourceTokens[i];
sourceTokens.forEach(function (sourceToken) {
switch (sourceToken[0]) {
case '[':
/** @type {boolean} */
Expand Down Expand Up @@ -511,7 +486,7 @@ RegExp.make = (function () {
default:
other(sourceToken);
}
}
});

return outputContext;
};
Expand Down

0 comments on commit e370f0b

Please sign in to comment.