(bytes: Uint8Array, offset: number, size: number)
| 719 | } |
| 720 | |
| 721 | const decodeNumeric = (bytes: Uint8Array, offset: number, size: number): string => { |
| 722 | if (size < 8) return fail("Truncated numeric value") |
| 723 | const count = readInt16(bytes, offset) |
| 724 | const weight = readInt16(bytes, offset + 2) |
| 725 | const sign = readUint16(bytes, offset + 4) |
| 726 | const scale = readInt16(bytes, offset + 6) |
| 727 | if (sign === NUMERIC_NAN) return "NaN" |
| 728 | if (sign === NUMERIC_PINF) return "Infinity" |
| 729 | if (sign === NUMERIC_NINF) return "-Infinity" |
| 730 | if (sign !== NUMERIC_POS && sign !== NUMERIC_NEG) { |
| 731 | return fail(`Invalid numeric sign: ${sign}`) |
| 732 | } |
| 733 | requireSize(size, 8 + count * 2, "numeric") |
| 734 | |
| 735 | const digits = offset + 8 |
| 736 | // Groups outside 0-9999 have no four-digit text, and reading past the ones |
| 737 | // the value carries is how the fractional part is padded out. |
| 738 | const digitAt = (index: number): number => { |
| 739 | if (index < 0 || index >= count) return 0 |
| 740 | const digit = readInt16(bytes, digits + index * 2) |
| 741 | return digit >= 0 && digit <= 9999 ? digit : fail(`Invalid numeric digit: ${digit}`) |
| 742 | } |
| 743 | |
| 744 | let result = sign === NUMERIC_NEG ? "-" : "" |
| 745 | if (weight < 0) { |
| 746 | result += "0" |
| 747 | } else { |
| 748 | for (let i = 0; i <= weight; i++) { |
| 749 | const digit = digitAt(i) |
| 750 | if (i === 0) { |
| 751 | result += String(digit) |
| 752 | } else { |
| 753 | result += twoDigits[(digit / 100) | 0] |
| 754 | result += twoDigits[digit % 100] |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | if (scale > 0) { |
| 759 | let fraction = "" |
| 760 | for (let i = weight + 1; fraction.length < scale; i++) { |
| 761 | const digit = digitAt(i) |
| 762 | fraction += twoDigits[(digit / 100) | 0] |
| 763 | fraction += twoDigits[digit % 100] |
| 764 | } |
| 765 | result += `.${fraction.slice(0, scale)}` |
| 766 | } |
| 767 | return result |
| 768 | } |
| 769 | |
| 770 | // ----------------------------------------------------------------------------- |
| 771 | // network addresses |
nothing calls this directly
no test coverage detected