Browse Source

perf: optimized text scans

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
460ba580ee
  1. 5
      lib/parser_inline.js
  2. 47
      lib/rules_inline/text.js

5
lib/parser_inline.js

@ -47,11 +47,6 @@ function validateLink(url) {
// Inline Parser class // Inline Parser class
// //
function ParserInline() { function ParserInline() {
// Rule to skip pure text
// - '{}$%@+=:' reserved for extentions
this.textMatch = /[\n\\`*_^\[\]!&<{}$%@~+=:]/;
// By default CommonMark allows too much in links // By default CommonMark allows too much in links
// If you need to restrict it - override this with your validator. // If you need to restrict it - override this with your validator.
this.validateLink = validateLink; this.validateLink = validateLink;

47
lib/rules_inline/text.js

@ -3,16 +3,51 @@
'use strict'; 'use strict';
// Rule to skip pure text
// '{}$%@~+=:' reserved for extentions
function isTerminatorChar(ch) {
switch (ch) {
case 0x0A/* \n */:
case 0x5C/* \ */:
case 0x60/* ` */:
case 0x2A/* * */:
case 0x5F/* _ */:
case 0x5E/* ^ */:
case 0x5B/* [ */:
case 0x5D/* ] */:
case 0x21/* ! */:
case 0x26/* & */:
case 0x3C/* < */:
case 0x3E/* > */:
case 0x7B/* { */:
case 0x7D/* } */:
case 0x24/* $ */:
case 0x25/* % */:
case 0x40/* @ */:
case 0x7E/* ~ */:
case 0x2B/* + */:
case 0x3D/* = */:
case 0x3A/* : */:
return true;
default:
return false;
}
}
module.exports = function text(state, silent) { module.exports = function text(state, silent) {
var str = state.src.slice(state.pos), var pos = state.pos;
next = str.search(state.parser.textMatch);
while (pos < state.posMax && !isTerminatorChar(state.src.charCodeAt(pos))) {
pos++;
}
if (next === 0) { return false; } if (pos === state.pos) { return false; }
if (next < 0) { next = str.length; } if (!silent) { state.pending += state.src.slice(state.pos, pos); }
if (!silent) { state.pending += str.slice(0, next); } state.pos = pos;
state.pos += next;
return true; return true;
}; };

Loading…
Cancel
Save