Skip to content

Commit

Permalink
strip trailing < from strings
Browse files Browse the repository at this point in the history
  • Loading branch information
patrit committed Jul 14, 2021
1 parent 64a6f88 commit 21b5f88
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Mrz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void Mrz::toJSON(ostream &os) const {
check.set("is_valid", val.isValid());
ret.set(fieldname, check);
} else {
ret.set(fieldname, val.getValue());
ret.set(fieldname, MrzFields::removeTrailingChar(val.getValue()));
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/MrzFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <unordered_map>

using namespace std;

namespace {
std::unordered_map<Field, std::string> _map = {
{MrzType, "mrz_type"},
Expand Down Expand Up @@ -34,15 +36,23 @@ std::unordered_map<Field, std::string> _map = {
};
};

std::unordered_map<Field, std::string> const &MrzFields::getMap() {
unordered_map<Field, string> const &MrzFields::getMap() {
return _map;
}

std::string const &MrzFields::toName(Field field) {
string const &MrzFields::toName(Field field) {
static std::string _empty;
auto it = _map.find(field);
if (it == _map.end()) {
return _empty;
}
return it->second;
}

string MrzFields::removeTrailingChar(string const& val) {
size_t idx = val.find_last_not_of('<');
if (idx != string::npos) {
return val.substr(0, idx + 1);
}
return string();
}
1 change: 1 addition & 0 deletions src/MrzFields.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MrzFields {

static std::unordered_map<Field, std::string> const &getMap();
static std::string const &toName(Field field);
static std::string removeTrailingChar(std::string const& val);
};

#endif
6 changes: 3 additions & 3 deletions test/mrz_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def test_valid_TD1(self):
ret = resp.json()
assert "TD1" == ret["mrz_type"]
assert "ID" == ret["type"]
assert "<" == ret["sex"]
assert "" == ret["sex"]
assert "ERIKA PAULA ANNA" == ret["names"]
assert "MUSTERMANN" == ret["surname"]
assert "T22000129" == ret["number"]
assert "D<<" == ret["country"]
assert "D<<" == ret["nationality"]
assert "D" == ret["country"]
assert "D" == ret["nationality"]
assert "640812" == ret["date_of_birth"]
assert "201031" == ret["expiration_date"]

Expand Down

0 comments on commit 21b5f88

Please sign in to comment.