Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix offset calculation for NCLOB data containing non-BMP characters #202

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/protocol/Lob.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function Lob(readLob, ld, options) {

function textChunkLength(chunk, useCesu8) {
return (useCesu8) ?
util.convert.lengthInCesu8(chunk) :
util.convert.lengthInCesu8(chunk, false) :
chunk.toString('utf-8').length;
}

Expand Down Expand Up @@ -165,4 +165,4 @@ function handleData(data) {
if (this._running) {
process.nextTick(sendReadLob.bind(this));
}
}
}
24 changes: 14 additions & 10 deletions lib/util/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ function decode(buffer, useCesu8) {
buffer.toString('utf-8');
}

function lengthInCesu8(buffer) {
function lengthInCesu8(buffer, combineSurrogates = true) {
var text = iconv.decode(buffer, 'cesu8');

// count surrogate parts as single char (String.lenght counts it as 2)
var count = 0, code;
for (var i = 0; i < text.length; i++) {
code = text.charCodeAt(i);
if (0xD800 <= code && code <= 0xDBFF) { i++; }
count++;
var len = 0;
if (combineSurrogates) {
// count surrogate pairs as a single char (String.length counts it as 2)
var code = 0;
for (var i = 0; i < text.length; i++) {
code = text.charCodeAt(i);
if (0xD800 <= code && code <= 0xDBFF) { i++; }
len++;
}
} else {
len = text.length;
}
return count;
}
return len;
}
6 changes: 3 additions & 3 deletions test/lib.Lob.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Lib', function () {
var lob = new Lob(readLob, createLobDescriptor(LobSourceType.NCLOB, chunk, 1), { useCesu8: true });
lob.length.should.equal(1);
lob.increaseOffset(chunk);
lob._offset.should.equal(2);
lob._offset.should.equal(3);
});

it('should create a Lob with type CLOB', function () {
Expand All @@ -199,7 +199,7 @@ describe('Lib', function () {
var lob = new Lob(readLob, ld, { useCesu8: true, useDefaultType: true });
lob.length.should.equal(1);
lob.increaseOffset(chunk);
lob._offset.should.equal(2);
lob._offset.should.equal(3);
});

function createLobDescriptor(type, chunk, charLength) {
Expand All @@ -209,4 +209,4 @@ describe('Lib', function () {
}

});
});
});
6 changes: 5 additions & 1 deletion test/util.convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ describe('Util', function () {
util.convert.decode(outOfBomCesuBuffer, true).should.eql(outOfBom);
});

it('should count cesu8 charactes in cesu8 encoded buffer', function () {
it('should count cesu8 characters in cesu8 encoded buffer', function () {
util.convert.lengthInCesu8(outOfBomCesuBuffer).should.equal(1);
});

it('should count cesu8 surrogates in cesu8 encoded buffer', function () {
util.convert.lengthInCesu8(outOfBomCesuBuffer, false).should.equal(2);
});

});

});