rename helper methods

This commit is contained in:
Vukašin Stepanović
2023-02-15 12:52:59 +00:00
parent bfcdb397ad
commit 935519ced5
133 changed files with 858 additions and 1044 deletions

View File

@@ -78,7 +78,7 @@ pub fn decode(
}
Mode::STRUCTURED_APPEND => {
if bits.available() < 16 {
return Err(Exceptions::format(format!(
return Err(Exceptions::formatWith(format!(
"Mode::Structured append expected bits.available() < 16, found bits of {}",
bits.available()
)));
@@ -93,7 +93,9 @@ pub fn decode(
let value = parseECIValue(&mut bits)?;
currentCharacterSetECI = CharacterSetECI::getCharacterSetECIByValue(value).ok();
if currentCharacterSetECI.is_none() {
return Err(Exceptions::format(format!("Value of {value} not valid")));
return Err(Exceptions::formatWith(format!(
"Value of {value} not valid"
)));
}
}
Mode::HANZI => {
@@ -130,7 +132,7 @@ pub fn decode(
currentCharacterSetECI,
hints,
)?,
_ => return Err(Exceptions::formatEmpty()),
_ => return Err(Exceptions::format),
}
}
}
@@ -179,7 +181,7 @@ fn decodeHanziSegment(
) -> Result<(), Exceptions> {
// Don't crash trying to read more bits than we have available.
if count * 13 > bits.available() {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
// Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -206,10 +208,10 @@ fn decodeHanziSegment(
}
let gb_encoder =
encoding::label::encoding_from_whatwg_label("GBK").ok_or(Exceptions::illegalStateEmpty())?;
encoding::label::encoding_from_whatwg_label("GBK").ok_or(Exceptions::illegalState)?;
let encode_string = gb_encoder
.decode(&buffer, encoding::DecoderTrap::Strict)
.map_err(|e| Exceptions::parse(format!("unable to decode buffer {buffer:?}: {e}")))?;
.map_err(|e| Exceptions::parseWith(format!("unable to decode buffer {buffer:?}: {e}")))?;
result.push_str(&encode_string);
Ok(())
}
@@ -223,7 +225,7 @@ fn decodeKanjiSegment(
) -> Result<(), Exceptions> {
// Don't crash trying to read more bits than we have available.
if count * 13 > bits.available() {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
// Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -252,7 +254,7 @@ fn decodeKanjiSegment(
let encoder = {
let _ = currentCharacterSetECI;
let _ = hints;
encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::formatEmpty())?
encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::format)?
};
#[cfg(feature = "allow_forced_iso_ied_18004_compliance")]
@@ -265,12 +267,12 @@ fn decodeKanjiSegment(
encoding::all::ISO_8859_1
}
} else {
encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::formatEmpty())?
encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::format)?
};
let encode_string = encoder
.decode(&buffer, encoding::DecoderTrap::Strict)
.map_err(|e| Exceptions::parse(format!("unable to decode buffer {buffer:?}: {e}")))?;
.map_err(|e| Exceptions::parseWith(format!("unable to decode buffer {buffer:?}: {e}")))?;
result.push_str(&encode_string);
@@ -287,7 +289,7 @@ fn decodeByteSegment(
) -> Result<(), Exceptions> {
// Don't crash trying to read more bits than we have available.
if 8 * count > bits.available() {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
let mut readBytes = vec![0u8; count];
@@ -303,7 +305,7 @@ fn decodeByteSegment(
// give a hint.
{
#[cfg(not(feature = "allow_forced_iso_ied_18004_compliance"))]
StringUtils::guessCharset(&readBytes, hints).ok_or(Exceptions::illegalStateEmpty())?
StringUtils::guessCharset(&readBytes, hints).ok_or(Exceptions::illegalState)?
}
#[cfg(feature = "allow_forced_iso_ied_18004_compliance")]
@@ -318,14 +320,14 @@ fn decodeByteSegment(
CharacterSetECI::getCharset(
currentCharacterSetECI
.as_ref()
.ok_or(Exceptions::illegalStateEmpty())?,
.ok_or(Exceptions::illegalState)?,
)
};
let encode_string = if currentCharacterSetECI.is_some()
&& currentCharacterSetECI
.as_ref()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
== &CharacterSetECI::Cp437
{
{
@@ -337,7 +339,9 @@ fn decodeByteSegment(
} else {
encoding
.decode(&readBytes, encoding::DecoderTrap::Strict)
.map_err(|e| Exceptions::parse(format!("unable to decode buffer {readBytes:?}: {e}")))?
.map_err(|e| {
Exceptions::parseWith(format!("unable to decode buffer {readBytes:?}: {e}"))
})?
};
result.push_str(&encode_string);
@@ -348,13 +352,13 @@ fn decodeByteSegment(
fn toAlphaNumericChar(value: u32) -> Result<char, Exceptions> {
if value as usize >= ALPHANUMERIC_CHARS.len() {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
ALPHANUMERIC_CHARS
.chars()
.nth(value as usize)
.ok_or(Exceptions::formatEmpty())
.ok_or(Exceptions::format)
}
fn decodeAlphanumericSegment(
@@ -368,7 +372,7 @@ fn decodeAlphanumericSegment(
let mut count = count;
while count > 1 {
if bits.available() < 11 {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
let nextTwoCharsBits = bits.readBits(11)?;
result.push(toAlphaNumericChar(nextTwoCharsBits / 45)?);
@@ -378,7 +382,7 @@ fn decodeAlphanumericSegment(
if count == 1 {
// special case: one character left
if bits.available() < 6 {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
result.push(toAlphaNumericChar(bits.readBits(6)?)?);
}
@@ -386,17 +390,12 @@ fn decodeAlphanumericSegment(
if fc1InEffect {
// We need to massage the result a bit if in an FNC1 mode:
for i in start..result.len() {
if result
.chars()
.nth(i)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
== '%'
{
if result.chars().nth(i).ok_or(Exceptions::indexOutOfBounds)? == '%' {
if i < result.len() - 1
&& result
.chars()
.nth(i + 1)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.ok_or(Exceptions::indexOutOfBounds)?
== '%'
{
// %% is rendered as %
@@ -422,11 +421,11 @@ fn decodeNumericSegment(
while count >= 3 {
// Each 10 bits encodes three digits
if bits.available() < 10 {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
let threeDigitsBits = bits.readBits(10)?;
if threeDigitsBits >= 1000 {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
result.push(toAlphaNumericChar(threeDigitsBits / 100)?);
result.push(toAlphaNumericChar((threeDigitsBits / 10) % 10)?);
@@ -436,22 +435,22 @@ fn decodeNumericSegment(
if count == 2 {
// Two digits left over to read, encoded in 7 bits
if bits.available() < 7 {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
let twoDigitsBits = bits.readBits(7)?;
if twoDigitsBits >= 100 {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
result.push(toAlphaNumericChar(twoDigitsBits / 10)?);
result.push(toAlphaNumericChar(twoDigitsBits % 10)?);
} else if count == 1 {
// One digit left over to read
if bits.available() < 4 {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
let digitBits = bits.readBits(4)?;
if digitBits >= 10 {
return Err(Exceptions::formatEmpty());
return Err(Exceptions::format);
}
result.push(toAlphaNumericChar(digitBits)?);
}
@@ -476,5 +475,5 @@ fn parseECIValue(bits: &mut BitSource) -> Result<u32, Exceptions> {
return Ok(((firstByte & 0x1F) << 16) | secondThirdBytes);
}
Err(Exceptions::formatEmpty())
Err(Exceptions::format)
}