Refactor a little bit of result parsing (mostly, a test commit)

This commit is contained in:
Sean Owen 2014-01-23 21:56:09 +00:00
parent be038c7eef
commit d7c78074ec
4 changed files with 9 additions and 29 deletions

View file

@ -52,7 +52,7 @@ public final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultPar
String note = matchSingleDoCoMoPrefixedField("NOTE:", rawText, false);
String[] addresses = matchDoCoMoPrefixedField("ADR:", rawText, true);
String birthday = matchSingleDoCoMoPrefixedField("BDAY:", rawText, true);
if (birthday != null && !isStringOfDigits(birthday, 8)) {
if (!isStringOfDigits(birthday, 8)) {
// No reason to throw out the whole card because the birthday is formatted wrong.
birthday = null;
}

View file

@ -36,12 +36,8 @@ public final class ProductResultParser extends ResultParser {
return null;
}
String rawText = getMassagedText(result);
int length = rawText.length();
for (int x = 0; x < length; x++) {
char c = rawText.charAt(x);
if (c < '0' || c > '9') {
return null;
}
if (!isStringOfDigits(rawText, rawText.length())) {
return null;
}
// Not actually checking the checksum again here

View file

@ -62,8 +62,7 @@ public abstract class ResultParser {
new VINResultParser(),
};
private static final Pattern DIGITS = Pattern.compile("\\d*");
private static final Pattern ALPHANUM = Pattern.compile("[a-zA-Z0-9]*");
private static final Pattern DIGITS = Pattern.compile("\\d+");
private static final Pattern AMPERSAND = Pattern.compile("&");
private static final Pattern EQUALS = Pattern.compile("=");
private static final String BYTE_ORDER_MARK = "\ufeff";
@ -148,25 +147,17 @@ public abstract class ResultParser {
}
protected static boolean isStringOfDigits(CharSequence value, int length) {
return value != null && length == value.length() && DIGITS.matcher(value).matches();
return value != null && length > 0 && length == value.length() && DIGITS.matcher(value).matches();
}
protected static boolean isSubstringOfDigits(CharSequence value, int offset, int length) {
if (value == null) {
if (value == null || length <= 0) {
return false;
}
int max = offset + length;
return value.length() >= max && DIGITS.matcher(value.subSequence(offset, max)).matches();
}
protected static boolean isSubstringOfAlphaNumeric(CharSequence value, int offset, int length) {
if (value == null) {
return false;
}
int max = offset + length;
return value.length() >= max && ALPHANUM.matcher(value.subSequence(offset, max)).matches();
}
static Map<String,String> parseNameValuePairs(String uri) {
int paramStart = uri.indexOf('?');
if (paramStart < 0) {

View file

@ -80,19 +80,12 @@ public final class URIParsedResult extends ParsedResult {
}
private static boolean isColonFollowedByPortNumber(String uri, int protocolEnd) {
int nextSlash = uri.indexOf('/', protocolEnd + 1);
int start = protocolEnd + 1;
int nextSlash = uri.indexOf('/', start);
if (nextSlash < 0) {
nextSlash = uri.length();
}
if (nextSlash <= protocolEnd + 1) {
return false;
}
for (int x = protocolEnd + 1; x < nextSlash; x++) {
if (uri.charAt(x) < '0' || uri.charAt(x) > '9') {
return false;
}
}
return true;
return ResultParser.isSubstringOfDigits(uri, start, nextSlash - start);
}