Skip to content

Commit ae88f04

Browse files
committed
FEAT: enhanced DEHEX with /escape refinement for changing the escape character
Can be used as: ``` >> dehex/escape "a#20b" #"#" == "a b" ```
1 parent 95b4541 commit ae88f04

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/boot/natives.r

+1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ uppercase: native [
531531
dehex: native [
532532
{Converts URL-style hex encoded (%xx) strings. If input is UTF-8 encode, you should first convert it to binary!}
533533
value [any-string! binary!] {The string to dehex}
534+
/escape char [char!] {Can be used to change the default escape char #"%"}
534535
]
535536

536537
enhex: native [

src/core/n-strings.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -560,17 +560,21 @@ static struct digest {
560560
**
561561
***********************************************************************/
562562
{
563-
REBVAL *arg = D_ARG(1);
563+
REBVAL *arg = D_ARG(1);
564+
// REBOOL ref_escape = D_REF(2);
565+
REBVAL *val_escape = D_ARG(3);
564566
REBINT len = (REBINT)VAL_LEN(arg); // due to len -= 2 below
565567
REBUNI n;
566568
REBSER *ser;
567569

570+
const REBCHR escape_char = (IS_CHAR(val_escape)) ? VAL_CHAR(val_escape) : '%';
571+
568572
if (VAL_BYTE_SIZE(arg)) {
569573
REBYTE *bp = VAL_BIN_DATA(arg);
570574
REBYTE *dp = Reset_Buffer(BUF_FORM, len);
571575

572576
for (; len > 0; len--) {
573-
if (*bp == '%' && len > 2 && Scan_Hex2(bp+1, &n, FALSE)) {
577+
if (*bp == escape_char && len > 2 && Scan_Hex2(bp+1, &n, FALSE)) {
574578
*dp++ = (REBYTE)n;
575579
bp += 3;
576580
len -= 2;
@@ -586,7 +590,7 @@ static struct digest {
586590
REBUNI *dp = (REBUNI*)Reset_Buffer(BUF_MOLD, len);
587591

588592
for (; len > 0; len--) {
589-
if (*up == '%' && len > 2 && Scan_Hex2((REBYTE*)(up+1), &n, TRUE)) {
593+
if (*up == escape_char && len > 2 && Scan_Hex2((REBYTE*)(up+1), &n, TRUE)) {
590594
*dp++ = (REBUNI)n;
591595
up += 3;
592596
len -= 2;

src/tests/units/series-test.r3

+4
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,10 @@ Rebol [
644644
--assert "%C2%81" = enhex to-string #{81}
645645
--assert "%E5%85%83" = enhex {元}
646646

647+
--test-- "DEHEX/escape"
648+
--assert "C# #XX" = dehex/escape "C#23#20#XX" #"#"
649+
--assert "(š)" = dehex/escape "#28š#29" #"#"
650+
647651

648652
===end-group===
649653

0 commit comments

Comments
 (0)