From 6db9360c5f0bfca3235da1ccb078e284e4d31a27 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Mon, 9 Apr 2012 12:03:55 -0700 Subject: [PATCH] Make scanInRange iterative instead of recursive --- src/app/buffer.coffee | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index ff25816e3..8669bfb88 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -167,7 +167,12 @@ class Buffer global = regex.global regex = new RegExp(regex.source, 'gm') - traverseRecursively = (text, startIndex, endIndex, lengthDelta) => + text = @getText() + startIndex = @characterIndexForPosition(range.start) + endIndex = @characterIndexForPosition(range.end) + lengthDelta = 0 + + while true regex.lastIndex = startIndex return unless match = regex.exec(text) @@ -186,9 +191,9 @@ class Buffer startPosition = @positionForCharacterIndex(matchStartIndex + lengthDelta) endPosition = @positionForCharacterIndex(matchEndIndex + lengthDelta) range = new Range(startPosition, endPosition) - recurse = true + keepLooping = true replacementText = null - stop = -> recurse = false + stop = -> keepLooping = false replace = (text) -> replacementText = text iterator(match, range, { stop, replace }) @@ -200,12 +205,8 @@ class Buffer matchStartIndex++ matchEndIndex++ - if global and recurse - traverseRecursively(text, matchEndIndex, endIndex, lengthDelta) - - startIndex = @characterIndexForPosition(range.start) - endIndex = @characterIndexForPosition(range.end) - traverseRecursively(@getText(), startIndex, endIndex, 0) + break unless global and keepLooping + startIndex = matchEndIndex backwardsScanInRange: (regex, range, iterator) -> global = regex.global @@ -217,8 +218,8 @@ class Buffer matches.reverse() - recurse = true - stop = -> recurse = false + keepLooping = true + stop = -> keepLooping = false replacementText = null replace = (text) -> replacementText = text @@ -226,7 +227,7 @@ class Buffer replacementText = null iterator(match, matchRange, { stop, replace }) @change(matchRange, replacementText) if replacementText - return unless global and recurse + return unless global and keepLooping _.extend(Buffer.prototype, EventEmitter)