RosettaCodeData/Task/Bitwise-IO/11l/bitwise-io.11l

71 lines
1.3 KiB
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
T BitWriter
2024-04-19 16:56:29 -07:00
FileWr out
2023-07-01 11:58:00 -04:00
accumulator = 0
bcount = 0
F (fname)
2024-04-19 16:56:29 -07:00
.out = File(fname, WRITE)
2023-07-01 11:58:00 -04:00
F _writebit(bit)
I .bcount == 8
.flush()
I bit > 0
.accumulator [|]= 1 << (7 - .bcount)
.bcount++
F writebits(bits, =n)
L n > 0
._writebit(bits [&] 1 << (n - 1))
n--
F flush()
.out.write_bytes([Byte(.accumulator)])
.accumulator = 0
.bcount = 0
F close()
.flush()
.out.close()
T BitReader
File input
accumulator = 0
bcount = 0
read = 0
F (fname)
2024-04-19 16:56:29 -07:00
.input = File(fname)
2023-07-01 11:58:00 -04:00
F _readbit()
I .bcount == 0
2024-04-19 16:56:29 -07:00
V a = .input.read_bytes(at_most' 1)
2023-07-01 11:58:00 -04:00
I !a.empty
.accumulator = a[0]
.bcount = 8
.read = a.len
V rv = (.accumulator [&] (1 << (.bcount - 1))) >> (.bcount - 1)
.bcount--
R rv
F readbits(=n)
V v = 0
L n > 0
v = (v << 1) [|] ._readbit()
n--
R v
V writer = BitWriter(bitio_test.dat)
V chars = 12345abcde
L(ch) chars
writer.writebits(ch.code, 7)
writer.close()
V reader = BitReader(bitio_test.dat)
[Char] charsa
L
V x = reader.readbits(7)
I reader.read == 0
L.break
charsa.append(Char(code' x))
print(charsa.join())