[Pkg-javascript-commits] [node-expat] 23/371: node.js Buffer support (speed++)
Jonas Smedegaard
dr at jones.dk
Sun Feb 28 09:59:41 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository node-expat.
commit f301bdee6b4da1da60407999eafba696815e765d
Author: Astro <astro at spaceboyz.net>
Date: Sun May 30 14:56:29 2010 +0200
node.js Buffer support (speed++)
---
expat.cc | 42 ++++++++++++++++++++++++++++++------------
test.js | 11 ++++++++++-
2 files changed, 40 insertions(+), 13 deletions(-)
diff --git a/expat.cc b/expat.cc
index 3aa6011..d22560c 100644
--- a/expat.cc
+++ b/expat.cc
@@ -1,5 +1,6 @@
#include <node.h>
#include <node_events.h>
+#include <node_buffer.h>
extern "C" {
#include <expat.h>
}
@@ -83,31 +84,44 @@ protected:
Local<String> str;
int isFinal = 0;
- /* Argument 1: buf :: String */
- if (args.Length() >= 1 && args[0]->IsString())
- {
- str = args[0]->ToString();
- }
- else
- return scope.Close(False());
-
/* Argument 2: isFinal :: Bool */
if (args.Length() >= 2)
{
isFinal = args[1]->IsTrue();
}
- return scope.Close(parser->parse(**str, isFinal) ? True() : False());
+ /* Argument 1: buf :: String or Buffer */
+ if (args.Length() >= 1 && args[0]->IsString())
+ {
+ str = args[0]->ToString();
+ return scope.Close(parser->parseString(**str, isFinal) ? True() : False());
+ }
+ else if (args.Length() >= 1 && args[0]->IsObject())
+ {
+ /* TODO: is it really a buffer? */
+ Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
+ return scope.Close(parser->parseBuffer(*buffer, isFinal) ? True() : False());
+ }
+ else
+ return scope.Close(False());
}
- bool parse(String &str, int isFinal)
+ /** Parse a v8 String by first writing it to the expat parser's
+ buffer */
+ bool parseString(String &str, int isFinal)
{
int len = str.Utf8Length();
void *buf = XML_GetBuffer(parser, len);
assert(buf != NULL);
assert(str.WriteUtf8(static_cast<char *>(buf), len) == len);
- return XML_ParseBuffer(parser, len, isFinal) != 0;
+ return XML_ParseBuffer(parser, len, isFinal) != XML_STATUS_ERROR;
+ }
+
+ /** Parse a node.js Buffer directly */
+ bool parseBuffer(Buffer &buffer, int isFinal)
+ {
+ return XML_Parse(parser, buffer.data(), buffer.length(), isFinal) != XML_STATUS_ERROR;
}
/*** setEncoding() ***/
@@ -144,7 +158,11 @@ protected:
HandleScope scope;
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
- return scope.Close(String::New(parser->getError()));
+ const XML_LChar *error = parser->getError();
+ if (error)
+ return scope.Close(String::New(error));
+ else
+ return scope.Close(Null());
}
const XML_LChar *getError()
diff --git a/test.js b/test.js
index cc2cbd3..d45c6e9 100644
--- a/test.js
+++ b/test.js
@@ -1,5 +1,6 @@
var sys = require('sys');
var expat = require('./build/default/expat');
+var Buffer = require('buffer').Buffer;
function collapseTexts(evs) {
var r = [];
@@ -47,7 +48,11 @@ function expect(s, evs_expected) {
});
for(var l = 0; l < s.length; l += step)
{
- if (!p.parse(s.substr(l, step), false))
+ var end = l + step;
+ if (end > s.length)
+ end = s.length;
+
+ if (!p.parse(s.slice(l, end), false))
evs_received.push(['error']);
}
@@ -102,5 +107,9 @@ expect("<!-- no comment -->",
expect("<&", [['error']]);
expect("<?xml version='1.0' encoding='UTF-8'?>",
[['xmlDecl', '1.0', 'UTF-8', true]]);
+expect(new Buffer('<foo>bar</foo>'),
+ [['startElement', 'foo', {}],
+ ['text', 'bar'],
+ ['endElement', 'foo']]);
sys.puts("Ran "+tests+" tests with "+iterations+" iterations: "+fails+" failures.");
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-expat.git
More information about the Pkg-javascript-commits
mailing list