Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh |
2 | * Jagannathan, and Stephen Weeks. | |
3 | * | |
4 | * MLton is released under a BSD-style license. | |
5 | * See the file MLton-LICENSE for details. | |
6 | *) | |
7 | ||
8 | structure Lines: LINES = | |
9 | struct | |
10 | ||
11 | fun startStop(ins, out, {start: int, stop: int}): unit = | |
12 | let | |
13 | val _ = Assert.assert("Lines.startStop", fn () => start <= stop) | |
14 | fun loop i = | |
15 | if i > stop | |
16 | then () | |
17 | else (case In.inputLine ins of | |
18 | NONE => () | |
19 | | SOME l => | |
20 | (if i >= start | |
21 | then Out.output(out, l) | |
22 | else (); | |
23 | loop(i + 1))) | |
24 | in loop 0 | |
25 | end | |
26 | ||
27 | fun dropLast (ins, out, {start: int, last: int}): unit = | |
28 | let | |
29 | val _ = Assert.assert ("Lines.dropLast", fn () => | |
30 | start >= 0 andalso last >= 0) | |
31 | fun line () = In.inputLine ins | |
32 | val _ = Int.for (0, start, fn _ => ignore (line ())) | |
33 | in | |
34 | if last = 0 | |
35 | then In.outputAll (ins, out) | |
36 | else | |
37 | let | |
38 | val q = | |
39 | Int.fold (0, last, Queue.empty (), fn (_, q) => | |
40 | Queue.enque (q, | |
41 | case line () of | |
42 | NONE => "" | |
43 | | SOME l => l)) | |
44 | fun loop (q: string Queue.t) = | |
45 | case line () of | |
46 | NONE => () | |
47 | | SOME l => | |
48 | let | |
49 | val q = Queue.enque (q, l) | |
50 | val (q, l') = valOf (Queue.deque q) | |
51 | val _ = Out.output (out, l') | |
52 | in | |
53 | loop q | |
54 | end | |
55 | in loop q | |
56 | end | |
57 | end | |
58 | ||
59 | ||
60 | end |