Skip to content

Newline lines from .lines from createFileStream converted to 0 #11

Open
thegoleffect opened this Issue Nov 30, 2011 · 5 comments

6 participants

@thegoleffect

I'm using code like this:

new Lazy(fs.createReadStream(fpath, {flags: 'r', encoding: 'utf8'}))
  .on("end", finished)
  .lines
  .forEach((line) ->
    all_lines.push(line)
  )

My files have blank lines that are just newline characters. Any lines like that get converted to "\u0030" aka the "0" character. Is this intentional?

@pkrumins
Owner
pkrumins commented Dec 1, 2011

It's not intentional.

@krjkkim
krjkkim commented Feb 13, 2012

I had the same problem and I looked at the source code. The following code looks suspicious

self.__defineGetter__('lines', function () {
        ...
            if(i>0) chunkArray.push(chunk.slice(lastNewLineIndex, i));
        ...

By simply removing the guarding if statement seems to solve the problem.

self.__defineGetter__('lines', function () {
        ...
            chunkArray.push(chunk.slice(lastNewLineIndex, i));
        ...
@mhart
mhart commented May 3, 2012

I'm seeing the same thing. Here's a simple test case that should just echo each line out as is (works fine for non-blank lines, produces undefined for each blank line if run interactively and 0 if piped):

Lazy = require('lazy');

process.stdin.resume();
process.stdin.setEncoding('utf8');

Lazy(process.stdin)
  .lines
  .map(String)
  .forEach(function(line) {
    process.stdout.write(line + '\n');
  });
@opatry
opatry commented Jul 12, 2013

Please, what is the status according to this issue? I'm stuck with that…

@netj netj added a commit to netj/node-lazy that referenced this issue Aug 11, 2013
@netj netj Fix pkrumins#11: .lines bug creating "0"s from empty lines
Empty lines (lines ending with newline immediately) were generating
bogus "0"s as discussed in pkrumins#11.

The problem was caused by a careless use of + in `mergeBuffers`, which
resulted in creating a Buffer with string "0" instead of an empty one
with length 0 when an empty buffer was among the ones being merged.
mergeBuffers also had an undefined case for empty set of input buffers,
which has been fixed to always return a buffer with length 0.

This also makes the .lines getter code more readable by using a better
variable name and eliminating tabs from white space.
3070204
@netj
netj commented Aug 11, 2013

Hi @opatry, I was also stuck with this problem and found a fix. Hope my fix also works for you.

Hi @pkrumins, could you please merge my fix and create a release soon, so I can simply install lazy from npm? You can easily check the bug and my fix with the following coffeescript snippet:

lines = ["a", "", "b"]
Lazy = require("lazy")
p = require("child_process").spawn("sh", ["-c", ("echo #{l}" for l in lines).join("; ")])
new Lazy(p.stdout).lines.map(String).join((x) -> console.log (x.join(",") is lines.join(",")), x)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.