Remove unnecessary logic

This commit is contained in:
yorickdewid 2016-03-01 20:36:43 +01:00
parent 8601bb11c3
commit 316d44be81
4 changed files with 28 additions and 34 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
iban.so

41
iban.cc
View File

@ -104,30 +104,29 @@ std::regex parseStructure(std::string structure) {
return std::regex(regex.c_str()); return std::regex(regex.c_str());
} }
void Validate::setSelectedSpecification(std::string countryCode) {
this->selectedSpec = this->specifications[countryCode];
}
bool Validate::isValid(std::string arg) { bool Validate::isValid(std::string arg) {
Specification* spec = new Specification("", 0, "", arg); Specification* spec = new Specification(arg);
if (!this->selectedSpec) {
std::transform(spec->example.begin(), spec->example.end(), spec->example.begin(), toupper); /* Convert uppercase */
std::transform(spec->example.begin(), spec->example.end(),
spec->example.begin(), toupper);
/* Match on country */
spec->countryCode = spec->example.substr(0, 2); spec->countryCode = spec->example.substr(0, 2);
spec->length = spec->example.length(); spec->length = spec->example.length();
setSelectedSpecification(spec->countryCode); Specification* specFound = this->specifications[spec->countryCode];
} if (!specFound)
if (!(this->selectedSpec == nullptr)) { return false;
/* Test accountnumber */
std::string shortened = spec->example.substr(4, spec->example.length()); std::string shortened = spec->example.substr(4, spec->example.length());
bool result = this->selectedSpec->length == spec->length bool result = specFound->length == spec->length
&& this->selectedSpec->countryCode.compare(spec->countryCode) == 0 && specFound->countryCode.compare(spec->countryCode) == 0
&& std::regex_match(shortened, parseStructure(this->selectedSpec->structure)) && std::regex_match(shortened, parseStructure(specFound->structure))
&& iso7064Mod97_10(spec->example); && iso7064Mod97_10(spec->example);
delete spec; delete spec;
return result; return result;
} else {
delete spec;
return false;
}
} }
Validate::~Validate() { Validate::~Validate() {
@ -399,13 +398,7 @@ bool account_validate(text *iban) {
Validate val; Validate val;
ciban = text_to_cstring(iban); ciban = text_to_cstring(iban);
elog(DEBUG1, "Evaluating '%s'", ciban); return val.isValid(std::string(ciban));
bool r = val.isValid(std::string(ciban));
elog(DEBUG1, "Result %d", r);
return r;
} }
} }

View File

@ -7,8 +7,9 @@ public:
countryCode(countryCode), countryCode(countryCode),
length(length), length(length),
structure(structure), structure(structure),
example(example) example(example) {
{}; };
Specification(std::string example) : example(example) {};
std::string countryCode; std::string countryCode;
int length; int length;
std::string structure; std::string structure;

View File

@ -9,8 +9,6 @@ class Validate {
~Validate(); ~Validate();
bool isValid(std::string arg); bool isValid(std::string arg);
void addSpecification(Specification* specPtr); void addSpecification(Specification* specPtr);
void setSelectedSpecification(std::string countryCode);
std::map<std::string, Specification*> specifications; std::map<std::string, Specification*> specifications;
Specification* selectedSpec;
}; };