Skip to content

Commit ac6fff1

Browse files
committed
[js] Make nqp::bitand_s, nqp::bitxor_s and nqp::bitand_s more correct
1 parent 06f1eed commit ac6fff1

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

src/vm/js/nqp-runtime/core.js

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,49 +1303,65 @@ op.getlexrel = function(pad, name) {
13031303

13041304

13051305
op.bitand_s = function(a, b) {
1306-
let ret = '';
1307-
let i = 0;
1308-
while (true) {
1309-
const codepointA = a.codePointAt(i);
1310-
const codepointB = b.codePointAt(i);
1311-
if (codepointA === undefined || codepointB == undefined) {
1312-
return ret;
1313-
}
1314-
ret += String.fromCodePoint(codepointA & codepointB);
1315-
i++;
1306+
const codePointsA = []
1307+
const codePointsB = []
1308+
1309+
for (const c of a.normalize('NFC')) {
1310+
codePointsA.push(c.codePointAt(0));
1311+
}
1312+
for (const c of b.normalize('NFC')) {
1313+
codePointsB.push(c.codePointAt(0));
1314+
}
1315+
1316+
const ret = [];
1317+
1318+
for (let i = 0; i < codePointsA.length && i < codePointsB.length; i++) {
1319+
ret.push(codePointsA[i] & codePointsB[i]);
13161320
}
1321+
1322+
return String.fromCodePoint.apply(undefined, ret).normalize('NFC');
13171323
};
13181324

13191325
op.bitor_s = function(a, b) {
1320-
let ret = '';
1321-
let i = 0;
1322-
while (true) {
1323-
let codepointA = a.codePointAt(i);
1324-
let codepointB = b.codePointAt(i);
1325-
if (codepointA === undefined && codepointB == undefined) {
1326-
return ret;
1327-
}
1328-
if (codepointA === undefined) codepointA = 0;
1329-
if (codepointB === undefined) codepointB = 0;
1330-
ret += String.fromCodePoint(codepointA | codepointB);
1331-
i++;
1326+
const codePointsA = []
1327+
const codePointsB = []
1328+
1329+
for (const c of a.normalize('NFC')) {
1330+
codePointsA.push(c.codePointAt(0));
1331+
}
1332+
for (const c of b.normalize('NFC')) {
1333+
codePointsB.push(c.codePointAt(0));
1334+
}
1335+
1336+
const ret = [];
1337+
1338+
for (let i = 0; i < codePointsA.length || i < codePointsB.length; i++) {
1339+
ret.push((codePointsA[i] || 0) | (codePointsB[i] || 0));
13321340
}
1341+
1342+
return String.fromCodePoint.apply(undefined, ret).normalize('NFC');
13331343
};
13341344

13351345
op.bitxor_s = function(a, b) {
1336-
let ret = '';
1337-
let i = 0;
1338-
while (true) {
1339-
let codepointA = a.codePointAt(i);
1340-
let codepointB = b.codePointAt(i);
1341-
if (codepointA === undefined && codepointB == undefined) {
1342-
return ret;
1343-
}
1344-
if (codepointA === undefined) codepointA = 0;
1345-
if (codepointB === undefined) codepointB = 0;
1346-
ret += String.fromCodePoint(codepointA ^ codepointB);
1347-
i++;
1346+
const codePointsA = []
1347+
const codePointsB = []
1348+
1349+
for (const c of a.normalize('NFC')) {
1350+
codePointsA.push(c.codePointAt(0));
1351+
}
1352+
for (const c of b.normalize('NFC')) {
1353+
codePointsB.push(c.codePointAt(0));
13481354
}
1355+
1356+
const ret = [];
1357+
1358+
for (let i = 0; i < codePointsA.length || i < codePointsB.length; i++) {
1359+
ret.push((codePointsA[i] || 0) ^ (codePointsB[i] || 0));
1360+
}
1361+
1362+
1363+
1364+
return String.fromCodePoint.apply(undefined, ret).normalize('NFC');
13491365
};
13501366

13511367
op.replace = function(str, offset, count, repl) {

0 commit comments

Comments
 (0)