-
Notifications
You must be signed in to change notification settings - Fork 67
/
Range.cpp
38 lines (35 loc) · 910 Bytes
/
Range.cpp
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
#include "stdafx.h"
#include "Range.h"
using namespace std;
Range Range::parse(const char *str)
{
size_t len = strlen(str);
int colons = 0;
for (int i = 0; i < len; i++) {
if (str[i] == ':') {
colons++;
} else if (!isdigit(str[i])) {
fprintf(stderr, "Invalid format: %s\n", str);
exit(1);
}
}
int start, end, step;
if (colons == 0) {
sscanf(str, "%d", &start);
end = start;
step = 1;
} else if (colons == 1) {
sscanf(str, "%d:%d", &start, &end);
step = 1;
} else if (colons == 2) {
sscanf(str, "%d:%d:%d", &start, &step, &end);
} else {
fprintf(stderr, "Invalid format: %s\n", str);
exit(1);
}
if (step < 1 || end < start) {
fprintf(stderr, "Invalid range: %s\n", str);
exit(1);
}
return Range(start, end, step);
}