From b0e0eeff3cc1eb05825c8f9157c2a7941cf1b503 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 27 Sep 2019 11:57:45 +0100 Subject: [PATCH] look for end marker across chunk boundaries --- services/spelling/app/js/ASpellWorker.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/services/spelling/app/js/ASpellWorker.js b/services/spelling/app/js/ASpellWorker.js index 66edfe8009..6d9921ece4 100644 --- a/services/spelling/app/js/ASpellWorker.js +++ b/services/spelling/app/js/ASpellWorker.js @@ -130,11 +130,16 @@ class ASpellWorker { this.pipe.stdout.setEncoding('utf8') // ensure utf8 output is handled correctly var output = '' - const endMarker = new RegExp('^[a-z][a-z]', 'm') - this.pipe.stdout.on('data', chunk => { - output = output + chunk - // We receive the language code from Aspell as the end of data marker - if (chunk.toString().match(endMarker)) { + const endMarkerRegex = new RegExp('^[a-z][a-z]', 'm') + this.pipe.stdout.on('data', data => { + // We receive the language code from Aspell as the end of data marker in + // the data. The input is a utf8 encoded string. + let oldPos = output.length + output = output + data + // The end marker may cross the end of a chunk, so we optimise the search + // using the regex lastIndex property. + endMarkerRegex.lastIndex = oldPos > 2 ? oldPos - 2 : 0 + if (endMarkerRegex.test(output)) { if (this.callback != null) { this.callback(null, output.slice()) this.callback = null // only allow one callback in use