MCPcopy Create free account
hub / github.com/microsoft/SandDance / hexWrite

Function hexWrite

docs/tests/v2/es6/js/sanddance.js:79155–79180  ·  view source on GitHub ↗
(buf, string, offset, length)

Source from the content-addressed store, hash-verified

79153}
79154
79155function hexWrite (buf, string, offset, length) {
79156 offset = Number(offset) || 0
79157 var remaining = buf.length - offset
79158 if (!length) {
79159 length = remaining
79160 } else {
79161 length = Number(length)
79162 if (length > remaining) {
79163 length = remaining
79164 }
79165 }
79166
79167 // must be an even number of digits
79168 var strLen = string.length
79169 if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
79170
79171 if (length > strLen / 2) {
79172 length = strLen / 2
79173 }
79174 for (var i = 0; i < length; ++i) {
79175 var parsed = parseInt(string.substr(i * 2, 2), 16)
79176 if (isNaN(parsed)) return i
79177 buf[offset + i] = parsed
79178 }
79179 return i
79180}
79181
79182function utf8Write (buf, string, offset, length) {
79183 return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)

Callers 1

sanddance.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected