@syncfusion/ej2-pdf
Version:
Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.
101 lines (100 loc) • 3.3 kB
JavaScript
import { _getBigInt } from '../../utils';
/**
* Arbitrary-precision integer helper using decimal digit arrays for internal math.
*
* @private
*/
var _PdfBigInt = /** @class */ (function () {
function _PdfBigInt(decimalStr) {
if (decimalStr === void 0) { decimalStr = '0'; }
this.digits = this._parseDecimalString(decimalStr);
}
/**
* Parse a decimal string into an internal reversed-digit array.
*
* @private
* @param {string} str - Decimal string to parse.
* @returns {number[]} Reversed array of decimal digits.
*/
_PdfBigInt.prototype._parseDecimalString = function (str) {
return str.split('').reverse().map(function (d) { return parseInt(d, 10); });
};
/**
* Serialize the internal big integer to its decimal string representation.
*
* @private
* @returns {string} Decimal string representation of the integer.
*/
_PdfBigInt.prototype._toString = function () {
return this.digits.slice().reverse().join('').replace(/^0+/, '') || '0';
};
/**
* Convert the internal decimal representation to a JavaScript `bigint`.
*
* @private
* @returns {bigint} The numeric value as a `bigint`.
*/
_PdfBigInt.prototype._toBigInt = function () {
var str = this.digits.slice().reverse().join('').replace(/^0+/, '') || '0';
var bigIntConstructor = _getBigInt();
return bigIntConstructor(str);
};
/**
* Add a small integer value to this big integer (in-place).
*
* @private
* @param {number} n - Small integer to add.
* @returns {void}
*/
_PdfBigInt.prototype._add = function (n) {
var carry = n;
for (var i = 0; i < this.digits.length || carry > 0; i++) {
var sum = (this.digits[i] || 0) + carry;
this.digits[i] = sum % 10;
carry = Math.floor(sum / 10);
}
};
/**
* Multiply this big integer by 256 (in-place), used when decoding binary data.
*
* @private
* @returns {void}
*/
_PdfBigInt.prototype._multiply = function () {
var carry = 0;
for (var i = 0; i < this.digits.length; i++) {
var product = this.digits[i] * 256 + carry;
this.digits[i] = product % 10;
carry = Math.floor(product / 10);
}
while (carry > 0) {
this.digits.push(carry % 10);
carry = Math.floor(carry / 10);
}
};
/**
* Compute the bit-length of the integer value.
*
* @private
* @returns {number} Number of bits required to represent the value.
*/
_PdfBigInt.prototype._bitLength = function () {
var digits = this.digits.slice();
var bits = 0;
while (digits.length > 1 || digits[0] !== 0) {
var carry = 0;
for (var i = digits.length - 1; i >= 0; i--) {
var current = carry * 10 + digits[i];
digits[i] = Math.floor(current / 2);
carry = current % 2;
}
while (digits.length > 1 && digits[digits.length - 1] === 0) {
digits.pop();
}
bits++;
}
return bits;
};
return _PdfBigInt;
}());
export { _PdfBigInt };