davidshimjs / qrcodejs

Cross-browser QRCode generator for javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect string length computation

alokmenghrajani opened this issue · comments

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.