Skip to content

Commit d1d8d0b

Browse files
committed
Merge pull request #8 from tyrasd/patch-1
use global offset for buffered chunks
2 parents c2ffef3 + d3f8149 commit d1d8d0b

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

index.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,22 @@ function BinarySplit (matcher) {
1414
function write (buf, enc, done) {
1515
bufcount++
1616
var offset = 0
17-
17+
var lastMatch = 0
1818
if (buffered) {
1919
buf = bops.join([buffered, buf])
20+
offset = buffered.length
2021
buffered = undefined
2122
}
2223

23-
while (buf) {
24+
while (true) {
2425
var idx = firstMatch(buf, offset)
25-
if (idx) {
26-
var line = bops.subarray(buf, offset, idx)
27-
if (idx === buf.length) {
28-
buffered = line
29-
buf = undefined
30-
offset = idx
31-
} else {
32-
this.push(line)
33-
offset = idx + matcher.length
34-
}
35-
} else if (idx === 0) {
36-
buf = bops.subarray(buf, offset + matcher.length)
26+
if (idx !== -1 && idx < buf.length) {
27+
this.push(bops.subarray(buf, lastMatch, idx))
28+
offset = idx + matcher.length
29+
lastMatch = offset
3730
} else {
38-
if (offset >= buf.length) {
39-
buffered = undefined
40-
} else {
41-
buffered = buf
42-
}
43-
buf = undefined
31+
buffered = bops.subarray(buf, lastMatch)
32+
break
4433
}
4534
}
4635

@@ -54,7 +43,7 @@ function BinarySplit (matcher) {
5443
}
5544

5645
function firstMatch (buf, offset) {
57-
if (offset >= buf.length) return false
46+
if (offset >= buf.length) return -1
5847
for (var i = offset; i < buf.length; i++) {
5948
if (buf[i] === matcher[0]) {
6049
if (matcher.length > 1) {

test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,25 @@ test('matcher at index 0 check', function (t) {
6565
splitStream.write(new Buffer('\nhello\nmax'))
6666
splitStream.end()
6767
})
68+
69+
test('chunked input', function (t) {
70+
fs.createReadStream('test.json')
71+
.pipe(split('\n'))
72+
.pipe(split('i'))
73+
.pipe(splitTest(':', function (err, items) {
74+
if (err) throw err
75+
t.equals(items.length, 4)
76+
t.end()
77+
}))
78+
})
79+
80+
test('chunked input with long matcher', function (t) {
81+
fs.createReadStream('test.json')
82+
.pipe(split('\n'))
83+
.pipe(splitTest('hello', function (err, items) {
84+
if (err) throw err
85+
t.equals(items.length, 2)
86+
t.equals(items[0].toString(), '{"')
87+
t.end()
88+
}))
89+
})

0 commit comments

Comments
 (0)