@arithmetic-operations-for/naturals-big-endian Home Manual Reference Source

src/core/convert/trim_natural.js

  1. import _alloc from '../array/_alloc.js';
  2. import _copy from '../array/_copy.js';
  3. import _trim_positive from './_trim_positive.js';
  4.  
  5. /**
  6. * Trim a limb array so that it is either [0] or does not start with any
  7. * leading zeros. Return a newly allocated array and does not modify the input.
  8. *
  9. * @param {number[]} a The input limb array.
  10. * @param {number} ai
  11. * @param {number} aj
  12. *
  13. * @return {number[]} The input but trimmed.
  14. */
  15. export default function trim_natural(a, ai, aj) {
  16. const x = _trim_positive(a, ai, aj);
  17.  
  18. if (x >= aj) return [0];
  19.  
  20. const b = _alloc(aj - x);
  21.  
  22. _copy(a, x, aj, b, 0);
  23.  
  24. return b;
  25. }