Skip to content

Commit

Permalink
Reuse basic utils in Unicode addon
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Mar 30, 2016
1 parent 381fa75 commit f9521c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 50 deletions.
29 changes: 8 additions & 21 deletions src/addons/unicode-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,23 @@
module.exports = function(XRegExp) {
'use strict';

// ==--------------------------==
// Private stuff
// ==--------------------------==

// Storage for Unicode data
var unicode = {};

// ==--------------------------==
// Private functions
// ==--------------------------==
// Reuse utils
var dec = XRegExp._dec;
var hex = XRegExp._hex;
var pad4 = XRegExp._pad4;

// Generates a token lookup name: lowercase, with hyphens, spaces, and underscores removed
function normalize(name) {
return name.replace(/[- _]+/g, '').toLowerCase();
}

// Adds leading zeros if shorter than four characters
function pad4(str) {
while (str.length < 4) {
str = '0' + str;
}
return str;
}

// Converts a hexadecimal number to decimal
function dec(hex) {
return parseInt(hex, 16);
}

// Converts a decimal number to hexadecimal
function hex(dec) {
return parseInt(dec, 10).toString(16);
}

// Gets the decimal code of a literal code unit, \xHH, \uHHHH, or a backslash-escaped literal
function charCode(chr) {
var esc = /^\\[xu](.+)/.exec(chr);
Expand Down
10 changes: 6 additions & 4 deletions src/xregexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ function isQuantifierNext(pattern, pos, flags) {
}

/**
* Pads the provided string with as many leading zeros as needed to get to length 4. Used to produce
* fixed-length hexadecimal values.
* Adds leading zeros if shorter than four characters. Used for fixed-length hexadecimal values.
*
* @param {String} str
* @returns {String}
Expand Down Expand Up @@ -599,7 +598,7 @@ function XRegExp(pattern, flags) {
pattern,
flags
);
};
}

// Add `RegExp.prototype` to the prototype chain
XRegExp.prototype = new RegExp();
Expand All @@ -621,8 +620,11 @@ XRegExp.version = '3.1.1-dev';
// Public methods
// ==--------------------------==

// Intentionally undocumented; used in tests
// Intentionally undocumented; used in tests and addons
XRegExp._hasNativeFlag = hasNativeFlag;
XRegExp._dec = dec;
XRegExp._hex = hex;
XRegExp._pad4 = pad4;

/**
* Extends XRegExp syntax and allows custom flags. This is used internally and can be used to
Expand Down
39 changes: 14 additions & 25 deletions xregexp-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,36 +399,23 @@ module.exports = function(XRegExp) {
module.exports = function(XRegExp) {
'use strict';

// ==--------------------------==
// Private stuff
// ==--------------------------==

// Storage for Unicode data
var unicode = {};

// ==--------------------------==
// Private functions
// ==--------------------------==
// Reuse utils
var dec = XRegExp._dec;
var hex = XRegExp._hex;
var pad4 = XRegExp._pad4;

// Generates a token lookup name: lowercase, with hyphens, spaces, and underscores removed
function normalize(name) {
return name.replace(/[- _]+/g, '').toLowerCase();
}

// Adds leading zeros if shorter than four characters
function pad4(str) {
while (str.length < 4) {
str = '0' + str;
}
return str;
}

// Converts a hexadecimal number to decimal
function dec(hex) {
return parseInt(hex, 16);
}

// Converts a decimal number to hexadecimal
function hex(dec) {
return parseInt(dec, 10).toString(16);
}

// Gets the decimal code of a literal code unit, \xHH, \uHHHH, or a backslash-escaped literal
function charCode(chr) {
var esc = /^\\[xu](.+)/.exec(chr);
Expand Down Expand Up @@ -2918,8 +2905,7 @@ function isQuantifierNext(pattern, pos, flags) {
}

/**
* Pads the provided string with as many leading zeros as needed to get to length 4. Used to produce
* fixed-length hexadecimal values.
* Adds leading zeros if shorter than four characters. Used for fixed-length hexadecimal values.
*
* @param {String} str
* @returns {String}
Expand Down Expand Up @@ -3213,7 +3199,7 @@ function XRegExp(pattern, flags) {
pattern,
flags
);
};
}

// Add `RegExp.prototype` to the prototype chain
XRegExp.prototype = new RegExp();
Expand All @@ -3235,8 +3221,11 @@ XRegExp.version = '3.1.1-dev';
// Public methods
// ==--------------------------==

// Intentionally undocumented; used in tests
// Intentionally undocumented; used in tests and addons
XRegExp._hasNativeFlag = hasNativeFlag;
XRegExp._dec = dec;
XRegExp._hex = hex;
XRegExp._pad4 = pad4;

/**
* Extends XRegExp syntax and allows custom flags. This is used internally and can be used to
Expand Down

0 comments on commit f9521c4

Please sign in to comment.