Skip to content
Snippets Groups Projects
Commit 20fe954f authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Use new tryParseRange instead of ad-hoc code in CLI parsing.

parent 32ccb963
Branches
No related tags found
No related merge requests found
package azgracompress.cli; package azgracompress.cli;
import azgracompress.compression.Range;
import azgracompress.data.V3i; import azgracompress.data.V3i;
import java.util.Optional; import java.util.Optional;
public abstract class ParseUtils { public abstract class ParseUtils {
private static String removeSpacesInString(final String string) {
return string.replaceAll("\\s", "");
}
/** /**
* Try to parse int from string. * Try to parse int from string.
* *
...@@ -19,14 +25,38 @@ public abstract class ParseUtils { ...@@ -19,14 +25,38 @@ public abstract class ParseUtils {
} }
} }
/**
* Try to parse integer range from string.
*
* @param rangeString Range string.
* @param delimiter Delimiter between numbers.
* @return Optional parsed range.
*/
public static Optional<Range<Integer>> tryParseRange(final String rangeString, final char delimiter) {
final String string = removeSpacesInString(rangeString);
final int delimiterIndex = string.indexOf(delimiter);
if (delimiterIndex == -1) {
return Optional.empty();
}
final Optional<Integer> maybeFrom = tryParseInt(string.substring(0, delimiterIndex));
final Optional<Integer> maybeTo = tryParseInt(string.substring(delimiterIndex + 1));
if (maybeFrom.isPresent() && maybeTo.isPresent()) {
return Optional.of(new Range<>(maybeFrom.get(), maybeTo.get()));
}
return Optional.empty();
}
/** /**
* Try to parse 3 dimensional vector from string. * Try to parse 3 dimensional vector from string.
* *
* @param string Vector string. * @param v3iString Vector string.
* @param delimiter Delimiter between numbers. * @param delimiter Delimiter between numbers.
* @return Optional parse result. * @return Optional parsed vector.
*/ */
public static Optional<V3i> tryParseV3i(final String string, final char delimiter) { public static Optional<V3i> tryParseV3i(final String v3iString, final char delimiter) {
final String string = removeSpacesInString(v3iString);
final int firstDelimiterIndex = string.indexOf(delimiter); final int firstDelimiterIndex = string.indexOf(delimiter);
if (firstDelimiterIndex == -1) { if (firstDelimiterIndex == -1) {
return Optional.empty(); return Optional.empty();
......
...@@ -298,21 +298,16 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable { ...@@ -298,21 +298,16 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
final int inputFileArgumentsOffset) { final int inputFileArgumentsOffset) {
int rangeSeparatorIndex = inputFileArguments[inputFileArgumentsOffset].indexOf("-"); int rangeSeparatorIndex = inputFileArguments[inputFileArgumentsOffset].indexOf("-");
if (rangeSeparatorIndex != -1) { if (rangeSeparatorIndex != -1) {
// Here we parse the plane range option.
final String fromIndexString =
inputFileArguments[inputFileArgumentsOffset].substring(0, rangeSeparatorIndex);
final String toIndexString =
inputFileArguments[inputFileArgumentsOffset].substring(rangeSeparatorIndex + 1);
final Optional<Integer> indexFromResult = ParseUtils.tryParseInt(fromIndexString); Optional<Range<Integer>> parsedRange =
final Optional<Integer> indexToResult = ParseUtils.tryParseInt(toIndexString); ParseUtils.tryParseRange(inputFileArguments[inputFileArgumentsOffset], '-');
if (indexFromResult.isPresent() && indexToResult.isPresent()) { if (!parsedRange.isPresent()) {
getInputDataInfo().setPlaneRange(new Range<>(indexFromResult.get(), indexToResult.get()));
} else {
parseErrorOccurred = true; parseErrorOccurred = true;
errorBuilder.append("Plane range index is wrong. Expected format D-D, got: ").append( errorBuilder.append("Plane range index is wrong. Expected format D-D, got: ")
inputFileArguments[inputFileArgumentsOffset]).append('\n'); .append(inputFileArguments[inputFileArgumentsOffset]).append('\n');
} else {
getInputDataInfo().setPlaneRange(parsedRange.get());
} }
} else { } else {
// Here we parse single plane index option. // Here we parse single plane index option.
...@@ -328,53 +323,6 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable { ...@@ -328,53 +323,6 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
} }
} }
// /**
// * Parse image dimensions from the command line.
// *
// * @param dimsString Dimensions string.
// * @param errorBuilder String error builder.
// */
// private V3i parseV3i(final String dimsString, StringBuilder errorBuilder) {
// // We thing of 3x3x1 and 3x3 as the same thing
//
// final int firstDelimiterIndex = dimsString.indexOf('x');
// if (firstDelimiterIndex == -1) {
// parseErrorOccurred = true;
// errorBuilder.append("Error parsing image dimensions. We require DxDxD or DxD [=DxDx1]\n");
// return;
// }
// final String num1String = dimsString.substring(0, firstDelimiterIndex);
// final String secondPart = dimsString.substring(firstDelimiterIndex + 1);
//
// final int secondDelimiterIndex = secondPart.indexOf('x');
// if (secondDelimiterIndex == -1) {
// final Optional<Integer> n1Result = ParseUtils.tryParseInt(num1String);
// final Optional<Integer> n2Result = ParseUtils.tryParseInt(secondPart);
// if (n1Result.isPresent() && n2Result.isPresent()) {
// getInputDataInfo().setDimension(new V3i(n1Result.get(), n2Result.get(), 1));
// } else {
// parseErrorOccurred = true;
// errorBuilder.append(String.format("%sx%s\n", num1String, secondPart));
// }
// } else {
// final String num2String = secondPart.substring(0, secondDelimiterIndex);
// final String num3String = secondPart.substring(secondDelimiterIndex + 1);
//
// final Optional<Integer> n1Result = ParseUtils.tryParseInt(num1String);
// final Optional<Integer> n2Result = ParseUtils.tryParseInt(num2String);
// final Optional<Integer> n3Result = ParseUtils.tryParseInt(num3String);
//
// if (n1Result.isPresent() && n2Result.isPresent() && n3Result.isPresent()) {
// getInputDataInfo().setDimension(new V3i(n1Result.get(), n2Result.get(), n3Result.get()));
// } else {
// parseErrorOccurred = true;
// errorBuilder.append("Failed to parse image dimensions of format DxDxD, got: ");
// errorBuilder.append(String.format("%sx%sx%s\n", num1String, num2String, num3String));
// }
// }
// }
/** /**
* Parse bits per codebook index. * Parse bits per codebook index.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment