Allow token/market names to fill storage bytes completely

Previously the last byte was always zero.
This commit is contained in:
Christian Kamm 2023-03-20 13:48:44 +01:00
parent 99360e69a3
commit 658a220095
1 changed files with 20 additions and 1 deletions

View File

@ -14,7 +14,7 @@ pub(crate) use zip;
pub fn fill_from_str<const N: usize>(name: &str) -> Result<[u8; N]> {
let name_bytes = name.as_bytes();
require!(name_bytes.len() < N, MangoError::SomeError);
require!(name_bytes.len() <= N, MangoError::SomeError);
let mut name_ = [0u8; N];
name_[..name_bytes.len()].copy_from_slice(name_bytes);
Ok(name_)
@ -30,3 +30,22 @@ pub fn format_zero_terminated_utf8_bytes(
.trim_matches(char::from(0)),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fill_from_str() {
assert_eq!(fill_from_str::<4>(""), Ok([0, 0, 0, 0]));
assert_eq!(
fill_from_str::<4>("abc"),
Ok(['a' as u8, 'b' as u8, 'c' as u8, 0])
);
assert_eq!(
fill_from_str::<4>("abcd"),
Ok(['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8])
);
assert!(fill_from_str::<4>("abcde").is_err());
}
}