Skip to content

Commit 6250441

Browse files
committed
FIX: insert/append/change binary! string!
fixes: Oldes/Rebol-issues#1791
1 parent 695efd4 commit 6250441

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/core/f-modify.c

+18-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@
125125
if (dups < 0) return (action == A_APPEND) ? 0 : dst_idx;
126126
if (action == A_APPEND || dst_idx > tail) dst_idx = tail;
127127

128-
// If the src_val is not a string, then we need to create a string:
128+
// If the src_val is not same type like trg_val, we must convert it
129129
if (GET_FLAG(flags, AN_SERIES)) { // used to indicate a BINARY series
130-
if (IS_INTEGER(src_val)) {
130+
if (IS_BINARY(src_val)) {
131+
// use as it is
132+
}
133+
else if (IS_INTEGER(src_val)) {
131134
src_ser = Append_Byte(0, Int8u(src_val)); // creates a binary
132135
}
133136
else if (IS_BLOCK(src_val)) {
@@ -137,7 +140,19 @@
137140
src_ser = Make_Binary(6); // (I hate unicode)
138141
src_ser->tail = Encode_UTF8_Char(BIN_HEAD(src_ser), VAL_CHAR(src_val));
139142
}
140-
else if (!ANY_BINSTR(src_val)) Trap_Arg(src_val);
143+
else if (ANY_STR(src_val)) {
144+
// here is used temporary src_len, used to limit conversion of the string to binary
145+
// If /part is used, not complete src is converted to binary (UTF8).
146+
// This src_len is modified by purpose later so the result may be shorter.
147+
// Like in this case: #{E1} == append/part #{} "^(1234)" 1
148+
if (action != A_CHANGE && GET_FLAG(flags, AN_PART)) {
149+
src_len = dst_len;
150+
} else {
151+
src_len = VAL_LEN(src_val);
152+
}
153+
src_ser = Encode_UTF8_Value(src_val, src_len, FALSE); // NOTE: uses shared FORM buffer!
154+
}
155+
else Trap_Arg(src_val);
141156
}
142157
else if (IS_CHAR(src_val)) {
143158
src_ser = Append_Byte(0, VAL_CHAR(src_val)); // unicode ok too

src/core/s-unicode.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,7 @@ ConversionResult ConvertUTF8toUTF32 (
11891189

11901190
size = Length_As_UTF8(up, len, TRUE, (REBOOL)ccr);
11911191
cp = Reset_Buffer(ser, size + (GET_FLAG(opts, ENC_OPT_BOM) ? 3 : 0));
1192-
UNUSED(cp);
1193-
Encode_UTF8(Reset_Buffer(ser, size), size, up, &len, TRUE, ccr);
1192+
Encode_UTF8(cp, size, up, &len, TRUE, ccr);
11941193
}
11951194
else {
11961195
REBYTE *bp = (REBYTE*)src;

src/tests/units/series-test.r3

+51
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,57 @@ Rebol [
173173

174174
===end-group===
175175

176+
;@@ https://github.com/Oldes/Rebol-issues/issues/1791
177+
===start-group=== "APPEND binary!"
178+
--test-- "APPEND binary! binary!"
179+
--assert #{0102} = append #{01} #{02}
180+
--assert #{0102} = append next #{01} #{02}
181+
--test-- "APPEND binary! string!"
182+
--assert #{0001} = append #{00} "^(01)"
183+
--assert #{0001} = append next #{00} "^(01)"
184+
--assert #{00E28690} = append #{00} "^(2190)"
185+
--assert #{00E28690} = append next #{00} "^(2190)"
186+
--test-- "APPEND binary! file!"
187+
--assert #{616263} = append #{} %abc
188+
--assert #{C3A162} = append #{} %áb
189+
--test-- "APPEND binary! char!"
190+
--assert #{0001} = append #{00} #"^(01)"
191+
--assert #{00E28690} = append #{00} #"^(2190)"
192+
--test-- "APPEND/part binary!"
193+
--assert #{01} = append/part #{} #{0102} 1
194+
--assert #{01} = append/part #{} "^(01)^(02)" 1
195+
--assert #{E2} = append/part #{} "^(2190)" 1 ;-- by design!
196+
===end-group===
197+
198+
===start-group=== "INSERT binary!"
199+
--test-- "INSERT binary! binary!"
200+
--assert #{0201} = head insert #{01} #{02}
201+
--assert #{0102} = head insert next #{01} #{02}
202+
--test-- "INSERT binary! string!"
203+
--assert #{0100} = head insert #{00} "^(01)"
204+
--assert #{0001} = head insert next #{00} "^(01)"
205+
--assert #{E2869000} = head insert #{00} "^(2190)"
206+
--assert #{00E28690} = head insert next #{00} "^(2190)"
207+
--test-- "INSERT binary! file!"
208+
--assert #{61626300} = head insert #{00} %abc
209+
--assert #{C3A16200} = head insert #{00} %áb
210+
--test-- "INSERT binary! char!"
211+
--assert #{0100} = head insert #{00} #"^(01)"
212+
--assert #{E2869000} = head insert #{00} #"^(2190)"
213+
--test-- "INSERT/part binary!"
214+
--assert #{0100} = head insert/part #{00} #{0102} 1
215+
--assert #{0100} = head insert/part #{00} "^(01)^(02)" 1
216+
--assert #{E200} = head insert/part #{00} "^(2190)" 1 ;-- by design!
217+
===end-group===
218+
219+
===start-group=== "CHANGE binary!"
220+
--test-- "CHANGE binary! string!"
221+
--assert #{E188B4} = head change #{} "^(1234)"
222+
--assert #{E188B4} = head change #{00} "^(1234)"
223+
--assert #{E188B4} = head change #{0000} "^(1234)"
224+
--assert #{E188B403} = head change/part #{010203} "^(1234)" 2
225+
===end-group===
226+
176227
===start-group=== "TAKE"
177228
--test-- "take string!"
178229
s: "a"

0 commit comments

Comments
 (0)