-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff8d2bc
commit e0e5599
Showing
3 changed files
with
56 additions
and
33 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::io::{self, Read, Write}; | ||
|
||
use crate::{byte_reader::ByteReader, byte_writer::ByteWriter}; | ||
|
||
/// It parses an integer bencoded value. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Will return an error if it can't read from the input or write to the | ||
/// output. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if we reach the end of the input without completing the | ||
/// integer (without reaching the end of the integer `e`). | ||
pub fn parse<R: Read, W: Write>( | ||
reader: &mut ByteReader<R>, | ||
writer: &mut ByteWriter<W>, | ||
) -> io::Result<()> { | ||
/* | ||
st = 0 -> Parsed begin integer (`i`) | ||
st = 1 -> Parsed sign (only negative is allowed) | ||
st = 2 -> Parsing digits | ||
st = 3 -> Parsed end integer (`e`) | ||
*/ | ||
|
||
let mut st = 0; | ||
|
||
loop { | ||
let byte = match reader.read_byte() { | ||
Ok(byte) => byte, | ||
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => { | ||
panic!("unexpected end of input parsing integer"); | ||
} | ||
Err(err) => return Err(err), | ||
}; | ||
|
||
let char = byte as char; | ||
|
||
if char.is_ascii_digit() { | ||
st = 2; | ||
writer.write_byte(byte)?; | ||
} else if char == 'e' && st == 2 { | ||
return Ok(()); | ||
} else if char == '-' && st == 0 { | ||
st = 1; | ||
writer.write_byte(byte)?; | ||
} else { | ||
panic!("invalid integer"); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod integer; |