-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathskip
executable file
·240 lines (197 loc) · 6.54 KB
/
skip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env perl
# Copyright (c) 2015,2020 Christian Jaeger, [email protected]
# This is free software. See the file COPYING.md that came bundled
# with this file.
use strict;
use warnings;
use warnings FATAL => 'uninitialized';
# find modules from functional-perl working directory (not installed)
use Cwd 'abs_path';
our ($mydir, $myname);
BEGIN {
my $location = (-l $0) ? abs_path($0) : $0;
$location =~ /(.*?)([^\/]+?)_?\z/s or die "?";
($mydir, $myname) = ($1, $2);
}
use lib "$mydir/../lib";
use Getopt::Long;
use FP::Predicates 'is_natural0';
use Chj::xopen 'glob_to_fh';
use FP::IOStream 'fh_to_chunks';
use FP::Stream ":all";
use FP::List ":all";
use FP::Lazy ":all";
use POSIX 'ESPIPE';
use Chj::TEST;
use FP::Carp;
our $BUFSIZ = 1024 * 16;
sub usage {
print STDERR map {"$_\n"} @_ if @_;
print "$myname n [m]
Skip n bytes of input, output the rest. If m is given, don't output
the last m bytes. Example: `echo -n 'Hi World' | skip 3 2` -> 'Wor'
--bufsize n use n instead of default $BUFSIZ as buffer size
--silent don't give error if the input is too short
";
exit(@_ ? 1 : 0);
}
sub resource_rss {
@_ == 0 or fp_croak_arity 0;
require BSD::Resource;
(BSD::Resource::getrusage(BSD::Resource::RUSAGE_SELF()))[2]
}
# ---- sliding buffer ----------------------------------------
# Handle a sliding buffer window to make sure we don't output the
# input too soon (so that when we hit EOF, we've got at least m bytes
# still buffered). Do this by using a lazy list of chunks as the input
# and keep two pointers into it as the window.
# `chunks_change_tail` returns a stream of the unmodified chunks until
# passing through one more would make the sum of the remaining chunks
# till the end of the stream smaller than $minsize; pass the remainder
# to &$fn($tail, $remainingsize) and use its result as the tail of the
# output stream. (See test cases below for illustration.)
sub chunks_change_tail {
@_ == 3 or fp_croak_arity 3;
my ($chunks, $minsize, $fn) = @_;
weaken $_[0];
is_null $chunks and die "got empty input";
# start and rest are parts of the same stream of chunks,
# windowsize is the number of bytes between them
my $next;
$next = sub {
@_ == 3 or fp_croak_arity 3;
my ($start, $rest, $windowsize) = @_;
my $next = $next;
lazy {
NEXT: {
FORCE $start, $rest; # optional (since is_null, first
# etc. are forcing promises
# anyway), but might reduce
# overhead slightly?
my $first = first $start;
my $lenfirst = length $first;
my $reserve = $windowsize - $minsize;
if ($lenfirst <= $reserve) {
cons($first,
&$next(rest($start), $rest, $windowsize - $lenfirst))
} else {
if (is_null $rest) {
&$fn($start, $windowsize)
} else {
#&$next($start, rest $rest, $windowsize + length first $rest)
$windowsize = $windowsize + length first $rest;
$rest = rest $rest;
redo NEXT;
}
}
}
}
};
Weakened($next)->($chunks, $chunks, 0);
}
sub test_with_size {
@_ == 1 or fp_croak_arity 1;
my ($size) = @_;
stream_to_array chunks_change_tail(array_to_stream(["foo", "bars", "baz"]),
$size, sub { my ($tail, $len) = @_; cons $len, $tail });
}
use FP::DumperEqual;
TEST { test_with_size 1 }
['foo', 'bars', 3, 'baz'];
TEST {
dumperequal(test_with_size(2), test_with_size(1))
and dumperequal(test_with_size(3), test_with_size(1))
}
1;
TEST { test_with_size 4 }
['foo', 7, 'bars', 'baz'];
TEST { test_with_size 999 }
[10, 'foo', 'bars', 'baz'];
# ------------------------------------------------------------
our $verbose = 0;
our $opt_repl;
our $opt_leaktest;
our $opt_silent;
GetOptions(
"verbose" => \$verbose,
"help" => sub {usage},
"bufsize=n" => \$BUFSIZ,
"repl" => \$opt_repl,
"leaktest" => \$opt_leaktest,
"silent" => \$opt_silent,
) or exit 1;
# called for testing or debugging:
if ($opt_repl) {
require FP::Repl;
FP::Repl::repl();
exit 0
}
if (perhaps_run_tests "main") {
exit 0;
}
# called as tool:
usage unless (@ARGV == 1 or @ARGV == 2);
our ($n, $maybe_m) = @ARGV;
is_natural0 $n or usage "n must be a non-negative integer";
if (defined $maybe_m) {
is_natural0 $maybe_m or usage "m must be a non-negative integer";
}
our $mem_start = resource_rss if $opt_leaktest;
our $mem_end;
our $in = glob_to_fh * STDIN;
our $out = glob_to_fh * STDOUT;
if ($n) {
$in->seek($n) || do {
if ($! == ESPIPE) {
# non-seekable device
my $nbufs = int($n / $BUFSIZ);
my $nrest = $n % $BUFSIZ;
$nbufs * $BUFSIZ + $nrest == $n or die "Perl can't calculate?";
my $buf;
for (1 .. $nbufs) {
$in->xsysreadcompletely($buf, $BUFSIZ);
}
$in->xsysreadcompletely($buf, $nrest);
# XX: seek does not complain when file is too short; this
# does. This is inconsistent. Also, check $opt_silent ?
} else {
die "seek: $!";
}
};
}
if ($maybe_m) {
my $chunks = force fh_to_chunks $in, $BUFSIZ;
if (is_null $chunks) {
die "$myname: no remainder left after skipping $n byte(s)\n"
unless $opt_silent;
} else {
my $chunks2 = chunks_change_tail(
$chunks, $maybe_m,
sub {
my ($rest, $remainingsize) = @_;
$mem_end = resource_rss if $opt_leaktest;
if ($remainingsize < $maybe_m) {
if ($opt_silent) {
null
} else {
die "$myname: only $remainingsize byte(s) left "
. "after skipping $n byte(s)\n"
}
} else {
my $last = first $rest;
cons(substr($last, 0, $remainingsize - $maybe_m), null)
}
}
);
stream_for_each sub {
$out->xprint($_[0])
}, $chunks2;
}
} else {
$in->xsendfile_to($out);
$in->xclose;
}
$out->xclose;
if ($opt_leaktest and (($mem_end - $mem_start) / $mem_start > 1.5)) {
die "there was a leak";
}