react / wstein / node_modules / browserify / node_modules / browser-pack / node_modules / combine-source-map / node_modules / source-map / lib / source-map / base64-vlq.js
80559 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2011 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*6* Based on the Base 64 VLQ implementation in Closure Compiler:7* https://code.google.com/p/closure-compiler/source/browse/trunk/src/com/google/debugging/sourcemap/Base64VLQ.java8*9* Copyright 2011 The Closure Compiler Authors. All rights reserved.10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions are12* met:13*14* * Redistributions of source code must retain the above copyright15* notice, this list of conditions and the following disclaimer.16* * Redistributions in binary form must reproduce the above17* copyright notice, this list of conditions and the following18* disclaimer in the documentation and/or other materials provided19* with the distribution.20* * Neither the name of Google Inc. nor the names of its21* contributors may be used to endorse or promote products derived22* from this software without specific prior written permission.23*24* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS25* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT26* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR27* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT28* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,29* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,31* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY32* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT33* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE34* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.35*/36if (typeof define !== 'function') {37var define = require('amdefine')(module, require);38}39define(function (require, exports, module) {4041var base64 = require('./base64');4243// A single base 64 digit can contain 6 bits of data. For the base 64 variable44// length quantities we use in the source map spec, the first bit is the sign,45// the next four bits are the actual value, and the 6th bit is the46// continuation bit. The continuation bit tells us whether there are more47// digits in this value following this digit.48//49// Continuation50// | Sign51// | |52// V V53// 1010115455var VLQ_BASE_SHIFT = 5;5657// binary: 10000058var VLQ_BASE = 1 << VLQ_BASE_SHIFT;5960// binary: 01111161var VLQ_BASE_MASK = VLQ_BASE - 1;6263// binary: 10000064var VLQ_CONTINUATION_BIT = VLQ_BASE;6566/**67* Converts from a two-complement value to a value where the sign bit is68* placed in the least significant bit. For example, as decimals:69* 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)70* 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)71*/72function toVLQSigned(aValue) {73return aValue < 074? ((-aValue) << 1) + 175: (aValue << 1) + 0;76}7778/**79* Converts to a two-complement value from a value where the sign bit is80* placed in the least significant bit. For example, as decimals:81* 2 (10 binary) becomes 1, 3 (11 binary) becomes -182* 4 (100 binary) becomes 2, 5 (101 binary) becomes -283*/84function fromVLQSigned(aValue) {85var isNegative = (aValue & 1) === 1;86var shifted = aValue >> 1;87return isNegative88? -shifted89: shifted;90}9192/**93* Returns the base 64 VLQ encoded value.94*/95exports.encode = function base64VLQ_encode(aValue) {96var encoded = "";97var digit;9899var vlq = toVLQSigned(aValue);100101do {102digit = vlq & VLQ_BASE_MASK;103vlq >>>= VLQ_BASE_SHIFT;104if (vlq > 0) {105// There are still more digits in this value, so we must make sure the106// continuation bit is marked.107digit |= VLQ_CONTINUATION_BIT;108}109encoded += base64.encode(digit);110} while (vlq > 0);111112return encoded;113};114115/**116* Decodes the next base 64 VLQ value from the given string and returns the117* value and the rest of the string via the out parameter.118*/119exports.decode = function base64VLQ_decode(aStr, aIndex, aOutParam) {120var strLen = aStr.length;121var result = 0;122var shift = 0;123var continuation, digit;124125do {126if (aIndex >= strLen) {127throw new Error("Expected more digits in base 64 VLQ value.");128}129digit = base64.decode(aStr.charAt(aIndex++));130continuation = !!(digit & VLQ_CONTINUATION_BIT);131digit &= VLQ_BASE_MASK;132result = result + (digit << shift);133shift += VLQ_BASE_SHIFT;134} while (continuation);135136aOutParam.value = fromVLQSigned(result);137aOutParam.rest = aIndex;138};139140});141142143