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

51
iban.cc
View File

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

View File

@ -2,13 +2,14 @@
#include <string>
class Specification {
public:
public:
Specification(std::string countryCode, int length, std::string structure, std::string example) :
countryCode(countryCode),
length(length),
structure(structure),
example(example)
{};
example(example) {
};
Specification(std::string example) : example(example) {};
std::string countryCode;
int length;
std::string structure;

View File

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