source: trip-planner-front/node_modules/source-list-map/lib/base64-vlq.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/* -*- Mode: js; js-indent-level: 2; -*- */
2/*
3 * Copyright 2011 Mozilla Foundation and contributors
4 * Licensed under the New BSD license. See LICENSE or:
5 * http://opensource.org/licenses/BSD-3-Clause
6 *
7 * Based on the Base 64 VLQ implementation in Closure Compiler:
8 * https://code.google.com/p/closure-compiler/source/browse/trunk/src/com/google/debugging/sourcemap/Base64VLQ.java
9 *
10 * Copyright 2011 The Closure Compiler Authors. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are
13 * met:
14 *
15 * * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 * * Neither the name of Google Inc. nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*eslint no-bitwise:0,quotes:0,global-strict:0*/
39
40var charToIntMap = {};
41var intToCharMap = {};
42
43'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
44 .split('')
45 .forEach(function (ch, index) {
46 charToIntMap[ch] = index;
47 intToCharMap[index] = ch;
48 });
49
50var base64 = {};
51/**
52 * Encode an integer in the range of 0 to 63 to a single base 64 digit.
53 */
54base64.encode = function base64_encode(aNumber) {
55 if (aNumber in intToCharMap) {
56 return intToCharMap[aNumber];
57 }
58 throw new TypeError("Must be between 0 and 63: " + aNumber);
59};
60
61/**
62 * Decode a single base 64 digit to an integer.
63 */
64base64.decode = function base64_decode(aChar) {
65 if (aChar in charToIntMap) {
66 return charToIntMap[aChar];
67 }
68 throw new TypeError("Not a valid base 64 digit: " + aChar);
69};
70
71
72
73// A single base 64 digit can contain 6 bits of data. For the base 64 variable
74// length quantities we use in the source map spec, the first bit is the sign,
75// the next four bits are the actual value, and the 6th bit is the
76// continuation bit. The continuation bit tells us whether there are more
77// digits in this value following this digit.
78//
79// Continuation
80// | Sign
81// | |
82// V V
83// 101011
84
85var VLQ_BASE_SHIFT = 5;
86
87// binary: 100000
88var VLQ_BASE = 1 << VLQ_BASE_SHIFT;
89
90// binary: 011111
91var VLQ_BASE_MASK = VLQ_BASE - 1;
92
93// binary: 100000
94var VLQ_CONTINUATION_BIT = VLQ_BASE;
95
96/**
97 * Converts from a two-complement value to a value where the sign bit is
98 * placed in the least significant bit. For example, as decimals:
99 * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
100 * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
101 */
102function toVLQSigned(aValue) {
103 return aValue < 0
104 ? ((-aValue) << 1) + 1
105 : (aValue << 1) + 0;
106}
107
108/**
109 * Converts to a two-complement value from a value where the sign bit is
110 * placed in the least significant bit. For example, as decimals:
111 * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1
112 * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2
113 */
114function fromVLQSigned(aValue) {
115 var isNegative = (aValue & 1) === 1;
116 var shifted = aValue >> 1;
117 return isNegative
118 ? -shifted
119 : shifted;
120}
121
122/**
123 * Returns the base 64 VLQ encoded value.
124 */
125exports.encode = function base64VLQ_encode(aValue) {
126 var encoded = "";
127 var digit;
128
129 var vlq = toVLQSigned(aValue);
130
131 do {
132 digit = vlq & VLQ_BASE_MASK;
133 vlq >>>= VLQ_BASE_SHIFT;
134 if (vlq > 0) {
135 // There are still more digits in this value, so we must make sure the
136 // continuation bit is marked.
137 digit |= VLQ_CONTINUATION_BIT;
138 }
139 encoded += base64.encode(digit);
140 } while (vlq > 0);
141
142 return encoded;
143};
144
145/**
146 * Decodes the next base 64 VLQ value from the given string and returns the
147 * value and the rest of the string via the out parameter.
148 */
149exports.decode = function base64VLQ_decode(aStr, aOutParam) {
150 var i = 0;
151 var strLen = aStr.length;
152 var result = 0;
153 var shift = 0;
154 var continuation, digit;
155
156 do {
157 if (i >= strLen) {
158 throw new Error("Expected more digits in base 64 VLQ value.");
159 }
160 digit = base64.decode(aStr.charAt(i++));
161 continuation = !!(digit & VLQ_CONTINUATION_BIT);
162 digit &= VLQ_BASE_MASK;
163 result = result + (digit << shift);
164 shift += VLQ_BASE_SHIFT;
165 } while (continuation);
166
167 aOutParam.value = fromVLQSigned(result);
168 aOutParam.rest = aStr.slice(i);
169};
Note: See TracBrowser for help on using the repository browser.