From 0f32ac4e2f44720315dd21fe686818f87000d96a Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 24 Oct 2014 10:22:30 -0700 Subject: js: Use linked list instead of array for emphasis_openers stack. --- js/lib/inlines.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'js/lib') diff --git a/js/lib/inlines.js b/js/lib/inlines.js index eede313..b7f4d1d 100644 --- a/js/lib/inlines.js +++ b/js/lib/inlines.js @@ -274,10 +274,9 @@ var parseEmphasis = function(cc,inlines) { if (res.can_close) { // Walk the stack and find a matching opener, if possible - var i = this.emphasis_openers.length - 1; - while (i >= 0) { + var opener = this.emphasis_openers; + while (opener) { - var opener = this.emphasis_openers[i]; if (opener.cc === cc) { // we have a match! if (opener.numdelims <= numdelims) { // all openers used @@ -299,7 +298,7 @@ var parseEmphasis = function(cc,inlines) { inlines[opener.pos] = X(inlines.slice(opener.pos + 1)); inlines.splice(opener.pos + 1, inlines.length - (opener.pos + 1)); // Remove entries after this, to prevent overlapping nesting: - this.emphasis_openers.splice(i, this.emphasis_openers.length - i); + this.emphasis_openers = opener.previous; return true; } else if (opener.numdelims > numdelims) { // only some openers used @@ -312,13 +311,13 @@ var parseEmphasis = function(cc,inlines) { inlines[opener.pos + 1] = X(inlines.slice(opener.pos + 1)); inlines.splice(opener.pos + 2, inlines.length - (opener.pos + 2)); // Remove entries after this, to prevent overlapping nesting: - this.emphasis_openers.splice(i + 1, this.emphasis_openers.length - (i + 1)); + this.emphasis_openers = opener; return true; } } - i--; + opener = opener.previous; } } @@ -330,10 +329,10 @@ var parseEmphasis = function(cc,inlines) { if (res.can_open) { // Add entry to stack for this opener - this.emphasis_openers.push({ cc: cc, - numdelims: numdelims, - pos: inlines.length - 1 }); - + this.emphasis_openers = { cc: cc, + numdelims: numdelims, + pos: inlines.length - 1, + previous: this.emphasis_openers }; } return true; @@ -685,7 +684,7 @@ var parseInlines = function(s, refmap) { this.pos = 0; this.refmap = refmap || {}; this.memo = {}; - this.emphasis_openers = []; + this.emphasis_openers = null; var inlines = []; while (this.parseInline(inlines, false)) { } @@ -697,7 +696,7 @@ function InlineParser(){ return { subject: '', label_nest_level: 0, // used by parseLinkLabel method - emphasis_openers: [], // used by parseEmphasis method + emphasis_openers: null, // used by parseEmphasis method pos: 0, refmap: {}, memo: {}, -- cgit v1.2.3