Incorrect string length computation
alokmenghrajani opened this issue · 0 comments
alokmenghrajani commented
var qrcode = new QRCode(document.getElementById("qrcode"), {
text: "01234567890abcd",
correctLevel : QRCode.CorrectLevel.L
});
console.log(qrcode._oQRCode.getModuleCount());
Should log 21 but this logs 25. The reason is _getUTF8Length is comparing a length to a string and almost always ends up adding 3 (*):
function _getUTF8Length(sText) {
var replacedText = encodeURI(sText).toString().replace(/\%[0-9a-fA-F]{2}/g, 'a');
return replacedText.length;
}
I'm not 100% sure, but I think the following fix is correct:
507c507
< return replacedText.length + (replacedText.length != sText ? 3 : 0);
---
> return replacedText.length;
Happy to send a PR if there's agreement on this fix. Otherwise, I'll let people more familiar with this computation decide what's the correct fix.
* It doesn't add 3 if replacedText
equals ""
, "1"
, "02"
, etc. which are degenerate cases.