-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
valid_ip_addresses.cc
56 lines (48 loc) · 1.52 KB
/
valid_ip_addresses.cc
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
#include <algorithm>
#include <string>
#include <vector>
#include "test_framework/generic_test.h"
using std::stoi;
using std::string;
using std::vector;
bool IsValidPart(const string& s);
vector<string> GetValidIpAddress(const string& s) {
vector<string> result;
for (size_t i = 1; i < 4 && i < size(s); ++i) {
if (const string first = s.substr(0, i); IsValidPart(first)) {
for (size_t j = 1; i + j < size(s) && j < 4; ++j) {
const string second = s.substr(i, j);
if (IsValidPart(second)) {
for (size_t k = 1; i + j + k < size(s) && k < 4; ++k) {
const string third = s.substr(i + j, k),
fourth = s.substr(i + j + k);
if (IsValidPart(third) && IsValidPart(fourth)) {
result.emplace_back(first + "." + second + "." + third + "." +
fourth);
}
}
}
}
}
}
return result;
}
bool IsValidPart(const string& s) {
if (size(s) > 3) {
return false;
}
// "00", "000", "01", etc. are not valid, but "0" is valid.
if (s.front() == '0' && size(s) > 1) {
return false;
}
int val = stoi(s);
return val <= 255 && val >= 0;
}
// clang-format off
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"s"};
return GenericTestMain(args, "valid_ip_addresses.cc", "valid_ip_addresses.tsv", &GetValidIpAddress,
UnorderedComparator{}, param_names);
}
// clang-format on