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

Store alphabet internally as object (fixes #309 and #250) #310

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 49 additions & 20 deletions bignumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
// The alphabet used for base conversion. It must be at least 2 characters long, with no '+',
// '-', '.', whitespace, or repeated character.
// '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';
ALPHABET = createAlphabet('0123456789abcdefghijklmnopqrstuvwxyz');


//------------------------------------------------------------------------------------------
Expand All @@ -179,8 +179,7 @@
* [b] {number} The base of v. Integer, 2 to ALPHABET.length inclusive.
*/
function BigNumber(v, b) {
var alphabet, c, caseChanged, e, i, isNum, len, str,
x = this;
var c, e, i, isNum, len, str, x = this;

// Enable constructor call without `new`.
if (!(x instanceof BigNumber)) return new BigNumber(v, b);
Expand Down Expand Up @@ -252,7 +251,7 @@

// Allow exponential notation to be used with base 10 argument, while
// also rounding to DECIMAL_PLACES as with other bases.
if (b == 10) {
if (b == 10 && ALPHABET.decimalCompatible) {
x = new BigNumber(v);
return round(x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE);
}
Expand All @@ -275,30 +274,19 @@
x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
}

alphabet = ALPHABET.slice(0, b);
e = i = 0;

// Check that str is a valid base b number.
// Don't use RegExp, so alphabet can contain special characters.
for (len = str.length; i < len; i++) {
if (alphabet.indexOf(c = str.charAt(i)) < 0) {
if (ALPHABET.charIndex(c = str.charAt(i), b) < 0) {
if (c == '.') {

// If '.' is not the first character and it has not be found before.
if (i > e) {
e = len;
continue;
}
} else if (!caseChanged) {

// Allow e.g. hexadecimal 'FF' as well as 'ff'.
if (str == str.toUpperCase() && (str = str.toLowerCase()) ||
str == str.toLowerCase() && (str = str.toUpperCase())) {
caseChanged = true;
i = -1;
e = 0;
continue;
}
}

return parseNumeric(x, String(v), isNum, b);
Expand Down Expand Up @@ -544,7 +532,7 @@
// Disallow if less than two characters,
// or if it contains '+', '-', '.', whitespace, or a repeated character.
if (typeof v == 'string' && !/^.?$|[+\-.\s]|(.).*\1/.test(v)) {
ALPHABET = v;
ALPHABET = createAlphabet(v);
} else {
throw Error
(bignumberError + p + ' invalid: ' + v);
Expand All @@ -568,7 +556,7 @@
MODULO_MODE: MODULO_MODE,
POW_PRECISION: POW_PRECISION,
FORMAT: FORMAT,
ALPHABET: ALPHABET
ALPHABET: ALPHABET.original
};
};

Expand Down Expand Up @@ -812,7 +800,7 @@

// Called by BigNumber and BigNumber.prototype.toString.
convertBase = (function () {
var decimal = '0123456789';
var decimal = createAlphabet('0123456789');

/*
* Convert string of baseIn to an array of numbers of baseOut.
Expand All @@ -829,7 +817,7 @@
for (; i < len;) {
for (arrL = arr.length; arrL--; arr[arrL] *= baseIn);

arr[0] += alphabet.indexOf(str.charAt(i++));
arr[0] += alphabet.charIndex(str.charAt(i++));

for (j = 0; j < arr.length; j++) {

Expand Down Expand Up @@ -2877,6 +2865,47 @@
}


function createAlphabet(alphabet) {
var i, j, c, d, caseSensitive, lowerAlphabet;

// check if alphabet is case-sensitive
for (i = 0; i < alphabet.length; i++) {
c = alphabet.charAt(i);
for (j = 0; j < i; j++) {
d = alphabet.charAt(j);
if (c.toLowerCase() === d.toLowerCase()) {
caseSensitive = true;
Copy link
Owner

@MikeMcl MikeMcl Dec 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to break out of the loops as soon as caseSensitive is true.

}
}
}

if (!caseSensitive) {
lowerAlphabet = alphabet.toLowerCase();
}

function charAt(i) {
return alphabet.charAt(i);
}

// case-insensitive version of indexOf
function charIndex(c, b) {
var i = caseSensitive
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The definition of the charIndex function could depend on the value of caseSensitive, rather than checking its value every call.

? alphabet.indexOf(c)
: lowerAlphabet.indexOf(c.toLowerCase());

return !b || i < b ? i : -1;
}

return {
original: alphabet,
decimalCompatible: alphabet.slice(0, 10) === '0123456789',
length: alphabet.length,
charAt: charAt,
charIndex: charIndex,
};
}


// EXPORT


Expand Down
8 changes: 4 additions & 4 deletions test/methods/BigNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,10 @@ Test('bigNumber', function () {
BigNumber.config({ALPHABET: 'xy'});

t('1', 'y', 2); // 1
t('2', 'yx', 2); // 10
t('3', 'yy', 2); // 11
t('4', 'yxx', 2); // 100
t('5', 'yxy', 2); // 101
t('2', 'Yx', 2); // 10
t('3', 'yY', 2); // 11
t('4', 'YxX', 2); // 100
t('5', 'yXy', 2); // 101

BigNumber.config({ALPHABET: '0123456789*#'});

Expand Down