mirror of
https://github.com/sigp/lighthouse.git
synced 2026-06-30 19:34:37 +00:00
Strip off newline chars from password (#1199)
* strips off newline chars from password * adds changes suggested in author review
This commit is contained in:
@@ -37,3 +37,57 @@ pub fn base_wallet_dir(matches: &ArgMatches, arg: &'static str) -> Result<PathBu
|
||||
PathBuf::new().join(".lighthouse").join("wallets"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Remove any number of newline or carriage returns from the end of a vector of bytes.
|
||||
pub fn strip_off_newlines(mut bytes: Vec<u8>) -> Vec<u8> {
|
||||
let mut strip_off = 0;
|
||||
for (i, byte) in bytes.iter().rev().enumerate() {
|
||||
if *byte == b'\n' || *byte == b'\r' {
|
||||
strip_off = i + 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
bytes.truncate(bytes.len() - strip_off);
|
||||
|
||||
bytes.to_vec()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::strip_off_newlines;
|
||||
|
||||
#[test]
|
||||
fn test_strip_off() {
|
||||
let expected = "hello world".as_bytes().to_vec();
|
||||
|
||||
assert_eq!(
|
||||
strip_off_newlines("hello world\n".as_bytes().to_vec()),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
strip_off_newlines("hello world\n\n\n\n".as_bytes().to_vec()),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
strip_off_newlines("hello world\r".as_bytes().to_vec()),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
strip_off_newlines("hello world\r\r\r\r\r".as_bytes().to_vec()),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
strip_off_newlines("hello world\r\n".as_bytes().to_vec()),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
strip_off_newlines("hello world\r\n\r\n".as_bytes().to_vec()),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
strip_off_newlines("hello world".as_bytes().to_vec()),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user