Skip to content

Commit 5bbac32

Browse files
committed
FIX: trim on block! removing nones only from head and tail, trim/all everywhere
resolves: Oldes/Rebol-issues#2482
1 parent 3da722a commit 5bbac32

File tree

4 files changed

+47
-40
lines changed

4 files changed

+47
-40
lines changed

src/core/s-trim.c

+40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6+
** Copyright 2012-2022 Rebol Open Source Developers
67
** REBOL is a trademark of REBOL Technologies
78
**
89
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -331,3 +332,42 @@ static REBFLG find_in_uni(REBUNI *up, REBINT len, REBUNI c)
331332
Trap0(RE_BAD_REFINES);
332333
}
333334
}
335+
336+
/***********************************************************************
337+
**
338+
*/ void Trim_Block(REBSER *ser, REBCNT index, REBCNT flags)
339+
/*
340+
***********************************************************************/
341+
{
342+
REBVAL *blk = BLK_HEAD(ser);
343+
REBCNT out = index;
344+
REBCNT end = ser->tail;
345+
346+
if (flags & AM_TRIM_ALL) {
347+
if (flags != (flags & AM_TRIM_ALL)) Trap0(RE_BAD_REFINES);
348+
for (; index < end; index++) {
349+
if (VAL_TYPE(blk + index) > REB_NONE) {
350+
*BLK_SKIP(ser, out) = blk[index];
351+
out++;
352+
}
353+
}
354+
Remove_Series(ser, out, end - out);
355+
return;
356+
}
357+
358+
if (flags & ~(AM_TRIM_HEAD | AM_TRIM_TAIL)) Trap0(RE_BAD_REFINES);
359+
360+
if (!flags || flags & AM_TRIM_TAIL) {
361+
for (; end >= (index + 1); end--) {
362+
if (VAL_TYPE(blk + end - 1) > REB_NONE) break;
363+
}
364+
Remove_Series(ser, end, ser->tail - end);
365+
}
366+
367+
if (!flags || flags & AM_TRIM_HEAD) {
368+
for (; index < end; index++) {
369+
if (VAL_TYPE(blk + index) > REB_NONE) break;
370+
}
371+
Remove_Series(ser, out, index - out);
372+
}
373+
}

src/core/t-block.c

+1-40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6+
** Copyright 2012-2022 Rebol Open Source Developers
67
** REBOL is a trademark of REBOL Technologies
78
**
89
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -505,45 +506,6 @@ static struct {
505506
}
506507

507508

508-
/***********************************************************************
509-
**
510-
*/ static void Trim_Block(REBSER *ser, REBCNT index, REBCNT flags)
511-
/*
512-
** See Trim_String().
513-
**
514-
***********************************************************************/
515-
{
516-
REBVAL *blk = BLK_HEAD(ser);
517-
REBCNT out = index;
518-
REBCNT end = ser->tail;
519-
520-
if (flags & AM_TRIM_TAIL) {
521-
for (; end >= (index+1); end--) {
522-
if (VAL_TYPE(blk+end-1) > REB_NONE) break;
523-
}
524-
Remove_Series(ser, end, ser->tail - end);
525-
if (!(flags & AM_TRIM_HEAD) || index >= end) return;
526-
}
527-
528-
if (flags & AM_TRIM_HEAD) {
529-
for (; index < end; index++) {
530-
if (VAL_TYPE(blk+index) > REB_NONE) break;
531-
}
532-
Remove_Series(ser, out, index - out);
533-
}
534-
535-
if (flags == 0) {
536-
for (; index < end; index++) {
537-
if (VAL_TYPE(blk+index) > REB_NONE) {
538-
*BLK_SKIP(ser, out) = blk[index];
539-
out++;
540-
}
541-
}
542-
Remove_Series(ser, out, end - out);
543-
}
544-
}
545-
546-
547509
/***********************************************************************
548510
**
549511
*/ void Shuffle_Block(REBVAL *value, REBFLG secure)
@@ -865,7 +827,6 @@ static struct {
865827

866828
case A_TRIM:
867829
args = Find_Refines(ds, ALL_TRIM_REFS);
868-
if (args & ~(AM_TRIM_HEAD|AM_TRIM_TAIL)) Trap0(RE_BAD_REFINES);
869830
Trim_Block(ser, index, args);
870831
break;
871832

src/core/t-string.c

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6+
** Copyright 2012-2022 Rebol Open Source Developers
67
** REBOL is a trademark of REBOL Technologies
78
**
89
** Licensed under the Apache License, Version 2.0 (the "License");

src/tests/units/series-test.r3

+5
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ Rebol [
324324
;@@ https://github.com/Oldes/Rebol-issues/issues/825
325325
--assert [1 #[none] 2 #[none]] = trim/head copy blk
326326
--assert [#[none] 1 #[none] 2] = trim/tail copy blk
327+
;@@ https://github.com/Oldes/Rebol-issues/issues/2482
328+
--assert [1 #[none] 2] = trim copy blk
329+
--assert [1 2] = trim/all copy blk
330+
--assert all [error? e: try [trim/head/all []] e/id = 'bad-refines]
331+
--assert all [error? e: try [trim/tail/all []] e/id = 'bad-refines]
327332

328333
===end-group===
329334

0 commit comments

Comments
 (0)