Skip to content

Commit 10f8efd

Browse files
Move tracking, pad to streams
1 parent a7f7745 commit 10f8efd

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

libraries/common/binstream.effekt

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -148,33 +148,6 @@ def x{ body: => Unit / { literal, HexSplices } }: Int = {
148148
res
149149
}
150150

151-
// Counting and padding
152-
// --------------------
153-
// TODO could also be part of stream library
154-
155-
/// get current position in stream
156-
effect getPos(): Int
157-
158-
/// Track position in stream, starting with init
159-
/// Handles getPos and pad
160-
def tracking[A](init: Int){ body: => Unit / { emit[A], getPos } }: Unit / emit[A] = {
161-
var n = init
162-
try body()
163-
with emit[A] { b => n = n + 1; resume(do emit[A](b)) }
164-
with getPos{ resume(n) }
165-
}
166-
167-
/// Pad to a multiple of fac, by calling gen for each
168-
def pad[A](fac: Int){ gen: => A }: Unit / {getPos, emit[A]} = {
169-
while(mod(do getPos(), fac) != 0) {
170-
do emit[A](gen())
171-
}
172-
}
173-
174-
/// Track how many bytes were emitted
175-
/// Handles getPos and pad
176-
def tracking[A]{ body: => Unit / { emit[A], getPos } }: Unit / emit[A] =
177-
tracking[A](0){body}
178151

179152
// Sub-Byte
180153
// ========

libraries/common/stream.effekt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,33 @@ def tee[A]{ cons1: { => Unit / emit[A] } => Unit }{ cons2: { => Unit / emit[A] }
504504
}
505505
}
506506

507+
// Counting and padding
508+
// --------------------
509+
510+
/// get current position in stream
511+
effect getPos(): Int
512+
513+
/// Track position in stream, starting with init
514+
/// Handles getPos
515+
def tracking[A](init: Int){ body: => Unit / { emit[A], getPos } }: Unit / emit[A] = {
516+
var n = init
517+
try body()
518+
with emit[A] { b => n = n + 1; resume(do emit[A](b)) }
519+
with getPos{ resume(n) }
520+
}
521+
522+
/// Pad to a multiple of fac emitted values, by emitting gen repeatedly
523+
def pad[A](fac: Int){ gen: => A }: Unit / {getPos, emit[A]} = {
524+
while(mod(do getPos(), fac) != 0) {
525+
do emit[A](gen())
526+
}
527+
}
528+
529+
/// Track how many bytes were emitted (in the body)
530+
/// Handles getPos
531+
def tracking[A]{ body: => Unit / { emit[A], getPos } }: Unit / emit[A] =
532+
tracking[A](0){body}
533+
507534
namespace returning {
508535

509536
/// Canonical handler of push streams that performs `action` for every

0 commit comments

Comments
 (0)