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

Function fxEncodeURI

xs/sources/xsGlobal.c:572–642  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

570}
571
572void fxEncodeURI(txMachine* the, txString theSet)
573{
574 txString src;
575 txInteger length;
576 txInteger c;
577 txString dst;
578
579 src = fxToString(the, mxArgv(0));
580 length = 0;
581 while (((src = mxStringByteDecode(src, &c))) && (c != C_EOF)) {
582 if (c < 0x80) {
583 if (c_read8(theSet + c))
584 length += 1;
585 else
586 length += 3;
587 }
588 else if (c < 0x800)
589 length += 6;
590 else if ((0xD800 <= c) && (c <= 0xDFFF))
591 mxURIError("invalid string");
592 else if (c < 0x10000)
593 length += 9;
594 else
595 length += 12;
596 }
597 length += 1;
598 if (length == (src - mxArgv(0)->value.string)) {
599 mxResult->value.string = mxArgv(0)->value.string;
600 mxResult->kind = mxArgv(0)->kind;
601 return;
602 }
603 mxResult->value.string = fxNewChunk(the, length);
604 mxResult->kind = XS_STRING_KIND;
605 src = mxArgv(0)->value.string;
606 dst = mxResult->value.string;
607 while (((src = mxStringByteDecode(src, &c))) && (c != C_EOF)) {
608 if (c < 0x80) {
609 if (c_read8(theSet + c))
610 *dst++ = (char)c;
611 else {
612 *dst++ = '%';
613 dst = fxStringifyHexEscape(dst, c);
614 }
615 }
616 else if (c < 0x800) {
617 *dst++ = '%';
618 dst = fxStringifyHexEscape(dst, 0xC0 | (c >> 6));
619 *dst++ = '%';
620 dst = fxStringifyHexEscape(dst, 0x80 | (c & 0x3F));
621 }
622 else if (c < 0x10000) {
623 *dst++ = '%';
624 dst = fxStringifyHexEscape(dst, 0xE0 | (c >> 12));
625 *dst++ = '%';
626 dst = fxStringifyHexEscape(dst, 0x80 | ((c >> 6) & 0x3F));
627 *dst++ = '%';
628 dst = fxStringifyHexEscape(dst, 0x80 | (c & 0x3F));
629 }

Callers 2

fx_encodeURIFunction · 0.85
fx_encodeURIComponentFunction · 0.85

Calls 3

fxToStringFunction · 0.85
fxNewChunkFunction · 0.85
fxStringifyHexEscapeFunction · 0.85

Tested by

no test coverage detected