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

Incorrect string length computation #284

Open
alokmenghrajani opened this issue Nov 24, 2022 · 0 comments
Open

Incorrect string length computation #284

alokmenghrajani opened this issue Nov 24, 2022 · 0 comments

Comments

@alokmenghrajani
Copy link

alokmenghrajani commented Nov 24, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant