Skip to content

Commit 0976daf

Browse files
committed
FIX: formed/molded output with ASCII chars using always a special char escaping
resolves: Oldes/Rebol-issues#2574
1 parent e1cc08c commit 0976daf

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

src/core/s-mold.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,16 @@ STOID Sniff_String(REBSER *ser, REBCNT idx, REB_STRF *sf)
317317
else if (IS_CHR_ESC(c)) sf->escape++;
318318
else if (c >= 0x1000) sf->paren += 6; // ^(1234)
319319
else if (c >= 0x100) sf->paren += 5; // ^(123)
320-
else if (c >= 0x80) sf->paren += 4; // ^(12)
320+
else if (c >= 0x7f) sf->paren += 4; // ^(12)
321321
}
322322
}
323323
if (sf->brace_in != sf->brace_out) sf->malign++;
324324
}
325325

326-
static REBUNI *Emit_Uni_Char(REBUNI *up, REBUNI chr, REBOOL parened)
326+
static REBUNI *Emit_Uni_Char(REBUNI *up, REBUNI chr)
327327
{
328328
if (chr >= 0x7f || chr == 0x1e) { // non ASCII or ^ must be (00) escaped
329-
if (parened || chr < 0xA0 || chr == 0x1e) { // do not AND with above
329+
if (chr < 0xA0 || chr == 0x1e) { // do not AND with above
330330
*up++ = '^';
331331
*up++ = '(';
332332
up = Form_Uni_Hex(up, chr);
@@ -344,7 +344,7 @@ static REBUNI *Emit_Uni_Char(REBUNI *up, REBUNI chr, REBOOL parened)
344344
return up;
345345
}
346346

347-
STOID Mold_Uni_Char(REBSER *dst, REBUNI chr, REBOOL molded, REBOOL parened)
347+
STOID Mold_Uni_Char(REBSER *dst, REBUNI chr, REBOOL molded)
348348
{
349349
REBCNT tail = SERIES_TAIL(dst);
350350
REBUNI *up;
@@ -358,7 +358,7 @@ STOID Mold_Uni_Char(REBSER *dst, REBUNI chr, REBOOL molded, REBOOL parened)
358358
up = UNI_SKIP(dst, tail);
359359
*up++ = '#';
360360
*up++ = '"';
361-
up = Emit_Uni_Char(up, chr, parened);
361+
up = Emit_Uni_Char(up, chr);
362362
*up++ = '"';
363363
dst->tail = up - UNI_HEAD(dst);
364364
}
@@ -387,7 +387,6 @@ STOID Mold_String_Series(REBVAL *value, REB_MOLD *mold)
387387
CHECK_MOLD_LIMIT(mold, len);
388388

389389
Sniff_String(ser, idx, &sf);
390-
if (!GET_MOPT(mold, MOPT_ANSI_ONLY)) sf.paren = 0;
391390

392391
// Source can be 8 or 16 bits:
393392
if (uni) up = UNI_HEAD(ser);
@@ -402,7 +401,7 @@ STOID Mold_String_Series(REBVAL *value, REB_MOLD *mold)
402401

403402
for (n = idx; n < VAL_TAIL(value); n++) {
404403
c = uni ? up[n] : (REBUNI)(bp[n]);
405-
dp = Emit_Uni_Char(dp, c, (REBOOL)GET_MOPT(mold, MOPT_ANSI_ONLY)); // parened
404+
dp = Emit_Uni_Char(dp, c);
406405
}
407406

408407
*dp++ = '"';
@@ -433,7 +432,7 @@ STOID Mold_String_Series(REBVAL *value, REB_MOLD *mold)
433432
*dp++ = c;
434433
break;
435434
default:
436-
dp = Emit_Uni_Char(dp, c, (REBOOL)GET_MOPT(mold, MOPT_ANSI_ONLY)); // parened
435+
dp = Emit_Uni_Char(dp, c);
437436
}
438437
}
439438

@@ -1184,7 +1183,7 @@ STOID Mold_Error(REBVAL *value, REB_MOLD *mold, REBFLG molded)
11841183
goto append;
11851184

11861185
case REB_CHAR:
1187-
Mold_Uni_Char(ser, VAL_CHAR(value), (REBOOL)molded, (REBOOL)GET_MOPT(mold, MOPT_MOLD_ALL));
1186+
Mold_Uni_Char(ser, VAL_CHAR(value), (REBOOL)molded);
11881187
break;
11891188

11901189
case REB_PAIR:

src/include/sys-core.h

-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ enum REB_Mold_Opts {
211211
#define DEC_MOLD_PERCENT 1 // follow num with %
212212
#define DEC_MOLD_MINIMAL 2 // allow decimal to be integer
213213

214-
// Temporary:
215-
#define MOPT_ANSI_ONLY MOPT_MOLD_ALL // Non ANSI chars are ^() escaped
216214

217215
// Reflector words (words-of, body-of, etc.)
218216
enum Reb_Reflectors {

src/tests/units/mold-test.r3

+30
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,36 @@ Rebol [
176176
--assert "" = mold/part "abcd" -2
177177
--assert {"abcd"} = mold/part "abcd" 10
178178

179+
--test-- "mold/form string with a special chars"
180+
;@@ https://github.com/Oldes/Rebol-issues/issues/2574
181+
s: "ab"
182+
for i 127 159 1 [
183+
s/1: to char! i
184+
--assert all [
185+
2 = length? s
186+
i = to integer! s/1
187+
s/2 = #"b"
188+
8 = length? v: mold s
189+
v/2 = #"^^"
190+
v/3 = #"("
191+
]
192+
--assert all [
193+
2 = length? v: form s
194+
i = to integer! v/1
195+
v/2 = #"b"
196+
]
197+
--assert all [
198+
8 = length? v: mold/all s
199+
v/2 = #"^^"
200+
v/3 = #"("
201+
]
202+
--assert all [
203+
s = try [load save %tmp2574 s]
204+
]
205+
]
206+
try [delete %tmp2574]
207+
208+
179209
===end-group===
180210

181211
===start-group=== "mold url!"

0 commit comments

Comments
 (0)