remove .to_owned() calls on &str in exceptions

This commit is contained in:
Vukašin Stepanović
2023-02-15 11:03:27 +00:00
parent 722ce78fd0
commit 528ddea41c
47 changed files with 107 additions and 167 deletions

View File

@@ -180,9 +180,7 @@ pub fn encode_with_hints(
version = Version::getVersionForNumber(versionNumber)?;
let bitsNeeded = calculateBitsNeeded(mode, &header_bits, &data_bits, version);
if !willFit(bitsNeeded, version, &ec_level) {
return Err(Exceptions::writer(
"Data too big for requested version".to_owned(),
));
return Err(Exceptions::writer("Data too big for requested version"));
}
} else {
version = recommendVersion(&ec_level, mode, &header_bits, &data_bits)?;
@@ -446,9 +444,7 @@ pub fn terminateBits(num_data_bytes: u32, bits: &mut BitArray) -> Result<(), Exc
bits.appendBits(if (i & 0x01) == 0 { 0xEC } else { 0x11 }, 8)?;
}
if bits.getSize() != capacity as usize {
return Err(Exceptions::writer(
"Bits size does not equal capacity".to_owned(),
));
return Err(Exceptions::writer("Bits size does not equal capacity"));
}
Ok(())
}
@@ -467,7 +463,7 @@ pub fn getNumDataBytesAndNumECBytesForBlockID(
// numECBytesInBlock: &mut [u32],
) -> Result<(u32, u32), Exceptions> {
if block_id >= num_rsblocks {
return Err(Exceptions::writer("Block ID too large".to_owned()));
return Err(Exceptions::writer("Block ID too large"));
}
// numRsBlocksInGroup2 = 196 % 5 = 1
let num_rs_blocks_in_group2 = num_total_bytes % num_rsblocks;
@@ -488,18 +484,18 @@ pub fn getNumDataBytesAndNumECBytesForBlockID(
// Sanity checks.
// 26 = 26
if num_ec_bytes_in_group1 != numEcBytesInGroup2 {
return Err(Exceptions::writer("EC bytes mismatch".to_owned()));
return Err(Exceptions::writer("EC bytes mismatch"));
}
// 5 = 4 + 1.
if num_rsblocks != num_rs_blocks_in_group1 + num_rs_blocks_in_group2 {
return Err(Exceptions::writer("RS blocks mismatch".to_owned()));
return Err(Exceptions::writer("RS blocks mismatch"));
}
// 196 = (13 + 26) * 4 + (14 + 26) * 1
if num_total_bytes
!= ((num_data_bytes_in_group1 + num_ec_bytes_in_group1) * num_rs_blocks_in_group1)
+ ((num_data_bytes_in_group2 + numEcBytesInGroup2) * num_rs_blocks_in_group2)
{
return Err(Exceptions::writer("total bytes mismatch".to_owned()));
return Err(Exceptions::writer("total bytes mismatch"));
}
Ok(if block_id < num_rs_blocks_in_group1 {
@@ -522,7 +518,7 @@ pub fn interleaveWithECBytes(
// "bits" must have "getNumDataBytes" bytes of data.
if bits.getSizeInBytes() as u32 != num_data_bytes {
return Err(Exceptions::writer(
"Number of bits and data bytes does not match".to_owned(),
"Number of bits and data bytes does not match",
));
}
@@ -556,9 +552,7 @@ pub fn interleaveWithECBytes(
data_bytes_offset += numDataBytesInBlock as usize;
}
if num_data_bytes != data_bytes_offset as u32 {
return Err(Exceptions::writer(
"Data bytes does not match offset".to_owned(),
));
return Err(Exceptions::writer("Data bytes does not match offset"));
}
let mut result = BitArray::new();
@@ -757,7 +751,7 @@ pub fn appendKanjiBytes(content: &str, bits: &mut BitArray) -> Result<(), Except
.encode(content, encoding::EncoderTrap::Strict)
.map_err(|e| Exceptions::writer(format!("error {e}")))?;
if bytes.len() % 2 != 0 {
return Err(Exceptions::writer("Kanji byte size not even".to_owned()));
return Err(Exceptions::writer("Kanji byte size not even"));
}
let max_i = bytes.len() - 1; // bytes.length must be even
let mut i = 0;
@@ -772,7 +766,7 @@ pub fn appendKanjiBytes(content: &str, bits: &mut BitArray) -> Result<(), Except
subtracted = code as i32 - 0xc140;
}
if subtracted == -1 {
return Err(Exceptions::writer("Invalid byte sequence".to_owned()));
return Err(Exceptions::writer("Invalid byte sequence"));
}
let encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
bits.appendBits(encoded as u32, 13)?;