multipasta
Advanced tools
Comparing version 0.1.11 to 0.1.12
@@ -117,2 +117,60 @@ "use strict"; | ||
} | ||
function writeBuffer(chunk_) { | ||
let chunk = Buffer.isBuffer(chunk_) ? chunk_ : Buffer.from(chunk_); | ||
let chunkLength = chunk.length; | ||
if (chunkLength < state.needleLength) { | ||
if (state.previousChunk === undefined) { | ||
state.previousChunk = chunk; | ||
state.previousChunkLength = chunkLength; | ||
return; | ||
} | ||
} | ||
if (state.previousChunk !== undefined) { | ||
const newChunk = Buffer.allocUnsafe(state.previousChunkLength + chunkLength); | ||
newChunk.set(state.previousChunk); | ||
newChunk.set(chunk, state.previousChunkLength); | ||
chunk = newChunk; | ||
chunkLength = state.previousChunkLength + chunkLength; | ||
state.previousChunk = undefined; | ||
} | ||
let pos = 0; | ||
while (pos < chunkLength) { | ||
const match = chunk.indexOf(state.needle, pos); | ||
if (match > -1) { | ||
if (match > pos) { | ||
callback(state.matchIndex, chunk.subarray(pos, match)); | ||
} | ||
state.matchIndex += 1; | ||
pos = match + state.needleLength; | ||
} else if (chunk[chunkLength - 1] in state.indexes) { | ||
const indexes = state.indexes[chunk[chunkLength - 1]]; | ||
let earliestIndex = -1; | ||
for (let i = 0, len = indexes.length; i < len; i++) { | ||
const index = indexes[i]; | ||
if (chunk[chunkLength - 1 - index] === state.firstByte && i > earliestIndex) { | ||
earliestIndex = index; | ||
} | ||
} | ||
if (earliestIndex === -1) { | ||
if (pos === 0) { | ||
callback(state.matchIndex, chunk); | ||
} else { | ||
callback(state.matchIndex, chunk.subarray(pos)); | ||
} | ||
} else { | ||
callback(state.matchIndex, chunk.subarray(pos, chunkLength - 1 - earliestIndex)); | ||
state.previousChunk = chunk.subarray(chunkLength - 1 - earliestIndex); | ||
state.previousChunkLength = earliestIndex + 1; | ||
} | ||
break; | ||
} else { | ||
if (pos === 0) { | ||
callback(state.matchIndex, chunk); | ||
} else { | ||
callback(state.matchIndex, chunk.subarray(pos)); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
function end() { | ||
@@ -127,3 +185,3 @@ if (state.previousChunk !== undefined && state.previousChunk !== seed) { | ||
return { | ||
write, | ||
write: "Buffer" in globalThis ? writeBuffer : write, | ||
end | ||
@@ -130,0 +188,0 @@ }; |
@@ -45,3 +45,2 @@ "use strict"; | ||
let finished = false; | ||
let writeController; | ||
const parser = MP.make({ | ||
@@ -100,6 +99,4 @@ ...config, | ||
onError(error_) { | ||
if (error !== undefined) return; | ||
error = error_; | ||
if (writeController !== undefined) { | ||
writeController.error(error); | ||
} | ||
if (readResolve !== undefined) readResolve(); | ||
@@ -114,3 +111,2 @@ }, | ||
write(chunk, controller) { | ||
writeController = controller; | ||
parser.write(chunk); | ||
@@ -117,0 +113,0 @@ }, |
@@ -111,2 +111,60 @@ function makeState(needle_) { | ||
} | ||
function writeBuffer(chunk_) { | ||
let chunk = Buffer.isBuffer(chunk_) ? chunk_ : Buffer.from(chunk_); | ||
let chunkLength = chunk.length; | ||
if (chunkLength < state.needleLength) { | ||
if (state.previousChunk === undefined) { | ||
state.previousChunk = chunk; | ||
state.previousChunkLength = chunkLength; | ||
return; | ||
} | ||
} | ||
if (state.previousChunk !== undefined) { | ||
const newChunk = Buffer.allocUnsafe(state.previousChunkLength + chunkLength); | ||
newChunk.set(state.previousChunk); | ||
newChunk.set(chunk, state.previousChunkLength); | ||
chunk = newChunk; | ||
chunkLength = state.previousChunkLength + chunkLength; | ||
state.previousChunk = undefined; | ||
} | ||
let pos = 0; | ||
while (pos < chunkLength) { | ||
const match = chunk.indexOf(state.needle, pos); | ||
if (match > -1) { | ||
if (match > pos) { | ||
callback(state.matchIndex, chunk.subarray(pos, match)); | ||
} | ||
state.matchIndex += 1; | ||
pos = match + state.needleLength; | ||
} else if (chunk[chunkLength - 1] in state.indexes) { | ||
const indexes = state.indexes[chunk[chunkLength - 1]]; | ||
let earliestIndex = -1; | ||
for (let i = 0, len = indexes.length; i < len; i++) { | ||
const index = indexes[i]; | ||
if (chunk[chunkLength - 1 - index] === state.firstByte && i > earliestIndex) { | ||
earliestIndex = index; | ||
} | ||
} | ||
if (earliestIndex === -1) { | ||
if (pos === 0) { | ||
callback(state.matchIndex, chunk); | ||
} else { | ||
callback(state.matchIndex, chunk.subarray(pos)); | ||
} | ||
} else { | ||
callback(state.matchIndex, chunk.subarray(pos, chunkLength - 1 - earliestIndex)); | ||
state.previousChunk = chunk.subarray(chunkLength - 1 - earliestIndex); | ||
state.previousChunkLength = earliestIndex + 1; | ||
} | ||
break; | ||
} else { | ||
if (pos === 0) { | ||
callback(state.matchIndex, chunk); | ||
} else { | ||
callback(state.matchIndex, chunk.subarray(pos)); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
function end() { | ||
@@ -121,3 +179,3 @@ if (state.previousChunk !== undefined && state.previousChunk !== seed) { | ||
return { | ||
write, | ||
write: "Buffer" in globalThis ? writeBuffer : write, | ||
end | ||
@@ -124,0 +182,0 @@ }; |
@@ -9,3 +9,2 @@ import * as MP from "./index.js"; | ||
let finished = false; | ||
let writeController; | ||
const parser = MP.make({ | ||
@@ -64,6 +63,4 @@ ...config, | ||
onError(error_) { | ||
if (error !== undefined) return; | ||
error = error_; | ||
if (writeController !== undefined) { | ||
writeController.error(error); | ||
} | ||
if (readResolve !== undefined) readResolve(); | ||
@@ -78,3 +75,2 @@ }, | ||
write(chunk, controller) { | ||
writeController = controller; | ||
parser.write(chunk); | ||
@@ -81,0 +77,0 @@ }, |
{ | ||
"name": "multipasta", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -169,2 +169,73 @@ interface SearchState { | ||
function writeBuffer(chunk_: Buffer | Uint8Array): void { | ||
let chunk = Buffer.isBuffer(chunk_) ? chunk_ : Buffer.from(chunk_) | ||
let chunkLength = chunk.length | ||
if (chunkLength < state.needleLength) { | ||
if (state.previousChunk === undefined) { | ||
state.previousChunk = chunk | ||
state.previousChunkLength = chunkLength | ||
return | ||
} | ||
} | ||
if (state.previousChunk !== undefined) { | ||
const newChunk = Buffer.allocUnsafe( | ||
state.previousChunkLength + chunkLength, | ||
) | ||
newChunk.set(state.previousChunk) | ||
newChunk.set(chunk, state.previousChunkLength) | ||
chunk = newChunk | ||
chunkLength = state.previousChunkLength + chunkLength | ||
state.previousChunk = undefined | ||
} | ||
let pos = 0 | ||
while (pos < chunkLength) { | ||
const match = chunk.indexOf(state.needle, pos) | ||
if (match > -1) { | ||
if (match > pos) { | ||
callback(state.matchIndex, chunk.subarray(pos, match)) | ||
} | ||
state.matchIndex += 1 | ||
pos = match + state.needleLength | ||
} else if (chunk[chunkLength - 1] in state.indexes) { | ||
const indexes = state.indexes[chunk[chunkLength - 1]] | ||
let earliestIndex = -1 | ||
for (let i = 0, len = indexes.length; i < len; i++) { | ||
const index = indexes[i] | ||
if ( | ||
chunk[chunkLength - 1 - index] === state.firstByte && | ||
i > earliestIndex | ||
) { | ||
earliestIndex = index | ||
} | ||
} | ||
if (earliestIndex === -1) { | ||
if (pos === 0) { | ||
callback(state.matchIndex, chunk) | ||
} else { | ||
callback(state.matchIndex, chunk.subarray(pos)) | ||
} | ||
} else { | ||
callback( | ||
state.matchIndex, | ||
chunk.subarray(pos, chunkLength - 1 - earliestIndex), | ||
) | ||
state.previousChunk = chunk.subarray(chunkLength - 1 - earliestIndex) | ||
state.previousChunkLength = earliestIndex + 1 | ||
} | ||
break | ||
} else { | ||
if (pos === 0) { | ||
callback(state.matchIndex, chunk) | ||
} else { | ||
callback(state.matchIndex, chunk.subarray(pos)) | ||
} | ||
break | ||
} | ||
} | ||
} | ||
function end(): void { | ||
@@ -180,3 +251,6 @@ if (state.previousChunk !== undefined && state.previousChunk !== seed) { | ||
return { write, end } as const | ||
return { | ||
write: "Buffer" in globalThis ? writeBuffer : write, | ||
end, | ||
} as const | ||
} |
@@ -35,3 +35,2 @@ import * as MP from "./index.js" | ||
let finished = false | ||
let writeController: WritableStreamDefaultController | undefined | ||
@@ -86,6 +85,4 @@ const parser = MP.make({ | ||
onError(error_) { | ||
if (error !== undefined) return | ||
error = error_ | ||
if (writeController !== undefined) { | ||
writeController.error(error) | ||
} | ||
if (readResolve !== undefined) readResolve() | ||
@@ -102,3 +99,2 @@ }, | ||
write(chunk, controller) { | ||
writeController = controller | ||
parser.write(chunk) | ||
@@ -105,0 +101,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
182587
3040