MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / fx_String_fromArrayBuffer

Function fx_String_fromArrayBuffer

xs/sources/xsString.c:577–667  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

575
576#ifndef mxCESU8
577void fx_String_fromArrayBuffer(txMachine* the)
578{
579 txSlot* slot;
580 txSlot* arrayBuffer = C_NULL, *sharedArrayBuffer = C_NULL;
581 txSlot* bufferInfo;
582 txInteger limit, offset;
583 txInteger inLength, outLength = 0, nulls = 0;
584 unsigned char *in;
585 txString string;
586 if (mxArgc < 1)
587 mxTypeError("no argument");
588 slot = mxArgv(0);
589 if (slot->kind == XS_REFERENCE_KIND) {
590 slot = slot->value.reference->next;
591 if (slot) {
592 bufferInfo = slot->next;
593 if (slot->kind == XS_ARRAY_BUFFER_KIND)
594 arrayBuffer = slot;
595 else if (slot->kind == XS_HOST_KIND) {
596 if (!(slot->flag & XS_HOST_CHUNK_FLAG) && bufferInfo && (bufferInfo->kind == XS_BUFFER_INFO_KIND))
597 sharedArrayBuffer = slot;
598 }
599 }
600 }
601 if (!arrayBuffer && !sharedArrayBuffer)
602 mxTypeError("argument: not an ArrayBuffer instance");
603 limit = bufferInfo->value.bufferInfo.length;
604 offset = fxArgToByteLength(the, 1, 0);
605 if (limit < offset)
606 mxRangeError("invalid byteOffset %ld", offset);
607 inLength = fxArgToByteLength(the, 2, limit - offset);
608 if ((limit < (offset + inLength)) || ((offset + inLength) < offset))
609 mxRangeError("invalid byteLength %ld", inLength);
610
611 in = offset + (unsigned char *)(arrayBuffer ? arrayBuffer->value.arrayBuffer.address : sharedArrayBuffer->value.host.data);
612 while (inLength > 0) {
613 unsigned char first = c_read8(in++), clen;
614 if (first < 0x80){
615 if (0 == first)
616 nulls += 1;
617 inLength -= 1;
618 outLength += 1;
619 continue;
620 }
621
622 if (0xC0 == (first & 0xE0))
623 clen = 2;
624 else if (0xE0 == (first & 0xF0))
625 clen = 3;
626 else if (0xF0 == (first & 0xF0))
627 clen = 4;
628 else
629 goto badUTF8;
630
631 inLength -= clen;
632 if (inLength < 0)
633 goto badUTF8;
634

Callers

nothing calls this directly

Calls 2

fxArgToByteLengthFunction · 0.85
fxNewChunkFunction · 0.85

Tested by

no test coverage detected