MCPcopy Create free account
hub / github.com/chronoxor/FastBinaryEncoding / set

Method set

projects/JavaScript/proto/fbe.js:1785–1829  ·  view source on GitHub ↗

* Set the decimal value * @this {!FieldModelDecimal} * @param {!Big} value Value

(value)

Source from the content-addressed store, hash-verified

1783 * @param {!Big} value Value
1784 */
1785 set (value) {
1786 console.assert(((this._buffer.offset + this.fbeOffset + this.fbeSize) <= this._buffer.size), 'Model is broken!')
1787 if ((this._buffer.offset + this.fbeOffset + this.fbeSize) > this._buffer.size) {
1788 return
1789 }
1790
1791 // Extract decimal parts
1792 let negative = value.s < 0
1793 let scale = Math.max(0, value.c.length - 1 - value.e)
1794 let number = value.mul(Math.pow(10, scale)).abs()
1795
1796 // Check for decimal scale overflow
1797 if ((scale < 0) || (scale > 28)) {
1798 // Value scale exceeds .NET Decimal limit of [0, 28]
1799 this.writeCount(this.fbeOffset, 0, this.fbeSize)
1800 return
1801 }
1802
1803 // Write unscaled value to bytes 0-11
1804 let index = 0
1805 while (number > 0) {
1806 // Check for decimal number overflow
1807 if (index > 11) {
1808 // Value too big for .NET Decimal (bit length is limited to [0, 96])
1809 this.writeCount(this.fbeOffset, 0, this.fbeSize)
1810 return
1811 }
1812 let byte = parseInt(number.mod(256))
1813 this.writeByte(this.fbeOffset + index, byte)
1814 number = number.div(256).round(0, 0)
1815 index++
1816 }
1817
1818 // Fill remaining bytes with zeros
1819 while (index < 12) {
1820 this.writeByte(this.fbeOffset + index, 0)
1821 index++
1822 }
1823
1824 // Write scale at byte 14
1825 this.writeByte(this.fbeOffset + 14, scale)
1826
1827 // Write signum at byte 15
1828 this.writeByte(this.fbeOffset + 15, (negative ? 0x80 : 0))
1829 }
1830}
1831
1832exports.FieldModelDecimal = FieldModelDecimal

Callers 1

verifyDecimalFunction · 0.95

Calls 5

writeCountMethod · 0.80
writeByteMethod · 0.80
mulMethod · 0.45
modMethod · 0.45
divMethod · 0.45

Tested by

no test coverage detected