Browse Source

toByteArray(): use cached length value

master
Feross Aboukhadijeh 9 years ago
parent
commit
d544569378
  1. 8
      lib/b64.js

8
lib/b64.js

@ -23,8 +23,9 @@ var Arr = (typeof Uint8Array !== 'undefined')
function toByteArray (b64) {
var i, j, l, tmp, placeHolders, arr
var len = b64.length
if (b64.length % 4 > 0) {
if (len % 4 > 0) {
throw new Error('Invalid string. Length must be a multiple of 4')
}
@ -33,14 +34,13 @@ function toByteArray (b64) {
// represent one byte
// if there is only one, then the three characters before it represent 2 bytes
// this is just a cheap hack to not do indexOf twice
var len = b64.length
placeHolders = b64.charAt(len - 2) === '=' ? 2 : b64.charAt(len - 1) === '=' ? 1 : 0
// base64 is 4/3 + up to two characters of the original data
arr = new Arr(b64.length * 3 / 4 - placeHolders)
arr = new Arr(len * 3 / 4 - placeHolders)
// if there are placeholders, only get up to the last complete 4 chars
l = placeHolders > 0 ? b64.length - 4 : b64.length
l = placeHolders > 0 ? len - 4 : len
var L = 0

Loading…
Cancel
Save