From bd8c14355c8495457eb213838eb0484c3c8f5a20 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Thu, 3 Oct 2013 13:40:43 -0700 Subject: [PATCH] Move regexes out into variables --- benchmark/benchmark-suite.coffee | 2 +- src/token.coffee | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/benchmark/benchmark-suite.coffee b/benchmark/benchmark-suite.coffee index e5cbeee29..2ef442241 100644 --- a/benchmark/benchmark-suite.coffee +++ b/benchmark/benchmark-suite.coffee @@ -95,7 +95,7 @@ describe "editor.", -> benchmark "resetDisplay", 50, -> editor.resetDisplay() - benchmark "htmlForScreenRows", 1000, -> + fbenchmark "htmlForScreenRows", 1000, -> lastRow = editor.getLastScreenRow() editor.htmlForScreenRows(0, lastRow) diff --git a/src/token.coffee b/src/token.coffee index ad2ece720..81b5b2556 100644 --- a/src/token.coffee +++ b/src/token.coffee @@ -5,6 +5,10 @@ WhitespaceRegexesByTabLength = {} LeadingWhitespaceRegex = /^[ ]+/ TrailingWhitespaceRegex = /[ ]+$/ EscapeRegex = /[&"'<>]/g +CharacterRegex = /./g +StartCharacterRegex = /^./ +StartDotRegex = /^\.?/ +WhitespaceRegex = /\S/ # Private: Represents a single unit of text as selected by a grammar. module.exports = @@ -115,10 +119,10 @@ class Token ) isOnlyWhitespace: -> - not /\S/.test(@value) + not WhitespaceRegex.test(@value) matchesScopeSelector: (selector) -> - targetClasses = selector.replace(/^\.?/, '').split('.') + targetClasses = selector.replace(StartDotRegex, '').split('.') _.any @scopes, (scope) -> scopeClasses = scope.split('.') _.isSubset(targetClasses, scopeClasses) @@ -131,7 +135,7 @@ class Token classes = 'hard-tab' classes += ' indent-guide' if hasIndentGuide classes += ' invisible-character' if invisibles.tab - html = html.replace /^./, (match) => + html = html.replace StartCharacterRegex, (match) => match = invisibles.tab ? match "#{@escapeString(match)}" else @@ -146,8 +150,8 @@ class Token classes += ' indent-guide' if hasIndentGuide classes += ' invisible-character' if invisibles.space - match[0] = match[0].replace(/./g, invisibles.space) if invisibles.space - leadingHtml = "#{@escapeString(match[0])}" + match[0] = match[0].replace(CharacterRegex, invisibles.space) if invisibles.space + leadingHtml = "#{match[0]}" startIndex = match[0].length @@ -156,8 +160,8 @@ class Token classes += ' indent-guide' if hasIndentGuide and not hasLeadingWhitespace classes += ' invisible-character' if invisibles.space - match[0] = match[0].replace(/./g, invisibles.space) if invisibles.space - trailingHtml = "#{@escapeString(match[0])}" + match[0] = match[0].replace(CharacterRegex, invisibles.space) if invisibles.space + trailingHtml = "#{match[0]}" endIndex = match.index