[Pkg-javascript-commits] [ltx] 229/469: sax_ltx: mv all parsing code inside closure
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:03:06 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository ltx.
commit 281b6f1e9bc3c1e1aa665794a7262e4bd3ee029d
Author: Astro <astro at spaceboyz.net>
Date: Tue Mar 20 04:27:47 2012 +0100
sax_ltx: mv all parsing code inside closure
speedup >2.0
---
lib/sax_ltx.js | 203 +++++++++++++++++++++++++++++----------------------------
1 file changed, 105 insertions(+), 98 deletions(-)
diff --git a/lib/sax_ltx.js b/lib/sax_ltx.js
index 1445aa7..9269be3 100644
--- a/lib/sax_ltx.js
+++ b/lib/sax_ltx.js
@@ -1,7 +1,7 @@
var util = require('util');
var events = require('events');
-var STATE_TEXT = 0,
+const STATE_TEXT = 0,
STATE_TAG_NAME = 2,
STATE_TAG = 3,
STATE_ATTR_NAME = 4,
@@ -15,31 +15,9 @@ var RE_TAG_NAME = /^[^\s\/>]+$/,
var SaxLtx = module.exports = function SaxLtx() {
events.EventEmitter.call(this);
- this.recordStart = 0;
- this.state = STATE_TEXT;
-
-
- /*var origEmit = this.emit;
- this.emit = function() {
- console.log('ltx', arguments);
- origEmit.apply(this, arguments);
- };*/
-};
-util.inherits(SaxLtx, events.EventEmitter);
-
-SaxLtx.prototype.write = function(data) {
- if (typeof data !== 'string')
- data = data.toString();
- var pos = 0;
-
- /* Anything from previous write()? */
- if (this.remainder) {
- data = this.remainder + data;
- pos += this.remainder.length;
- delete this.remainder;
- }
-
- var that = this;
+ var state = STATE_TEXT, remainder;
+ var tagName, attrs, endTag, selfClosing, attrQuote;
+ var recordStart = 0;
function endRecording() {
if (that.hasOwnProperty('recordStart')) {
var recorded = data.slice(that.recordStart, pos);
@@ -48,88 +26,117 @@ SaxLtx.prototype.write = function(data) {
}
}
- for(; pos < data.length; pos++) {
- var c = data.charCodeAt(pos);
- //console.log("state", this.state, "c", c, data[pos]);
- switch(this.state) {
- case STATE_TEXT:
- if (c === 60 /* < */) {
- var text = endRecording();
- if (text)
- this.emit('text', unescapeXml(text));
- this.state = STATE_TAG_NAME;
- this.recordStart = pos + 1;
- this.attrs = {};
- }
- break;
- case STATE_TAG_NAME:
- if (c === 47 /* / */ && this.recordStart === pos) {
- this.recordStart = pos + 1;
- this.endTag = true;
- } else if (c <= 32 || c === 47 /* / */ || c === 62 /* > */) {
- this.tagName = endRecording();
- pos--;
- this.state = STATE_TAG;
- }
- break;
- case STATE_TAG:
- if (c === 62 /* > */) {
- if (!this.endTag) {
- this.emit('startElement', this.tagName, this.attrs);
- if (this.selfClosing)
- this.emit('endElement', this.tagName);
- } else
- this.emit('endElement', this.tagName);
- delete this.tagName;
- delete this.attrs;
- delete this.endTag;
- delete this.selfClosing;
- this.state = STATE_TEXT;
- this.recordStart = pos + 1;
- } else if (c === 47 /* / */) {
- this.selfClosing = true;
- } else if (c > 32) {
- this.recordStart = pos;
- this.state = STATE_ATTR_NAME;
- }
- break;
- case STATE_ATTR_NAME:
- if (c <= 32 || c === 61 /* = */) {
- this.attrName = endRecording();
- pos--;
- this.state = STATE_ATTR_EQ;
- }
- break;
- case STATE_ATTR_EQ:
- if (c === 61 /* = */) {
- this.state = STATE_ATTR_QUOT;
- }
- break;
- case STATE_ATTR_QUOT:
- if (c === 34 /* " */ || c === 39 /* ' */) {
- this.attrQuote = c;
- this.state = STATE_ATTR_VALUE;
- this.recordStart = pos + 1;
- }
- break;
- case STATE_ATTR_VALUE:
- if (c === this.attrQuote) {
- var value = unescapeXml(endRecording());
- this.attrs[this.attrName] = value;
- delete this.attrName;
- this.state = STATE_TAG;
+ this.write = function(data) {
+ if (typeof data !== 'string')
+ data = data.toString();
+ var pos = 0;
+
+ /* Anything from previous write()? */
+ if (remainder) {
+ data = remainder + data;
+ pos += remainder.length;
+ delete remainder;
+ }
+
+ function endRecording() {
+ if (typeof recordStart === 'number') {
+ var recorded = data.slice(recordStart, pos);
+ recordStart = undefined;
+ return recorded;
}
- break;
+ }
+
+ for(; pos < data.length; pos++) {
+ var c = data.charCodeAt(pos);
+ //console.log("state", state, "c", c, data[pos]);
+ switch(state) {
+ case STATE_TEXT:
+ if (c === 60 /* < */) {
+ var text = endRecording();
+ if (text)
+ this.emit('text', unescapeXml(text));
+ state = STATE_TAG_NAME;
+ recordStart = pos + 1;
+ attrs = {};
+ }
+ break;
+ case STATE_TAG_NAME:
+ if (c === 47 /* / */ && recordStart === pos) {
+ recordStart = pos + 1;
+ endTag = true;
+ } else if (c <= 32 || c === 47 /* / */ || c === 62 /* > */) {
+ tagName = endRecording();
+ pos--;
+ state = STATE_TAG;
+ }
+ break;
+ case STATE_TAG:
+ if (c === 62 /* > */) {
+ if (!endTag) {
+ this.emit('startElement', tagName, attrs);
+ if (selfClosing)
+ this.emit('endElement', tagName);
+ } else
+ this.emit('endElement', tagName);
+ tagName = undefined;
+ attrs = undefined;
+ endTag = undefined;
+ selfClosing = undefined;
+ state = STATE_TEXT;
+ recordStart = pos + 1;
+ } else if (c === 47 /* / */) {
+ selfClosing = true;
+ } else if (c > 32) {
+ recordStart = pos;
+ state = STATE_ATTR_NAME;
+ }
+ break;
+ case STATE_ATTR_NAME:
+ if (c <= 32 || c === 61 /* = */) {
+ attrName = endRecording();
+ pos--;
+ state = STATE_ATTR_EQ;
+ }
+ break;
+ case STATE_ATTR_EQ:
+ if (c === 61 /* = */) {
+ state = STATE_ATTR_QUOT;
+ }
+ break;
+ case STATE_ATTR_QUOT:
+ if (c === 34 /* " */ || c === 39 /* ' */) {
+ attrQuote = c;
+ state = STATE_ATTR_VALUE;
+ recordStart = pos + 1;
+ }
+ break;
+ case STATE_ATTR_VALUE:
+ if (c === attrQuote) {
+ var value = unescapeXml(endRecording());
+ attrs[attrName] = value;
+ attrName = undefined;
+ state = STATE_TAG;
+ }
+ break;
}
}
if (this.hasOwnProperty('recordStart') &&
this.recordStart <= data.length) {
- this.remainder = data.slice(this.recordStart);
+ remainder = data.slice(this.recordStart);
this.recordStart = 0;
}
};
+ /*var origEmit = this.emit;
+ this.emit = function() {
+ console.log('ltx', arguments);
+ origEmit.apply(this, arguments);
+ };*/
+};
+util.inherits(SaxLtx, events.EventEmitter);
+
+
SaxLtx.prototype.end = function(data) {
if (data)
this.write(data);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git
More information about the Pkg-javascript-commits
mailing list