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

@@ -168,7 +168,7 @@ impl BitArray {
pub fn setRange(&mut self, start: usize, end: usize) -> Result<(), Exceptions> {
let mut end = end;
if end < start || end > self.size {
return Err(Exceptions::illegalArgumentEmpty());
return Err(Exceptions::illegalArgument);
}
if end == start {
return Ok(());
@@ -211,7 +211,7 @@ impl BitArray {
pub fn isRange(&self, start: usize, end: usize, value: bool) -> Result<bool, Exceptions> {
let mut end = end;
if end < start || end > self.size {
return Err(Exceptions::illegalArgumentEmpty());
return Err(Exceptions::illegalArgument);
}
if end == start {
return Ok(true); // empty range matches
@@ -253,7 +253,7 @@ impl BitArray {
*/
pub fn appendBits(&mut self, value: u32, num_bits: usize) -> Result<(), Exceptions> {
if num_bits > 32 {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"num bits must be between 0 and 32",
));
}
@@ -286,7 +286,7 @@ impl BitArray {
pub fn xor(&mut self, other: &BitArray) -> Result<(), Exceptions> {
if self.size != other.size {
return Err(Exceptions::illegalArgument("Sizes don't match"));
return Err(Exceptions::illegalArgumentWith("Sizes don't match"));
}
for i in 0..self.bits.len() {
//for (int i = 0; i < bits.length; i++) {

View File

@@ -65,7 +65,7 @@ impl BitMatrix {
*/
pub fn new(width: u32, height: u32) -> Result<Self, Exceptions> {
if width < 1 || height < 1 {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"Both dimensions must be greater than 0",
));
}
@@ -137,12 +137,12 @@ impl BitMatrix {
if string_representation
.chars()
.nth(pos)
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
== '\n'
|| string_representation
.chars()
.nth(pos)
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
== '\r'
{
if bitsPos > rowStartPos {
@@ -151,7 +151,7 @@ impl BitMatrix {
first_run = false;
rowLength = bitsPos - rowStartPos;
} else if bitsPos - rowStartPos != rowLength {
return Err(Exceptions::illegalArgument("row lengths do not match"));
return Err(Exceptions::illegalArgumentWith("row lengths do not match"));
}
rowStartPos = bitsPos;
nRows += 1;
@@ -166,7 +166,7 @@ impl BitMatrix {
bits[bitsPos] = false;
bitsPos += 1;
} else {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"illegal character encountered: {}",
string_representation[pos..].to_owned()
)));
@@ -180,7 +180,7 @@ impl BitMatrix {
// first_run = false;
rowLength = bitsPos - rowStartPos;
} else if bitsPos - rowStartPos != rowLength {
return Err(Exceptions::illegalArgument("row lengths do not match"));
return Err(Exceptions::illegalArgumentWith("row lengths do not match"));
}
nRows += 1;
}
@@ -307,7 +307,7 @@ impl BitMatrix {
pub fn xor(&mut self, mask: &BitMatrix) -> Result<(), Exceptions> {
if self.width != mask.width || self.height != mask.height || self.row_size != mask.row_size
{
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"input matrix dimensions do not match",
));
}
@@ -359,14 +359,14 @@ impl BitMatrix {
// ));
// }
if height < 1 || width < 1 {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"height and width must be at least 1",
));
}
let right = left + width;
let bottom = top + height;
if bottom > self.height || right > self.width {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"the region must fit inside the matrix",
));
}
@@ -440,7 +440,7 @@ impl BitMatrix {
self.rotate180();
Ok(())
}
_ => Err(Exceptions::illegalArgument(
_ => Err(Exceptions::illegalArgumentWith(
"degrees must be a multiple of 0, 90, 180, or 270",
)),
}

View File

@@ -70,7 +70,7 @@ impl BitSource {
*/
pub fn readBits(&mut self, numBits: usize) -> Result<u32, Exceptions> {
if !(1..=32).contains(&numBits) || numBits > self.available() {
return Err(Exceptions::illegalArgument(numBits.to_string()));
return Err(Exceptions::illegalArgumentWith(numBits.to_string()));
}
let mut result: u32 = 0;

View File

@@ -244,7 +244,7 @@ impl CharacterSetECI {
28 => Ok(CharacterSetECI::Big5),
29 => Ok(CharacterSetECI::GB18030),
30 => Ok(CharacterSetECI::EUC_KR),
_ => Err(Exceptions::notFound("Bad ECI Value")),
_ => Err(Exceptions::notFoundWith("Bad ECI Value")),
}
}

View File

@@ -67,7 +67,7 @@ impl GridSampler for DefaultGridSampler {
transform: &PerspectiveTransform,
) -> Result<BitMatrix, Exceptions> {
if dimensionX == 0 || dimensionY == 0 {
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
let mut bits = BitMatrix::new(dimensionX, dimensionY)?;
let mut points = vec![0.0; 2 * dimensionX as usize];
@@ -98,7 +98,7 @@ impl GridSampler for DefaultGridSampler {
// }
if image
.try_get(points[x] as u32, points[x + 1] as u32)
.ok_or(Exceptions::notFound(
.ok_or(Exceptions::notFoundWith(
"index out of bounds, see documentation in file for explanation",
))?
{

View File

@@ -200,13 +200,13 @@ impl<'a> MonochromeRectangleDetector<'_> {
}
}
} else {
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
lastRange_z = range;
y += deltaY;
x += deltaX
}
Err(Exceptions::notFoundEmpty())
Err(Exceptions::notFound)
}
/**

View File

@@ -77,7 +77,7 @@ impl<'a> WhiteRectangleDetector<'_> {
|| downInit >= image.getHeight() as i32
|| rightInit >= image.getWidth() as i32
{
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
Ok(WhiteRectangleDetector {
@@ -223,7 +223,7 @@ impl<'a> WhiteRectangleDetector<'_> {
}
if z.is_none() {
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
let mut t: Option<RXingResultPoint> = None;
@@ -241,7 +241,7 @@ impl<'a> WhiteRectangleDetector<'_> {
}
if t.is_none() {
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
let mut x: Option<RXingResultPoint> = None;
@@ -259,7 +259,7 @@ impl<'a> WhiteRectangleDetector<'_> {
}
if x.is_none() {
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
let mut y: Option<RXingResultPoint> = None;
@@ -277,12 +277,12 @@ impl<'a> WhiteRectangleDetector<'_> {
}
if y.is_none() {
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
Ok(self.center_edges(&y.unwrap(), &z.unwrap(), &x.unwrap(), &t.unwrap()))
} else {
Err(Exceptions::notFoundEmpty())
Err(Exceptions::notFound)
}
}

View File

@@ -233,7 +233,7 @@ impl GlobalHistogramBinarizer {
// If there is too little contrast in the image to pick a meaningful black point, throw rather
// than waste time trying to decode the image, and risk false positives.
if secondPeak - firstPeak <= numBuckets / 16 {
return Err(Exceptions::notFound(
return Err(Exceptions::notFoundWith(
"secondPeak - firstPeak <= numBuckets / 16 ",
));
}

View File

@@ -145,7 +145,7 @@ pub trait GridSampler {
let x = points[offset] as i32;
let y = points[offset + 1] as i32;
if x < -1 || x > width as i32 || y < -1 || y > height as i32 {
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
nudged = false;
if x == -1 {
@@ -172,7 +172,7 @@ pub trait GridSampler {
let x = points[offset as usize] as i32;
let y = points[offset as usize + 1] as i32;
if x < -1 || x > width as i32 || y < -1 || y > height as i32 {
return Err(Exceptions::notFoundEmpty());
return Err(Exceptions::notFound);
}
nudged = false;
if x == -1 {

View File

@@ -67,10 +67,10 @@ impl ECIInput for MinimalECIInput {
*/
fn charAt(&self, index: usize) -> Result<char, Exceptions> {
if index >= self.length() {
return Err(Exceptions::indexOutOfBounds(index.to_string()));
return Err(Exceptions::indexOutOfBoundsWith(index.to_string()));
}
if self.isECI(index as u32)? {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"value at {index} is not a character but an ECI"
)));
}
@@ -103,13 +103,13 @@ impl ECIInput for MinimalECIInput {
*/
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>, Exceptions> {
if start > end || end > self.length() {
return Err(Exceptions::indexOutOfBoundsEmpty());
return Err(Exceptions::indexOutOfBounds);
}
let mut result = String::new();
for i in start..end {
// for (int i = start; i < end; i++) {
if self.isECI(i as u32)? {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"value at {i} is not a character but an ECI"
)));
}
@@ -131,7 +131,7 @@ impl ECIInput for MinimalECIInput {
*/
fn isECI(&self, index: u32) -> Result<bool, Exceptions> {
if index >= self.length() as u32 {
return Err(Exceptions::indexOutOfBoundsEmpty());
return Err(Exceptions::indexOutOfBounds);
}
Ok(self.bytes[index as usize] > 255) // && self.bytes[index as usize] <= u16::MAX)
}
@@ -156,10 +156,10 @@ impl ECIInput for MinimalECIInput {
*/
fn getECIValue(&self, index: usize) -> Result<i32, Exceptions> {
if index >= self.length() {
return Err(Exceptions::indexOutOfBoundsEmpty());
return Err(Exceptions::indexOutOfBounds);
}
if !self.isECI(index as u32)? {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"value at {index} is not an ECI but a character"
)));
}
@@ -248,7 +248,7 @@ impl MinimalECIInput {
*/
pub fn isFNC1(&self, index: usize) -> Result<bool, Exceptions> {
if index >= self.length() {
return Err(Exceptions::indexOutOfBoundsEmpty());
return Err(Exceptions::indexOutOfBounds);
}
Ok(self.bytes[index] == 1000)
}

View File

@@ -19,7 +19,7 @@ impl OtsuLevelBinarizer {
fn generate_threshold_matrix(source: &dyn LuminanceSource) -> Result<BitMatrix, Exceptions> {
let image_buffer = {
let Some(buff) : Option<ImageBuffer<Luma<u8>,Vec<u8>>> = ImageBuffer::from_vec(source.getWidth() as u32, source.getHeight() as u32, source.getMatrix()) else {
return Err(Exceptions::illegalArgumentEmpty())
return Err(Exceptions::illegalArgument)
};
buff
};

View File

@@ -133,7 +133,7 @@ impl GenericGF {
*/
pub fn log(&self, a: i32) -> Result<i32, Exceptions> {
if a == 0 {
return Err(Exceptions::illegalArgumentEmpty());
return Err(Exceptions::illegalArgument);
}
// let pos: usize = a.try_into().unwrap();
Ok(self.logTable[a as usize])
@@ -144,7 +144,7 @@ impl GenericGF {
*/
pub fn inverse(&self, a: i32) -> Result<i32, Exceptions> {
if a == 0 {
return Err(Exceptions::arithmeticEmpty());
return Err(Exceptions::arithmetic);
}
let log_t_loc: usize = a as usize;
let loc: usize = ((self.size as i32) - self.logTable[log_t_loc] - 1) as usize;

View File

@@ -49,7 +49,9 @@ impl GenericGFPoly {
*/
pub fn new(field: GenericGFRef, coefficients: &[i32]) -> Result<Self, Exceptions> {
if coefficients.is_empty() {
return Err(Exceptions::illegalArgument("coefficients cannot be empty"));
return Err(Exceptions::illegalArgumentWith(
"coefficients cannot be empty",
));
}
Ok(Self {
field,
@@ -138,7 +140,7 @@ impl GenericGFPoly {
pub fn addOrSubtract(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
if self.field != other.field {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"GenericGFPolys do not have same GenericGF field",
));
}
@@ -175,7 +177,7 @@ impl GenericGFPoly {
pub fn multiply(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
if self.field != other.field {
//if (!field.equals(other.field)) {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"GenericGFPolys do not have same GenericGF field",
));
}
@@ -250,12 +252,12 @@ impl GenericGFPoly {
other: &GenericGFPoly,
) -> Result<(GenericGFPoly, GenericGFPoly), Exceptions> {
if self.field != other.field {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"GenericGFPolys do not have same GenericGF field",
));
}
if other.isZero() {
return Err(Exceptions::illegalArgument("Divide by 0"));
return Err(Exceptions::illegalArgumentWith("Divide by 0"));
}
let mut quotient = self.getZero();
@@ -264,7 +266,7 @@ impl GenericGFPoly {
let denominator_leading_term = other.getCoefficient(other.getDegree());
let inverse_denominator_leading_term = match self.field.inverse(denominator_leading_term) {
Ok(val) => val,
Err(_issue) => return Err(Exceptions::illegalArgument("arithmetic issue")),
Err(_issue) => return Err(Exceptions::illegalArgumentWith("arithmetic issue")),
};
while remainder.getDegree() >= other.getDegree() && !remainder.isZero() {

View File

@@ -77,7 +77,7 @@ impl ReedSolomonDecoder {
return Ok(0);
}
let Ok(syndrome) = GenericGFPoly::new(self.field, &syndromeCoefficients) else {
return Err(Exceptions::reedSolomonEmpty());
return Err(Exceptions::reedSolomon);
};
let sigmaOmega = self.runEuclideanAlgorithm(
&GenericGF::buildMonomial(self.field, twoS as usize, 1),
@@ -92,11 +92,11 @@ impl ReedSolomonDecoder {
//for (int i = 0; i < errorLocations.length; i++) {
let log_value = self.field.log(errorLocations[i] as i32)?;
if log_value > received.len() as i32 - 1 {
return Err(Exceptions::reedSolomon("Bad error location"));
return Err(Exceptions::reedSolomonWith("Bad error location"));
}
let position: isize = received.len() as isize - 1 - log_value as isize;
if position < 0 {
return Err(Exceptions::reedSolomon("Bad error location"));
return Err(Exceptions::reedSolomonWith("Bad error location"));
}
received[position as usize] =
GenericGF::addOrSubtract(received[position as usize], errorMagnitudes[i]);
@@ -134,7 +134,7 @@ impl ReedSolomonDecoder {
// Divide rLastLast by rLast, with quotient in q and remainder in r
if rLast.isZero() {
// Oops, Euclidean algorithm already terminated?
return Err(Exceptions::reedSolomon("r_{i-1} was zero"));
return Err(Exceptions::reedSolomonWith("r_{i-1} was zero"));
}
r = rLastLast;
let mut q = r.getZero();
@@ -152,7 +152,7 @@ impl ReedSolomonDecoder {
t = (q.multiply(&tLast)?).addOrSubtract(&tLastLast)?;
if r.getDegree() >= rLast.getDegree() {
return Err(Exceptions::reedSolomon(format!(
return Err(Exceptions::reedSolomonWith(format!(
"Division algorithm failed to reduce polynomial? r: {r}, rLast: {rLast}"
)));
}
@@ -160,12 +160,12 @@ impl ReedSolomonDecoder {
let sigmaTildeAtZero = t.getCoefficient(0);
if sigmaTildeAtZero == 0 {
return Err(Exceptions::reedSolomon("sigmaTilde(0) was zero"));
return Err(Exceptions::reedSolomonWith("sigmaTilde(0) was zero"));
}
let inverse = match self.field.inverse(sigmaTildeAtZero) {
Ok(res) => res,
Err(_err) => return Err(Exceptions::reedSolomon("ArithmetricException")),
Err(_err) => return Err(Exceptions::reedSolomonWith("ArithmetricException")),
};
let sigma = t.multiply_with_scalar(inverse);
let omega = r.multiply_with_scalar(inverse);
@@ -193,7 +193,7 @@ impl ReedSolomonDecoder {
}
}
if e != numErrors {
return Err(Exceptions::reedSolomon(
return Err(Exceptions::reedSolomonWith(
"Error locator degree does not match number of roots",
));
}

View File

@@ -73,11 +73,11 @@ impl ReedSolomonEncoder {
pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<(), Exceptions> {
if ec_bytes == 0 {
return Err(Exceptions::illegalArgument("No error correction bytes"));
return Err(Exceptions::illegalArgumentWith("No error correction bytes"));
}
let data_bytes = to_encode.len() - ec_bytes;
if data_bytes == 0 {
return Err(Exceptions::illegalArgument("No data bytes provided"));
return Err(Exceptions::illegalArgumentWith("No data bytes provided"));
}
let fld = self.field;
let generator = self.buildGenerator(ec_bytes);
@@ -86,9 +86,7 @@ impl ReedSolomonEncoder {
//System.arraycopy(toEncode, 0, infoCoefficients, 0, dataBytes);
let mut info = GenericGFPoly::new(fld, &info_coefficients)?;
info = info.multiply_by_monomial(ec_bytes, 1)?;
let remainder = &info
.divide(generator.ok_or(Exceptions::reedSolomonEmpty())?)?
.1;
let remainder = &info.divide(generator.ok_or(Exceptions::reedSolomon)?)?.1;
let coefficients = remainder.getCoefficients();
let num_zero_coefficients = ec_bytes - coefficients.len();
for i in 0..num_zero_coefficients {