Skip to content

Commit 70afdf8

Browse files
authored
Merge pull request #202 from IanMcCurdy/fixOffsetForNCLOB
Fix offset calculation for NCLOB data containing non-BMP characters
2 parents b41a903 + 1df79ab commit 70afdf8

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

lib/protocol/Lob.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function Lob(readLob, ld, options) {
7171

7272
function textChunkLength(chunk, useCesu8) {
7373
return (useCesu8) ?
74-
util.convert.lengthInCesu8(chunk) :
74+
util.convert.lengthInCesu8(chunk, false) :
7575
chunk.toString('utf-8').length;
7676
}
7777

@@ -165,4 +165,4 @@ function handleData(data) {
165165
if (this._running) {
166166
process.nextTick(sendReadLob.bind(this));
167167
}
168-
}
168+
}

lib/util/convert.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ function decode(buffer, useCesu8) {
1818
buffer.toString('utf-8');
1919
}
2020

21-
function lengthInCesu8(buffer) {
21+
function lengthInCesu8(buffer, combineSurrogates = true) {
2222
var text = iconv.decode(buffer, 'cesu8');
23-
24-
// count surrogate parts as single char (String.lenght counts it as 2)
25-
var count = 0, code;
26-
for (var i = 0; i < text.length; i++) {
27-
code = text.charCodeAt(i);
28-
if (0xD800 <= code && code <= 0xDBFF) { i++; }
29-
count++;
23+
var len = 0;
24+
if (combineSurrogates) {
25+
// count surrogate pairs as a single char (String.length counts it as 2)
26+
var code = 0;
27+
for (var i = 0; i < text.length; i++) {
28+
code = text.charCodeAt(i);
29+
if (0xD800 <= code && code <= 0xDBFF) { i++; }
30+
len++;
31+
}
32+
} else {
33+
len = text.length;
3034
}
31-
return count;
32-
}
35+
return len;
36+
}

test/lib.Lob.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ describe('Lib', function () {
176176
var lob = new Lob(readLob, createLobDescriptor(LobSourceType.NCLOB, chunk, 1), { useCesu8: true });
177177
lob.length.should.equal(1);
178178
lob.increaseOffset(chunk);
179-
lob._offset.should.equal(2);
179+
lob._offset.should.equal(3);
180180
});
181181

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

205205
function createLobDescriptor(type, chunk, charLength) {
@@ -209,4 +209,4 @@ describe('Lib', function () {
209209
}
210210

211211
});
212-
});
212+
});

test/util.convert.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ describe('Util', function () {
4040
util.convert.decode(outOfBomCesuBuffer, true).should.eql(outOfBom);
4141
});
4242

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

47+
it('should count cesu8 surrogates in cesu8 encoded buffer', function () {
48+
util.convert.lengthInCesu8(outOfBomCesuBuffer, false).should.equal(2);
49+
});
50+
4751
});
4852

4953
});

0 commit comments

Comments
 (0)