Anonymous user
Bitwise IO: Difference between revisions
Add source for Rust
MaiconSoft (talk | contribs) No edit summary |
(Add source for Rust) |
||
Line 90:
1,0,0,0,0,1,1, -- C
1,0,1,0,1,0,1, -- U
1,0,1,0,0,1,1 -- S
);
Data : Bit_Array (ABACUS'Range);
Line 269:
<lang autohotkey>file = %A_WorkingDir%\z.dat
IfExist, %A_WorkingDir%\z.dat
IfNotEqual ErrorLevel,0, MsgBox Can't delete file "%file%"`nErrorLevel = "%ErrorLevel%"
Line 453:
typedef uint8_t byte;
typedef struct {
} bit_io_t, *bit_filter;
bit_filter b_attach(FILE *f)
{
}
void b_write(byte *buf, size_t n_bits, size_t shift, bit_filter bf)
{
}
}
}
}
}
size_t b_read(byte *buf, size_t n_bits, size_t shift, bit_filter bf)
{
}
}
}
}
void b_detach(bit_filter bf)
{
}
}
int main()
{
}</lang>
Line 1,303:
import (
)
Line 1,312:
const (
)
Line 1,321:
type writer interface {
}
// Writer implements bit-wise writing to an io.Writer.
type Writer struct {
}
// writeLSB writes `width` bits of `c` in LSB order.
func (w *Writer) writeLSB(c uint32, width uint) error {
}
}
}
// writeMSB writes `width` bits of `c` in MSB order.
func (w *Writer) writeMSB(c uint32, width uint) error {
}
}
}
// WriteBits writes up to 16 bits of `c` to the underlying writer.
// Even for MSB ordering the bits are taken from the lower bits of `c`.
// (e.g.
func (w *Writer) WriteBits(c uint16, width uint) error {
}
}
Line 1,378:
// It does not close the underlying writer.
func (w *Writer) Close() error {
}
}
}
}
}
}
}
// NewWriter returns a new bit Writer that writes completed bytes to `w`.
func NewWriter(w io.Writer, order Order) *Writer {
}
}
}
Line 1,427:
// Reader implements bit-wise reading from an io.Reader.
type Reader struct {
}
func (r *Reader) readLSB(width uint) (uint16, error) {
}
}
}
func (r *Reader) readMSB(width uint) (uint16, error) {
}
}
}
// ReadBits reads up to 16 bits from the underlying reader.
func (r *Reader) ReadBits(width uint) (uint16, error) {
}
}
Line 1,476:
// It does not close the underlying reader.
func (r *Reader) Close() error {
}
}
// NewReader returns a new bit Reader that reads bytes from `r`.
func NewReader(r io.Reader, order Order) *Reader {
}
}
}</lang>
And a test file (such as <code>bit_test.go</code>):
Line 1,506:
import (
)
func ExampleWriter_WriteBits() {
}
}
func Example() {
}
}
}
}
}
}
}</lang>
With this test file, running <code>go test -v</code> will compile the package and run the example verifying the output is as listed above in the <code>// Output:</code> comments.
Line 2,030:
my ($out, $l, $num, $q) = @_;
$l .= substr(unpack("B*", pack("N", $num)),
if ( (length($l) > 8) ) {
}
return $l;
Line 2,055:
if ( $n > 32 ) { return 0; }
while ( length($b) < $n ) {
}
my $bits = "0" x ( 32-$n ) . substr($b, 0, $n);
Line 2,619:
=={{header|Red}}==
<lang red>Red [
]
str-compress: function [
][
]
]
]
str-expand: function [
][
]
]
]
Line 2,669:
expanded: str-expand compressed
prin [
]
</lang>
Line 2,788:
puts "fail!"
end</lang>
=={{header|Rust}}==
The implementation accepts the number of bits to discard/expand as an argument.
<lang Rust>pub trait Codec<Input = u8> {
type Output: Iterator<Item = u8>;
fn accept(&mut self, input: Input) -> Self::Output;
fn finish(self) -> Self::Output;
}
#[derive(Debug)]
pub struct BitDiscard {
buf: u16, // Use the higher byte for storing the leftovers
buf_bits: u8, // How many bits are valid in the buffer
valid_len: u8, // How many bits to keep from the input
shift_len: u8, // Pre-computed shift of the input byte
}
impl BitDiscard {
pub fn new(discard: u8) -> Self {
assert!(discard < 8);
BitDiscard {
buf: 0,
buf_bits: 0,
valid_len: 8 - discard,
shift_len: 8 + discard,
}
}
}
impl Codec<u8> for BitDiscard {
type Output = std::option::IntoIter<u8>;
fn accept(&mut self, input: u8) -> Self::Output {
let add = ((input as u16) << self.shift_len) >> self.buf_bits;
self.buf |= add;
self.buf_bits += self.valid_len;
let result = if self.buf_bits >= 8 {
let result = (self.buf >> 8) as u8;
self.buf <<= 8;
self.buf_bits -= 8;
Some(result)
} else {
None
};
result.into_iter()
}
fn finish(self) -> Self::Output {
let result = if self.buf_bits > 0 {
Some((self.buf >> 8) as u8)
} else {
None
};
result.into_iter()
}
}
#[derive(Debug)]
pub struct BitExpand {
buf: u16, // For storing the leftovers
buf_bits: u8, // How many bits are valid in the buffer
valid_len: u8, // How many bits are valid in the input
shift_len: u8, // How many bits to shift when expanding
}
impl BitExpand {
pub fn new(expand: u8) -> Self {
assert!(expand < 8);
Self {
buf: 0,
buf_bits: 0,
valid_len: 8 - expand,
shift_len: 8 + expand,
}
}
}
impl Codec<u8> for BitExpand {
type Output = BitExpandIter;
fn accept(&mut self, input: u8) -> Self::Output {
let add = ((input as u16) << 8) >> self.buf_bits;
self.buf |= add;
self.buf_bits += 8;
let buf = self.buf;
let leftover = self.buf_bits % self.valid_len;
let buf_bits = self.buf_bits - leftover;
self.buf <<= buf_bits;
self.buf_bits = leftover;
Self::Output {
buf,
buf_bits,
shift_len: self.shift_len,
valid_len: self.valid_len,
}
}
fn finish(self) -> Self::Output {
Self::Output {
buf: 0,
buf_bits: 0,
shift_len: 0,
valid_len: self.valid_len,
}
}
}
#[derive(Debug)]
pub struct BitExpandIter {
buf: u16,
buf_bits: u8,
valid_len: u8,
shift_len: u8,
}
impl Iterator for BitExpandIter {
type Item = u8;
fn next(&mut self) -> Option<u8> {
if self.buf_bits < self.valid_len {
None
} else {
let result = (self.buf >> self.shift_len) as u8;
self.buf <<= self.valid_len;
self.buf_bits -= self.valid_len;
Some(result)
}
}
}
fn process_bytes<C: Codec>(mut codec: C, bytes: &[u8]) -> Vec<u8> {
let mut result: Vec<u8> = bytes.iter().flat_map(|byte| codec.accept(*byte)).collect();
codec.finish().for_each(|byte| result.push(byte));
result
}
fn print_bytes(bytes: &[u8]) {
for byte in bytes {
print!("{:08b} ", byte);
}
println!();
for byte in bytes {
print!("{:02x} ", byte);
}
println!();
}
fn main() {
let original = b"STRINGIFY!";
let discard = 1;
print_bytes(&original[..]);
let compressed = process_bytes(BitDiscard::new(discard), &original[..]);
print_bytes(&compressed);
let decompressed = process_bytes(BitExpand::new(discard), &compressed);
print_bytes(&decompressed);
}</lang>
=={{header|Seed7}}==
|