-
-
Notifications
You must be signed in to change notification settings - Fork 172
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
} | ||
|
||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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; | ||
|
@@ -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() | ||
} | ||
|
||
/// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, how about just |
||
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 { | ||
|
@@ -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 => { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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