Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9417ab4

Browse files
Don't allow the wrong prefix in BigInteger.parse when you specify a base. Fixes #10.
1 parent 31b4a88 commit 9417ab4

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

‎biginteger.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,23 @@ BigInteger.parse = function(s, base) {
359359
s = expandExponential(s);
360360
}
361361

362-
var parts = /^([+\-]?)(0[xXcCbB])?([0-9A-Za-z]*)(?:\.\d*)?$/.exec(s);
362+
var prefixRE;
363+
if (typeof base === "undefined") {
364+
prefixRE = '0[xcb]';
365+
}
366+
else if (base == 16) {
367+
prefixRE = '0x';
368+
}
369+
else if (base == 8) {
370+
prefixRE = '0c';
371+
}
372+
else if (base == 2) {
373+
prefixRE = '0b';
374+
}
375+
else {
376+
prefixRE = '';
377+
}
378+
var parts = new RegExp('^([+\\-]?)(' + prefixRE + ')?([0-9a-z]*)(?:\\.\\d*)?$', 'i').exec(s);
363379
if (parts) {
364380
var sign = parts[1] || "+";
365381
var baseSection = parts[2] || "";

‎test/test-biginteger.js‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,6 @@ function testParse() {
266266
n = BigInteger.parse("-0c715");
267267
checkBigInteger(n, [461], -1);
268268

269-
n = BigInteger.parse("-0C715", 10);
270-
checkBigInteger(n, [715], -1);
271-
272269
n = BigInteger.parse("+0b1101");
273270
checkBigInteger(n, [13], 1);
274271

@@ -308,6 +305,27 @@ function testParse() {
308305
n = BigInteger.parse("1011", 36);
309306
checkBigInteger(n, BigInteger.base_log10 === 7 ? [46693] : [6693, 4], 1);
310307

308+
n = BigInteger.parse("0b", 16);
309+
checkBigInteger(n, [11], 1);
310+
311+
n = BigInteger.parse("0c", 16);
312+
checkBigInteger(n, [12], 1);
313+
314+
n = BigInteger.parse("0b12", 16);
315+
checkBigInteger(n, [2834], 1);
316+
317+
n = BigInteger.parse("0c12", 16);
318+
checkBigInteger(n, [3090], 1);
319+
320+
n = BigInteger.parse("0b101", 2);
321+
checkBigInteger(n, [5], 1);
322+
323+
n = BigInteger.parse("0c101", 8);
324+
checkBigInteger(n, [65], 1);
325+
326+
n = BigInteger.parse("0x101", 16);
327+
checkBigInteger(n, [257], 1);
328+
311329
BigInteger.parse("1", 2);
312330
BigInteger.parse("2", 3);
313331
BigInteger.parse("3", 4);
@@ -372,6 +390,13 @@ function testParseFail() {
372390
assertThrows(createTest("52", 5), digitError);
373391
assertThrows(createTest("23a105"), digitError);
374392
assertThrows(createTest("DeadBeef", 15), digitError);
393+
assertThrows(createTest("-0C715", 10), digitError);
394+
assertThrows(createTest("-0x715", 10), digitError);
395+
assertThrows(createTest("-0b715", 10), digitError);
396+
assertThrows(createTest("-0x715", 8), digitError);
397+
assertThrows(createTest("-0b715", 8), digitError);
398+
assertThrows(createTest("-0C715", 2), digitError);
399+
assertThrows(createTest("-0x715", 2), digitError);
375400

376401
assertThrows(createTest("2", 2), digitError);
377402
assertThrows(createTest("3", 3), digitError);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /