Skip to content

Commit 126a266

Browse files
authored
[various modes] Add missing ^ anchors to StringStream.match() as optimization
The method fails anyway (i.e. it acts as if there was no match) when the match is not at the start of the string. Adding the anchor does not change any behavior, but increases performance.
1 parent 281f132 commit 126a266

File tree

7 files changed

+16
-16
lines changed

7 files changed

+16
-16
lines changed

keymap/vim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3483,7 +3483,7 @@
34833483
},
34843484
isComplete: function(state) {
34853485
if (state.nextCh === '#') {
3486-
var token = state.lineText.match(/#(\w+)/)[1];
3486+
var token = state.lineText.match(/^#(\w+)/)[1];
34873487
if (token === 'endif') {
34883488
if (state.forward && state.depth === 0) {
34893489
return true;

mode/clike/clike.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
350350
function cpp11StringHook(stream, state) {
351351
stream.backUp(1);
352352
// Raw strings.
353-
if (stream.match(/(R|u8R|uR|UR|LR)/)) {
354-
var match = stream.match(/"([^\s\\()]{0,16})\(/);
353+
if (stream.match(/^(?:R|u8R|uR|UR|LR)/)) {
354+
var match = stream.match(/^"([^\s\\()]{0,16})\(/);
355355
if (!match) {
356356
return false;
357357
}
@@ -360,8 +360,8 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
360360
return tokenRawString(stream, state);
361361
}
362362
// Unicode strings/chars.
363-
if (stream.match(/(u8|u|U|L)/)) {
364-
if (stream.match(/["']/, /* eat */ false)) {
363+
if (stream.match(/^(?:u8|u|U|L)/)) {
364+
if (stream.match(/^["']/, /* eat */ false)) {
365365
return "string";
366366
}
367367
return false;

mode/css/css.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ CodeMirror.defineMode("css", function(config, parserConfig) {
7878
return ret("qualifier", "qualifier");
7979
} else if (/[:;{}\[\]\(\)]/.test(ch)) {
8080
return ret(null, ch);
81-
} else if (stream.match(/[\w-.]+(?=\()/)) {
82-
if (/^(url(-prefix)?|domain|regexp)$/.test(stream.current().toLowerCase())) {
81+
} else if (stream.match(/^[\w-.]+(?=\()/)) {
82+
if (/^(url(-prefix)?|domain|regexp)$/i.test(stream.current())) {
8383
state.tokenize = tokenParenthesized;
8484
}
8585
return ret("variable callee", "variable");
@@ -108,7 +108,7 @@ CodeMirror.defineMode("css", function(config, parserConfig) {
108108

109109
function tokenParenthesized(stream, state) {
110110
stream.next(); // Must be '('
111-
if (!stream.match(/\s*[\"\')]/, false))
111+
if (!stream.match(/^\s*[\"\')]/, false))
112112
state.tokenize = tokenString(")");
113113
else
114114
state.tokenize = null;
@@ -781,7 +781,7 @@ CodeMirror.defineMode("css", function(config, parserConfig) {
781781
}
782782
},
783783
":": function(stream) {
784-
if (stream.match(/\s*\{/, false))
784+
if (stream.match(/^\s*\{/, false))
785785
return [null, null]
786786
return false;
787787
},

mode/cypher/cypher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
var tokenBase = function(stream/*, state*/) {
2222
var ch = stream.next();
2323
if (ch ==='"') {
24-
stream.match(/.*?"/);
24+
stream.match(/^[^"]*"/);
2525
return "string";
2626
}
2727
if (ch === "'") {
28-
stream.match(/.*?'/);
28+
stream.match(/^[^']*'/);
2929
return "string";
3030
}
3131
if (/[{}\(\),\.;\[\]]/.test(ch)) {

mode/mscgen/mscgen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@
7272
CodeMirror.defineMIME("text/x-msgenny", {name: "mscgen", language: "msgenny"});
7373

7474
function wordRegexpBoundary(pWords) {
75-
return new RegExp("\\b(" + pWords.join("|") + ")\\b", "i");
75+
return new RegExp("^\\b(?:" + pWords.join("|") + ")\\b", "i");
7676
}
7777

7878
function wordRegexp(pWords) {
79-
return new RegExp("(" + pWords.join("|") + ")", "i");
79+
return new RegExp("^(?:" + pWords.join("|") + ")", "i");
8080
}
8181

8282
function startStateFn() {

mode/php/php.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
[["]", null]]
5454
], closing, escapes);
5555
}
56-
if (stream.match(/\-\>\w/, false)) {
56+
if (stream.match(/^->\w/, false)) {
5757
// Match object operator
5858
state.tokenize = matchSequence([
5959
[["->", null]],
@@ -106,7 +106,7 @@
106106
},
107107
"<": function(stream, state) {
108108
var before;
109-
if (before = stream.match(/<<\s*/)) {
109+
if (before = stream.match(/^<<\s*/)) {
110110
var quoted = stream.eat(/['"]/);
111111
stream.eatWhile(/[\w\.]/);
112112
var delim = stream.current().slice(before[0].length + (quoted ? 2 : 1));

mode/puppet/puppet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ CodeMirror.defineMode("puppet", function () {
176176
// Match characters that we are going to assume
177177
// are trying to be regex
178178
if (ch == '/') {
179-
stream.match(/.*?\//);
179+
stream.match(/^[^\/]*\//);
180180
return 'variable-3';
181181
}
182182
// Match all the numbers

0 commit comments

Comments
 (0)