[Pkg-javascript-commits] [node-expat] 134/371: Implement libexpat getCurrentLineNumber/getCurrentColumnNumber/getCurrentByteIndex

Jonas Smedegaard dr at jones.dk
Sun Feb 28 09:59:55 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 776779ecc6a6c4a8b37a1fd78f087174dc728e30
Author: Astro <astro at spaceboyz.net>
Date:   Wed Apr 4 00:06:45 2012 +0200

    Implement libexpat getCurrentLineNumber/getCurrentColumnNumber/getCurrentByteIndex
    
    Fixes Github issue #31
---
 lib/node-expat.js |  9 +++++++++
 node-expat.cc     | 42 ++++++++++++++++++++++++++++++++++++++++++
 test.js           | 30 ++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+)

diff --git a/lib/node-expat.js b/lib/node-expat.js
index cad557f..765515e 100644
--- a/lib/node-expat.js
+++ b/lib/node-expat.js
@@ -40,3 +40,12 @@ exports.Parser.prototype.pause = function() {
 exports.Parser.prototype.resume = function() {
     return this.parser.resume();
 };
+exports.Parser.prototype.getCurrentLineNumber = function() {
+    return this.parser.getCurrentLineNumber();
+};
+exports.Parser.prototype.getCurrentColumnNumber = function() {
+    return this.parser.getCurrentColumnNumber();
+};
+exports.Parser.prototype.getCurrentByteIndex = function() {
+    return this.parser.getCurrentByteIndex();
+};
diff --git a/node-expat.cc b/node-expat.cc
index 35b39be..c35b3b2 100644
--- a/node-expat.cc
+++ b/node-expat.cc
@@ -29,6 +29,9 @@ public:
     NODE_SET_PROTOTYPE_METHOD(t, "getError", GetError);
     NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
     NODE_SET_PROTOTYPE_METHOD(t, "resume", Resume);
+    NODE_SET_PROTOTYPE_METHOD(t, "getCurrentLineNumber", GetCurrentLineNumber);
+    NODE_SET_PROTOTYPE_METHOD(t, "getCurrentColumnNumber", GetCurrentColumnNumber);
+    NODE_SET_PROTOTYPE_METHOD(t, "getCurrentByteIndex", GetCurrentByteIndex);
 
     target->Set(String::NewSymbol("Parser"), t->GetFunction());
 
@@ -239,6 +242,45 @@ protected:
     return XML_ErrorString(code);
   }
 
+  static Handle<Value> GetCurrentLineNumber(const Arguments& args)
+  {
+    Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
+    HandleScope scope;
+
+    return scope.Close(Integer::NewFromUnsigned(parser->getCurrentLineNumber()));
+  }
+
+  uint32_t getCurrentLineNumber()
+  {
+    return XML_GetCurrentLineNumber(parser);
+  }
+
+  static Handle<Value> GetCurrentColumnNumber(const Arguments& args)
+  {
+    Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
+    HandleScope scope;
+
+    return scope.Close(Integer::NewFromUnsigned(parser->getCurrentColumnNumber()));
+  }
+
+  uint32_t getCurrentColumnNumber()
+  {
+    return XML_GetCurrentColumnNumber(parser);
+  }
+
+  static Handle<Value> GetCurrentByteIndex(const Arguments& args)
+  {
+    Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
+    HandleScope scope;
+
+    return scope.Close(Integer::New(parser->getCurrentByteIndex()));
+  }
+
+  int32_t getCurrentByteIndex()
+  {
+    return XML_GetCurrentByteIndex(parser);
+  }
+
 private:
   /* expat instance */
   XML_Parser parser;
diff --git a/test.js b/test.js
index 1508407..6b5a2dd 100644
--- a/test.js
+++ b/test.js
@@ -249,5 +249,35 @@ vows.describe('node-expat').addBatch({
            assert.ok(result2);
 
         }*/
+    },
+    'statistics': {
+	'line number': function() {
+	    var p = new expat.Parser();
+	    assert.equal(p.getCurrentLineNumber(), 1);
+	    p.parse("\n");
+	    assert.equal(p.getCurrentLineNumber(), 2);
+	    p.parse("\n");
+	    assert.equal(p.getCurrentLineNumber(), 3);
+	},
+	'column number': function() {
+	    var p = new expat.Parser();
+	    assert.equal(p.getCurrentColumnNumber(), 0);
+	    p.parse(" ");
+	    assert.equal(p.getCurrentColumnNumber(), 1);
+	    p.parse(" ");
+	    assert.equal(p.getCurrentColumnNumber(), 2);
+	    p.parse("\n");
+	    assert.equal(p.getCurrentColumnNumber(), 0);
+	},
+	'byte index': function() {
+	    var p = new expat.Parser();
+	    assert.equal(p.getCurrentByteIndex(), -1);
+	    p.parse("");
+	    assert.equal(p.getCurrentByteIndex(), -1);
+	    p.parse("\n");
+	    assert.equal(p.getCurrentByteIndex(), 1);
+	    p.parse(" ");
+	    assert.equal(p.getCurrentByteIndex(), 2);
+	},
     }
 }).export(module);

-- 
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