Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small cleanup of byte_stream code #385

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 74 additions & 64 deletions crates/gosub_shared/src/byte_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub struct ByteStream {
buffer_pos: usize,
/// Reference to the actual buffer stream in u8 bytes
u8_buffer: Vec<u8>,
// True when the buffer is empty and not yet have a closed stream
/// True when the buffer is empty and not yet have a closed stream
closed: bool,
}

Expand All @@ -103,10 +103,8 @@ pub trait Stream {
fn prev(&mut self);
/// Unread n characters
fn prev_n(&mut self, n: usize);

// Returns a slice
fn get_slice(&self, start: usize, end: usize) -> &[Character];

/// Resets the stream back to the start position
fn reset_stream(&mut self);
/// Closes the stream (no more data can be added)
Expand All @@ -132,54 +130,26 @@ impl Default for ByteStream {
}

impl Stream for ByteStream {
/// Closes the stream so no more data can be added
fn close(&mut self) {
self.closed = true;
}

/// Returns true when the stream is closed and no more input can be read after this buffer
/// is emptied
fn closed(&self) -> bool {
self.closed
}

/// Returns true when the buffer is empty and there is no more input to read
fn empty(&self) -> bool {
self.buffer_pos >= self.buffer.len()
}

/// Returns true when the stream is closed and all the bytes have been read
fn eof(&self) -> bool {
self.closed() && self.empty()
}

fn next(&mut self) {
self.next_n(1);
}

fn next_n(&mut self, offset: usize) {
if self.buffer.is_empty() {
return;
/// Read the current character
fn read(&self) -> Character {
// Return none if we already have read EOF
if self.eof() {
return StreamEnd;
}

self.buffer_pos += offset;
if self.buffer_pos >= self.buffer.len() {
self.buffer_pos = self.buffer.len();
if self.buffer.is_empty() || self.buffer_pos >= self.buffer.len() {
return StreamEmpty;
}
}

/// Returns the current offset in the stream
fn tell(&self) -> usize {
self.buffer_pos
self.buffer[self.buffer_pos]
}

/// Returns the length of the buffer
fn length(&self) -> usize {
self.buffer.len()
}
/// Read a character and advance to the next
fn read_and_next(&mut self) -> Character {
let c = self.read();

fn reset_stream(&mut self) {
self.buffer_pos = 0;
self.next();
c
}

/// Looks ahead in the stream, can use an optional index if we want to seek further
Expand All @@ -201,38 +171,29 @@ impl Stream for ByteStream {
self.buffer[self.buffer_pos + offset]
}

fn read_and_next(&mut self) -> Character {
let c = self.read();

self.next();
c
/// Returns the next character in the stream
fn next(&mut self) {
self.next_n(1);
}

fn read(&self) -> Character {
// Return none if we already have read EOF
if self.eof() {
return StreamEnd;
}

if self.buffer.is_empty() || self.buffer_pos >= self.buffer.len() {
return StreamEmpty;
/// Returns the n'th character in the stream
fn next_n(&mut self, offset: usize) {
if self.buffer.is_empty() {
return;
}

self.buffer[self.buffer_pos]
}

fn chars_left(&self) -> usize {
self.buffer_pos += offset;
if self.buffer_pos >= self.buffer.len() {
return 0;
self.buffer_pos = self.buffer.len();
}

self.buffer.len() - self.buffer_pos
}

/// Unread the current character
fn prev(&mut self) {
self.prev_n(1);
}

/// Unread n characters
fn prev_n(&mut self, n: usize) {
if self.buffer_pos < n {
self.buffer_pos = 0;
Expand All @@ -245,6 +206,51 @@ impl Stream for ByteStream {
fn get_slice(&self, start: usize, end: usize) -> &[Character] {
&self.buffer[start..end]
}

/// Resets the stream to the first character of the stream
fn reset_stream(&mut self) {
self.buffer_pos = 0;
}

/// Closes the stream so no more data can be added
fn close(&mut self) {
self.closed = true;
}

/// Returns true when the stream is closed and no more input can be read after this buffer
/// is emptied
fn closed(&self) -> bool {
self.closed
}

/// Returns true when the buffer is empty and there is no more input to read
fn empty(&self) -> bool {
self.buffer_pos >= self.buffer.len()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty doesn't quite fit here, exhausted is probably more descriptive


/// Returns true when the stream is closed and all the bytes have been read
fn eof(&self) -> bool {
self.closed() && self.empty()
}

/// Returns the current offset in the stream
fn tell(&self) -> usize {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, how about just offset here?

self.buffer_pos
}

/// Returns the length of the buffer
fn length(&self) -> usize {
self.buffer.len()
}

/// Returns the number of characters left in the buffer
fn chars_left(&self) -> usize {
if self.buffer_pos >= self.buffer.len() {
return 0;
}

self.buffer.len() - self.buffer_pos
}
}

impl ByteStream {
Expand Down Expand Up @@ -351,6 +357,10 @@ impl ByteStream {

/// Sets the encoding for this stream, and decodes the u8_buffer into the buffer with the
/// correct encoding.
///
/// @TODO: I think we should not set an encoding and completely convert a stream. Instead,
/// we should set an encoding, and try to use that encoding. If we find that we have a different
/// encoding, we can notify the user, or try to convert the stream to the correct encoding.
pub fn force_set_encoding(&mut self, e: Encoding) {
match e {
Encoding::UTF8 => {
Expand Down
Loading