Skip to content

Commit 6e0d26f

Browse files
committed
FIX: trim on binary! should deal with NULL char instead of white-spaces like on string!
resolves: Oldes/Rebol-issues#1482
1 parent 9228635 commit 6e0d26f

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

src/boot/actions.reb

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ clear: action [
345345
]
346346

347347
trim: action [
348-
{Removes spaces from strings or nones from blocks or objects.}
348+
{Removes spaces from strings, nulls from binary, nones from blocks or objects.}
349349
series [series! object! error! module!] {Series (modified) or object (made)}
350350
/head {Removes only from the head}
351351
/tail {Removes only from the tail}

src/core/s-trim.c

+38
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,41 @@ static REBFLG find_in_uni(REBUNI *up, REBINT len, REBUNI c)
293293
trim_head_tail(ser, index, tail, flags & AM_TRIM_HEAD, flags & AM_TRIM_TAIL);
294294
}
295295
}
296+
297+
/***********************************************************************
298+
**
299+
*/ void Trim_Binary(REBSER *ser, REBCNT index, REBCNT len, REBCNT flags, REBVAL *with)
300+
/*
301+
***********************************************************************/
302+
{
303+
REBCNT tail = index + len;
304+
REBCNT n;
305+
306+
// /all or /with
307+
if (flags & (AM_TRIM_ALL | AM_TRIM_WITH)) {
308+
if (IS_NONE(with)) SET_INTEGER(with, 0); // only NULL by default on binary
309+
replace_with(ser, index, tail, with);
310+
return;
311+
}
312+
// /head
313+
if (!flags || flags & AM_TRIM_HEAD) {
314+
for (n = 0; n < len; n++) {
315+
if (BIN_HEAD(ser)[index + n]) break;
316+
}
317+
if (n > 0) {
318+
Remove_Series(ser, index, n);
319+
tail -= n;
320+
}
321+
}
322+
// /tail
323+
if (!flags || flags & AM_TRIM_TAIL) {
324+
for (; index < tail; tail--) {
325+
if (BIN_HEAD(ser)[tail - 1]) break;
326+
}
327+
SERIES_TAIL(ser) = tail;
328+
}
329+
// these are not supported on binary values
330+
if (flags & (AM_TRIM_AUTO | AM_TRIM_LINES)) {
331+
Trap0(RE_BAD_REFINES);
332+
}
333+
}

src/core/t-string.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,10 @@ static struct {
794794
(args & (AM_TRIM_HEAD | AM_TRIM_TAIL | AM_TRIM_LINES | AM_TRIM_ALL | AM_TRIM_WITH)))
795795
)
796796
Trap0(RE_BAD_REFINES);
797-
798-
Trim_String(VAL_SERIES(value), VAL_INDEX(value), VAL_LEN(value), args, D_ARG(ARG_TRIM_STR));
797+
if (IS_BINARY(value))
798+
Trim_Binary(VAL_SERIES(value), VAL_INDEX(value), VAL_LEN(value), args, D_ARG(ARG_TRIM_STR));
799+
else
800+
Trim_String(VAL_SERIES(value), VAL_INDEX(value), VAL_LEN(value), args, D_ARG(ARG_TRIM_STR));
799801
break;
800802

801803
case A_SWAP:

src/tests/units/series-test.r3

+36
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,42 @@ Rebol [
280280
===end-group===
281281

282282

283+
===start-group=== "TRIM"
284+
--test-- "trim string!"
285+
str1: " a b c "
286+
str2: " ^(A0) ^-a b ^- c ^(2000) "
287+
mstr: { a ^-1^/ ab2^- ^/ ac3 ^/ ^/^/}
288+
--assert "a b c" = trim copy str1
289+
--assert "a b c" = trim/head/tail copy str1
290+
--assert "a b c " = trim/head copy str1
291+
--assert " a b c" = trim/tail copy str1
292+
; --assert "a b ^- c" = trim copy str2 ;- not like Red!
293+
--assert "a ^-1^/ab2^/ac3^/" = trim copy mstr
294+
--assert "a1ab2ac3" = trim/all { a ^-1^/ ab2^- ^/ ac3 ^/ ^/^/}
295+
--assert " ^-1^/ b2^- ^/ c3 ^/ ^/^/" = trim/with copy mstr #"a"
296+
--assert " ^-1^/ b2^- ^/ c3 ^/ ^/^/" = trim/with copy mstr 97
297+
--test-- "trim binary!"
298+
;@@ https://github.com/Oldes/Rebol-issues/issues/1482
299+
bin: #{0011001100}
300+
--assert #{110011} = trim copy bin
301+
--assert #{110011} = trim/head/tail copy bin
302+
--assert #{11001100} = trim/head copy bin
303+
--assert #{00110011} = trim/tail copy bin
304+
--assert #{1111} = trim/all copy bin
305+
--assert #{000000} = trim/all/with copy bin #{11}
306+
--assert #{} = trim #{0000}
307+
--assert #{} = trim/tail #{0000}
308+
--assert #{} = trim/head #{0000}
309+
--test-- "trim binary! with index > 1"
310+
bin: #{0000110000}
311+
--assert #{00001100} = head trim/tail at copy bin 5
312+
--assert #{00110000} = head trim/head at copy bin 2
313+
--assert #{0011} = head trim/all at copy bin 2
314+
315+
===end-group===
316+
317+
318+
283319
===start-group=== "REPLACE string!"
284320
;@@ https://github.com/Oldes/Rebol-issues/issues/54
285321
--test-- "issue-54"

0 commit comments

Comments
 (0)