Trim empty bytes from the pre-allocated buffer

This commit is contained in:
Yash Karandikar 2021-11-16 15:18:38 -06:00
parent e0472ea6f0
commit 75fc554525
2 changed files with 12 additions and 4 deletions

View file

@ -1,7 +1,7 @@
channels = ["#main", "#circe"]
host = "192.168.1.28"
host = "karx.xyz"
mode = "+B"
nickname = "circe"
port = 6667
port = 6697
username = "circe"
ssl = true

View file

@ -119,6 +119,7 @@ impl Client {
self.write_command(commands::Command::PONG(code.to_string()))?;
}
commands::Command::OTHER(line) => {
#[cfg(feature = "debug")]
println!("{}", line);
if line.contains("001") {
break;
@ -146,7 +147,11 @@ impl Client {
Err(_) => return None,
};
Some(String::from_utf8_lossy(&buffer).into())
let res = String::from_utf8_lossy(&buffer);
// 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())
}
/// Read data coming from the IRC as a [`commands::Command`].
@ -167,6 +172,9 @@ impl Client {
/// Returns error if there are no new messages. This should not be taken as an actual error, because nothing went wrong.
pub fn read(&mut self) -> Result<commands::Command, ()> {
if let Some(string) = self.read_string() {
#[cfg(feature = "debug")]
println!("{:#?}", string);
let command = commands::Command::from_str(&string);
if let commands::Command::PONG(command) = command {