Repository navigation
Expand file tree
/
Copy pathfiscal_code.cpp
More file actions
262 lines (231 loc) · 8.97 KB
/
Copy pathfiscal_code.cpp
File metadata and controls
262 lines (231 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Italian Fiscal Code (codice fiscale) — C++ implementation.
//
// Reads one JSON object from stdin and writes the result to stdout,
// following SPECIFICATION.md. The C++ standard library has no JSON
// support, so a minimal parser is included.
#include <array>
#include <iostream>
#include <iterator>
#include <map>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
namespace {
struct InputError : std::runtime_error {
explicit InputError(const std::string &code) : std::runtime_error(code) {}
};
// A JSON value reduced to what the specification needs: either a string
// or "something else".
using Value = std::optional<std::string>;
class JsonParser {
public:
explicit JsonParser(std::string_view text) : text_(text) {}
// Parses the whole document, which must be a single object.
std::map<std::string, Value> parseDocument() {
skipWhitespace();
if (peek() != '{') fail();
std::map<std::string, Value> members;
parseObject(&members);
skipWhitespace();
if (pos_ != text_.size()) fail();
return members;
}
private:
std::string_view text_;
std::size_t pos_ = 0;
[[noreturn]] static void fail() { throw InputError("invalid_json"); }
char peek() const { return pos_ < text_.size() ? text_[pos_] : '\0'; }
void expect(char c) {
if (peek() != c) fail();
++pos_;
}
void skipWhitespace() {
while (peek() == ' ' || peek() == '\t' || peek() == '\n' || peek() == '\r') ++pos_;
}
Value parseValue() {
skipWhitespace();
switch (peek()) {
case '"': return parseString();
case '{': parseObject(nullptr); return std::nullopt;
case '[': parseArray(); return std::nullopt;
case 't': parseLiteral("true"); return std::nullopt;
case 'f': parseLiteral("false"); return std::nullopt;
case 'n': parseLiteral("null"); return std::nullopt;
default: parseNumber(); return std::nullopt;
}
}
void parseObject(std::map<std::string, Value> *members) {
expect('{');
skipWhitespace();
if (peek() == '}') { ++pos_; return; }
while (true) {
skipWhitespace();
if (peek() != '"') fail();
std::string key = parseString();
skipWhitespace();
expect(':');
Value value = parseValue();
if (members) (*members)[key] = std::move(value);
skipWhitespace();
if (peek() == ',') { ++pos_; continue; }
expect('}');
return;
}
}
void parseArray() {
expect('[');
skipWhitespace();
if (peek() == ']') { ++pos_; return; }
while (true) {
parseValue();
skipWhitespace();
if (peek() == ',') { ++pos_; continue; }
expect(']');
return;
}
}
void parseLiteral(std::string_view word) {
if (text_.substr(pos_, word.size()) != word) fail();
pos_ += word.size();
}
void digits() {
if (!isDigit(peek())) fail();
while (isDigit(peek())) ++pos_;
}
void parseNumber() {
if (peek() == '-') ++pos_;
if (peek() == '0') ++pos_; else digits();
if (peek() == '.') { ++pos_; digits(); }
if (peek() == 'e' || peek() == 'E') {
++pos_;
if (peek() == '+' || peek() == '-') ++pos_;
digits();
}
}
// Non-ASCII characters are kept as raw bytes (>= 0x80); names reject them.
std::string parseString() {
expect('"');
std::string out;
while (true) {
if (pos_ >= text_.size()) fail();
auto c = static_cast<unsigned char>(text_[pos_++]);
if (c == '"') return out;
if (c < 0x20) fail();
if (c != '\\') { out += static_cast<char>(c); continue; }
if (pos_ >= text_.size()) fail();
char e = text_[pos_++];
switch (e) {
case '"': case '\\': case '/': out += e; break;
case 'b': out += '\b'; break;
case 'f': out += '\f'; break;
case 'n': out += '\n'; break;
case 'r': out += '\r'; break;
case 't': out += '\t'; break;
case 'u': {
if (pos_ + 4 > text_.size()) fail();
int code = 0;
for (int k = 0; k < 4; ++k) {
char h = text_[pos_++];
int v = isDigit(h) ? h - '0'
: (h >= 'a' && h <= 'f') ? h - 'a' + 10
: (h >= 'A' && h <= 'F') ? h - 'A' + 10 : -1;
if (v < 0) fail();
code = code * 16 + v;
}
out += static_cast<char>(code >= 0x20 && code < 0x80 ? code : 0x80);
break;
}
default: fail();
}
}
}
public:
static bool isDigit(char c) { return c >= '0' && c <= '9'; }
};
constexpr std::string_view kVowels = "AEIOU";
constexpr std::string_view kMonthCodes = "ABCDEHLMPRST";
constexpr std::array<int, 36> kOddValues = {
1, 0, 5, 7, 9, 13, 15, 17, 19, 21,
1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3,
6, 8, 12, 14, 16, 10, 22, 25, 24, 23};
bool isVowel(char c) { return kVowels.find(c) != std::string_view::npos; }
std::string normalizeName(const Value &value, const std::string &error) {
if (!value) throw InputError(error);
std::string letters;
for (char c : *value) {
if (c >= 'A' && c <= 'Z') letters += c;
else if (c >= 'a' && c <= 'z') letters += static_cast<char>(c - 'a' + 'A');
else if (c != ' ' && c != '\'' && c != '-') throw InputError(error);
}
if (letters.empty()) throw InputError(error);
return letters;
}
std::string encodePart(const std::string &text, bool isName) {
std::string consonants, vowels;
for (char c : text) (isVowel(c) ? vowels : consonants) += c;
if (isName && consonants.size() >= 4) {
return {consonants[0], consonants[2], consonants[3]};
}
return (consonants + vowels + "XXX").substr(0, 3);
}
struct Date { int year, month, day; };
Date parseDate(const Value &value) {
const InputError error("invalid_birth_date");
if (!value || value->size() != 10) throw error;
const std::string &s = *value;
for (std::size_t k = 0; k < s.size(); ++k) {
bool dash = k == 4 || k == 7;
if (dash ? s[k] != '-' : !JsonParser::isDigit(s[k])) throw error;
}
Date d{std::stoi(s.substr(0, 4)), std::stoi(s.substr(5, 2)), std::stoi(s.substr(8, 2))};
bool leap = (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0;
const std::array<int, 12> days = {31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (d.year < 1 || d.month < 1 || d.month > 12 || d.day < 1 || d.day > days[d.month - 1]) {
throw error;
}
return d;
}
char checkCharacter(const std::string &partial) {
int sum = 0;
for (std::size_t k = 0; k < partial.size(); ++k) {
char c = partial[k];
bool digit = JsonParser::isDigit(c);
if (k % 2 == 0) sum += kOddValues[digit ? c - '0' : c - 'A' + 10];
else sum += digit ? c - '0' : c - 'A';
}
return static_cast<char>('A' + sum % 26);
}
std::string twoDigits(int n) { return (n < 10 ? "0" : "") + std::to_string(n); }
std::string fiscalCode(const std::map<std::string, Value> &input) {
for (const char *field : {"surname", "name", "birth_date", "sex", "birthplace_code"}) {
if (input.find(field) == input.end()) throw InputError("missing_field");
}
std::string surname = normalizeName(input.at("surname"), "invalid_surname");
std::string name = normalizeName(input.at("name"), "invalid_name");
Date date = parseDate(input.at("birth_date"));
const Value &sex = input.at("sex");
if (!sex || (*sex != "M" && *sex != "F")) throw InputError("invalid_sex");
const Value &place = input.at("birthplace_code");
if (!place || place->size() != 4 || !((*place)[0] >= 'A' && (*place)[0] <= 'Z') ||
!JsonParser::isDigit((*place)[1]) || !JsonParser::isDigit((*place)[2]) ||
!JsonParser::isDigit((*place)[3])) {
throw InputError("invalid_birthplace_code");
}
std::string partial = encodePart(surname, false) + encodePart(name, true) +
twoDigits(date.year % 100) + kMonthCodes[date.month - 1] +
twoDigits(*sex == "F" ? date.day + 40 : date.day) + *place;
return partial + checkCharacter(partial);
}
} // namespace
int main() {
std::string text{std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>()};
try {
std::string code = fiscalCode(JsonParser(text).parseDocument());
std::cout << "{\"fiscal_code\":\"" << code << "\"}\n";
return 0;
} catch (const InputError &e) {
std::cout << "{\"error\":\"" << e.what() << "\"}\n";
return 1;
}
}