Skip to content

Commit

Permalink
Fix long string splitting bugs
Browse files Browse the repository at this point in the history
Fix bug where bytes are lost from long strings with no whitespace. If no
whitespace was found, the starting position of the next call should not be
shifted forward.

If a string's length is equal to maxLength, it should be returned as it
is.
  • Loading branch information
ghostal authored and jirwin committed Apr 15, 2015
1 parent 4f70878 commit 0603fc8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
13 changes: 9 additions & 4 deletions lib/irc.js
Expand Up @@ -910,14 +910,18 @@ Client.prototype.action = function(channel, text) {
};

Client.prototype._splitLongLines = function(words, maxLength, destination) {
if (words.length < maxLength) {
if (words.length == 0) {
return destination;
}
if (words.length <= maxLength) {
destination.push(words);
return destination;
}
var c = words[maxLength - 1];
var c = words[maxLength];
var cutPos;
var wsLength = 1;
if (c.match(/\s/)) {
cutPos = maxLength - 1;
cutPos = maxLength;
} else {
var offset = 1;
while ((maxLength - offset) > 0) {
Expand All @@ -930,11 +934,12 @@ Client.prototype._splitLongLines = function(words, maxLength, destination) {
}
if (maxLength - offset <= 0) {
cutPos = maxLength;
wsLength = 0;
}
}
var part = words.substring(0, cutPos);
destination.push(part);
return this._splitLongLines(words.substring(cutPos + 1, words.length), maxLength, destination);
return this._splitLongLines(words.substring(cutPos + wsLength, words.length), maxLength, destination);
};

Client.prototype.say = function(target, text) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -13,7 +13,8 @@
"Mischa Spiegelmock <revmischa@cpan.org>",
"Justin Gallardo <justin.gallardo@gmail.com>",
"Chris Nehren <cnehren@pobox.com>",
"Henri Niemeläinen <aivot-on@iki.fi>"
"Henri Niemeläinen <aivot-on@iki.fi>",
"Alex Miles <ghostaldev@gmail.com>"
],
"repository": {
"type": "git",
Expand Down
24 changes: 23 additions & 1 deletion test/data/fixtures.json
Expand Up @@ -155,5 +155,27 @@
":sblack1!~sblack1@unaffiliated/sblack1 NICK :sblack\r\n",
":TijG!~TijG@null.1ago.be PRIVMSG #ubuntu :ThinkPad\r\n"
]
}
},
"_splitLongLines": [
{
"input": "abcde ",
"maxLength": 5,
"result": ["abcde"]
},
{
"input": "abcde",
"maxLength": 5,
"result": ["abcde"]
},
{
"input": "abcdefghijklmnopqrstuvwxyz",
"maxLength": 5,
"result": ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
},
{
"input": "abc abcdef abc abcd abc",
"maxLength": 5,
"result": ["abc", "abcde", "f abc", "abcd", "abc"]
}
]
}
19 changes: 19 additions & 0 deletions test/test-irc.js
Expand Up @@ -65,3 +65,22 @@ function runTests(t, isSecure, useSecureObject) {
mock.close();
});
}

test ('splitting of long lines', function(t) {
var port = 6667;
var mock = testHelpers.MockIrcd(port, 'utf-8', false);
var client = new irc.Client('localhost', 'testbot', {
secure: false,
selfSigned: true,
port: port,
retryCount: 0,
debug: true
});

var group = testHelpers.getFixtures('_splitLongLines');
t.plan(group.length);
group.forEach(function(item) {
t.deepEqual(client._splitLongLines(item.input, item.maxLength, []), item.result);
});
mock.close();
});

0 comments on commit 0603fc8

Please sign in to comment.