Skip to content

Commit

Permalink
v2.3.22
Browse files Browse the repository at this point in the history
Final tweak
  • Loading branch information
amarcruz committed Jan 24, 2016
1 parent 9eaeb0a commit 20661c0
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions lib/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ var FINDBRACES = {
*/
var DEFAULT = '{ }'

/**
* Pre-made string and regexes for the default brackets
* @type {Array}
*/
// Pre-made string and regexes for the default brackets
var _pairs = [
'{', '}',
'{', '}',
Expand All @@ -64,10 +61,7 @@ var _pairs = [
DEFAULT
]

/**
* Pre-made string and regexes for the last bracket pair
* @type {Array}
*/
// Pre-made string and regexes for the last bracket pair
var _cache = []

/*
Expand All @@ -79,12 +73,11 @@ var _cache = []
* Rewrite a regex with the default brackets replaced with the custom ones.
*
* @param {RegExp} re - RegExp with the default riot brackets
* @param {Array} bp - Array with info of the new brackets
* @returns {RegExp} The new regex with the default brackets replaced.
*/
function _rewrite (re, bp) {
function _rewrite (re) {
return RegExp(
re.source.replace(/{/g, bp[2]).replace(/}/g, bp[3]), re.global ? 'g' : ''
re.source.replace(/{/g, _cache[2]).replace(/}/g, _cache[3]), re.global ? 'g' : ''
)
}

Expand Down Expand Up @@ -221,6 +214,10 @@ module.exports.split = function split (str, _, _bp) {
}
}

var
INVALIDCH = /[\x00-\x1F<>a-zA-Z0-9'",;\\]/, // invalid characters for brackets
ESCAPEDCH = /(?=[[\]()*+?.^$|])/g // this characters must be escaped

/**
* Returns an array with information for the given brackets using a cache for the
* last custom brackets pair required by the caller.
Expand All @@ -239,14 +236,14 @@ module.exports.array = function array (pair) {
if (_cache[8] !== pair) {
_cache = pair.split(' ')

if (_cache.length !== 2 || /[\x00-\x1F<>a-zA-Z0-9'",;\\]/.test(pair)) {
if (_cache.length !== 2 || INVALIDCH.test(pair)) {
throw new Error('Unsupported brackets "' + pair + '"')
}
_cache = _cache.concat(pair.replace(/(?=[[\]()*+?.^$|])/g, '\\').split(' '))
_cache = _cache.concat(pair.replace(ESCAPEDCH, '\\').split(' '))

_cache[4] = _rewrite(_cache[1].length > 1 ? /{[\S\s]*?}/ : _pairs[4], _cache)
_cache[5] = _rewrite(/\\({|})/g, _cache)
_cache[6] = _rewrite(_pairs[6], _cache) // for _split()
_cache[4] = _rewrite(_cache[1].length > 1 ? /{[\S\s]*?}/ : _pairs[4])
_cache[5] = _rewrite(/\\({|})/g)
_cache[6] = _rewrite(_pairs[6]) // for _split()
_cache[7] = RegExp('\\\\(' + _cache[3] + ')|([[({])|(' + _cache[3] + ')|' + S_QBLOCKS, 'g')
_cache[8] = pair
}
Expand Down

0 comments on commit 20661c0

Please sign in to comment.