Skip to content

Commit 783309e

Browse files
committed
FEAT: new truncate native for shortening a series
1 parent c518768 commit 783309e

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

src/core/n-data.c

+30
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,36 @@ static int Do_Ordinal(REBVAL *ds, REBINT n)
10231023
}
10241024

10251025

1026+
/***********************************************************************
1027+
**
1028+
*/ REBNATIVE(truncate)
1029+
/*
1030+
// truncate: native [
1031+
// "Removes all bytes/values from series' head to its current index position"
1032+
// series [series!] "Series to be truncated"
1033+
// /part "Also shorten resulted series to a length or end position"
1034+
// range [number! series!]
1035+
// ]
1036+
***********************************************************************/
1037+
{
1038+
REBVAL *arg = D_ARG(1);
1039+
REBINT index = VAL_INDEX(arg);
1040+
REBINT len = -1;
1041+
1042+
if (D_REF(2)) {
1043+
len = Partial(arg, 0, D_ARG(3), 0);
1044+
}
1045+
if (index > 0) {
1046+
Remove_Series(VAL_SERIES(arg), 0, index);
1047+
VAL_INDEX(arg) = 0;
1048+
}
1049+
if (len >= 0) {
1050+
VAL_TAIL(arg) = len;
1051+
}
1052+
return R_ARG1;
1053+
}
1054+
1055+
10261056
#ifdef not_fast_enough
10271057
/***********************************************************************
10281058
**

src/modules/httpd.reb

+5-5
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ sys/make-scheme [
736736
len: data/2 & 127
737737
data: skip data 2
738738
;? final? ? opcode ? len
739+
740+
;@@ Not removing bytes until we make sure, that there is enough data!
739741
case [
740742
len = 126 [
741743
if 2 >= length? data [break]
@@ -749,8 +751,7 @@ sys/make-scheme [
749751
]
750752
]
751753
if (4 + length? data) < len [break]
752-
remove/part head data data
753-
data: head data
754+
data: truncate data ;; removes already processed bytes from the head
754755
either mask? [
755756
request-data: make binary! len
756757
masks: take/part data 4
@@ -761,9 +762,8 @@ sys/make-scheme [
761762
][
762763
request-data: take/part data len
763764
]
764-
ready?: true
765-
clear skip request-data len
766-
ctx/inp/content: request-data
765+
ready?: true
766+
ctx/inp/content: truncate/part request-data len
767767
if opcode = 8 [
768768
sys/log/more 'HTTPD "WS Connection Close Frame!"
769769
code: 0

src/tests/units/series-test.r3

+13
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,19 @@ Rebol [
392392
===end-group===
393393

394394

395+
===start-group=== "TRUNCATE"
396+
--test-- "TRUNCATE"
397+
--assert "23" = truncate next "123"
398+
--assert [2 3] = truncate next [1 2 3]
399+
--assert "2" = truncate/part next "123" 1
400+
--assert [2] = truncate/part next [1 2 3] 1
401+
--assert "23" = head truncate next "123"
402+
--assert [2 3] = head truncate next [1 2 3]
403+
--assert "2" = head truncate/part next "123" 1
404+
--assert [2] = head truncate/part next [1 2 3] 1
405+
===end-group===
406+
407+
395408

396409
===start-group=== "REPLACE string!"
397410
;@@ https://github.com/Oldes/Rebol-issues/issues/54

0 commit comments

Comments
 (0)