Skip to content

Commit

Permalink
Trying to implement byteOffset for read()
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Llamas committed May 13, 2012
1 parent 373cbaf commit 4963abc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
38 changes: 24 additions & 14 deletions src/buffered-reader.js
Expand Up @@ -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.";
Expand Down Expand Up @@ -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;
Expand All @@ -80,44 +86,48 @@ 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){
chunk = lastChunk.concat (chunk);
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 ();
Expand All @@ -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");
});
Expand Down
2 changes: 2 additions & 0 deletions test/a
@@ -0,0 +1,2 @@
asd
qwe
22 changes: 21 additions & 1 deletion 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)

0 comments on commit 4963abc

Please sign in to comment.