Trim source buffer to how many bytes were read
This commit is contained in:
parent
b11a0f66d3
commit
8dca0b4a7f
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -28,7 +28,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||
|
||||
[[package]]
|
||||
name = "circe"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
dependencies = [
|
||||
"native-tls",
|
||||
"serde",
|
||||
|
|
17
src/lib.rs
17
src/lib.rs
|
@ -163,16 +163,19 @@ impl Client {
|
|||
fn read_string(&mut self) -> Option<String> {
|
||||
let mut buffer = [0u8; 512];
|
||||
|
||||
match self.stream.read(&mut buffer) {
|
||||
Ok(_) => {}
|
||||
Err(_) => return None,
|
||||
let num_bytes = match self.stream.read(&mut buffer) {
|
||||
Ok(b) => b,
|
||||
Err(_) => 0,
|
||||
};
|
||||
|
||||
let res = String::from_utf8_lossy(&buffer);
|
||||
if num_bytes == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// The trimming is required because if the message is less than 512 bytes it will be
|
||||
// padded with a bunch of 0u8 because of the pre-allocated buffer
|
||||
Some(res.trim().trim_matches(char::from(0)).trim().into())
|
||||
// Slice to the number of bytes that were actually read
|
||||
let res = String::from_utf8_lossy(&buffer[..num_bytes]);
|
||||
|
||||
Some(res.into())
|
||||
}
|
||||
|
||||
/// Read data coming from the IRC as a [`commands::Command`].
|
||||
|
|
|
@ -30,7 +30,7 @@ fn main() -> Result<(), std::io::Error> {
|
|||
if message.contains("!close") {
|
||||
client.part("#main")?;
|
||||
}
|
||||
println!("PRIVMSG received from {}: {} {}", nick, channel, message);
|
||||
println!("PRIVMSG received from {}: {} {:#?}", nick, channel, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue