Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Trying to implement byteOffset for read()

  • Loading branch information...
commit 4963abce34de1702439d17c2306a996092e42b50 1 parent 373cbaf
@gagle authored
Showing with 47 additions and 15 deletions.
  1. +24 −14 src/buffered-reader.js
  2. +2 −0  test/a
  3. +21 −1 test/playground.js
View
38 src/buffered-reader.js
@@ -13,6 +13,7 @@ var EVENTS = require ("events");
var FS = require ("fs");
var BUFFER_SIZE = 16384;
+var WIN = process.platform.indexOf ("win") !== -1;
var INVALID_BUFFER_SIZE = "The buffer size must be greater than 0.";
var INVALID_START_OFFSET = "The start offset must be greater than or equals to 0.";
@@ -64,10 +65,15 @@ BufferedReader.prototype.interrupt = function (){
this._interrupted = true;
};
+var realByteLength = function (character, encoding){
+ return Buffer.byteLength (character, encoding);
+};
+
BufferedReader.prototype.read = function (){
var stream = FS.createReadStream (this._fileName, this._settings);
var lastChunk;
+ var byteOffset = 0;
var me = this;
var onChar = this.listeners ("character").length !== 0;
@@ -80,22 +86,28 @@ BufferedReader.prototype.read = function (){
var chunk;
var character;
var len = data.length;
+ var isCR = false;
if (loop){
for (var i=0; i<len; i++){
if (me._interrupted) break;
character = data[i];
- if (stream.encoding){
- if (onChar) me.emit ("character", character === "\r" ? "\n" : character);
- }else{
+ if (!stream.encoding){
if (onByte) me.emit ("byte", character);
continue;
}
- if (!onLine) continue;
- if (character === "\n" || character === "\r"){
- chunk = data.slice (offset, i);
+ byteOffset += realByteLength (character, me._settings.encoding);
+ if (onChar) me.emit ("character", character, byteOffset);
+
+ if (character === "\r"){
+ isCR = true;
+ continue;
+ }
+
+ if (character === "\n"){
+ chunk = data.slice (offset, isCR ? i - 1 : i);
offset = i + 1;
if (lastChunk){
@@ -103,21 +115,19 @@ BufferedReader.prototype.read = function (){
lastChunk = null;
}
- if (i + 1 !== len && character === "\r" && data[i + 1] === "\n"){
- i++;
- }
-
- me.emit ("line", chunk);
+ me.emit ("line", chunk, byteOffset);
}
+
+ isCR = false;
}
- if (onLine && stream.encoding && offset !== len){
+ if (stream.encoding && offset !== len){
var s = offset === 0 ? data : data.slice (offset);
lastChunk = lastChunk ? lastChunk.concat (s) : s;
}
}
- me.emit ("buffer", data);
+ me.emit ("buffer", data, byteOffset);
if (me._interrupted){
me._interrupted = false;
stream.destroy ();
@@ -128,7 +138,7 @@ BufferedReader.prototype.read = function (){
stream.on ("end", function (){
me._interrupted = false;
if (loop && lastChunk){
- me.emit ("line", lastChunk);
+ me.emit ("line", lastChunk, byteOffset);
}
me.emit ("end");
});
View
2  test/a
@@ -0,0 +1,2 @@
+asd
+qwe
View
22 test/playground.js
@@ -1 +1,21 @@
-var BufferedReader = require ("../build/buffered-reader");
+var BufferedReader = require ("../src/buffered-reader");
+
+new BufferedReader ("a", { encoding: "utf8" })
+ .on ("error", function (error){
+ console.log (error);
+ })
+ .on ("character", function (c, byteOffset){
+ console.log ("char: >" + c.charCodeAt(0) + "< " + byteOffset);
+ })
+ .on ("line", function (line, byteOffset){
+ console.log ("line: >" + line + "< " + byteOffset);
+ })
+ .on ("buffer", function (buffer, byteOffset){
+ console.log ("buffer: >" + buffer + "< " + byteOffset);
+ })
+ .read ();
+
+//console.log (Buffer.byteLength ("a", "utf8")); //bytes: 1, UNICODE hex: 0x61 (1), REAL hex: 0x61 (1)
+//console.log (Buffer.byteLength ("¡", "utf8")); //bytes: 2, UNICODE hex: 0xA1 (1), REAL hex: 0xC2A1 (2)
+//console.log (Buffer.byteLength ("↑", "utf8")); //bytes: 3, UNICODE hex: 0x2191 (2), REAL hex: 0xE28691 (3)
+//console.log (Buffer.byteLength ("𤁥", "utf8")); //bytes: 3, UNICODE hex: 0x24065 (3), REAL hex: 0xF0A481A5 (4)
Please sign in to comment.
Something went wrong with that request. Please try again.