-
Notifications
You must be signed in to change notification settings - Fork 741
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
jbrower2
wants to merge
3
commits into
MikeMcl:master
Choose a base branch
from
jbrower2:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
|
||
|
||
//------------------------------------------------------------------------------------------ | ||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -568,7 +556,7 @@ | |
MODULO_MODE: MODULO_MODE, | ||
POW_PRECISION: POW_PRECISION, | ||
FORMAT: FORMAT, | ||
ALPHABET: ALPHABET | ||
ALPHABET: ALPHABET.original | ||
}; | ||
}; | ||
|
||
|
@@ -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. | ||
|
@@ -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++) { | ||
|
||
|
@@ -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; | ||
} | ||
} | ||
} | ||
|
||
if (!caseSensitive) { | ||
lowerAlphabet = alphabet.toLowerCase(); | ||
} | ||
|
||
function charAt(i) { | ||
return alphabet.charAt(i); | ||
} | ||
|
||
// case-insensitive version of indexOf | ||
function charIndex(c, b) { | ||
var i = caseSensitive | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The definition of the |
||
? 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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ascaseSensitive
istrue
.