Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem_setting: speed up int validation #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions sample_files/problem_setting/examples/identical_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,28 @@ std::string readToken(char min_char = 0, char max_char = 127) {
return token;
}

bool validateInt(const std::string &token) {
if (token == "0") {
return true;
}
auto i = 0u;
if (i < token.size() && token[i] == '-') {
++i;
}
if (i >= token.size() || token[i] < '1' || '9' < token[i]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should explicitly note that -0 is not a valid int, just in case some language considers it separate from 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caveat is already in the documentation for readInt, should we add it here as a comment as well? I think the documentation is likely sufficient.

return false;
}
for (i++; i < token.size(); i++) {
if (token[i] < '0' || '9' < token[i]) {
return false;
}
}
return true;
}

long long readInt(long long lo, long long hi) {
static regex_t re = regex_helpers::compile("^(0|-?[1-9][0-9]*)$");
std::string token = readToken();
assertWA(regex_helpers::match(re, token));
assertWA(validateInt(token));

long long parsedInt;
try {
Expand Down
22 changes: 20 additions & 2 deletions sample_files/problem_setting/examples/standard_interactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,28 @@ std::string readToken(char min_char = 0, char max_char = 127) {
return token;
}

bool validateInt(const std::string &token) {
if (token == "0") {
return true;
}
auto i = 0u;
if (i < token.size() && token[i] == '-') {
++i;
}
if (i >= token.size() || token[i] < '1' || '9' < token[i]) {
return false;
}
for (i++; i < token.size(); i++) {
if (token[i] < '0' || '9' < token[i]) {
return false;
}
}
return true;
}

long long readInt(long long lo, long long hi) {
static regex_t re = regex_helpers::compile("^(0|-?[1-9][0-9]*)$");
std::string token = readToken();
assertWA(regex_helpers::match(re, token));
assertWA(validateInt(token));

long long parsedInt;
try {
Expand Down
22 changes: 20 additions & 2 deletions sample_files/problem_setting/examples/validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,28 @@ std::string readToken(char min_char = 0, char max_char = 127) {
return token;
}

bool validateInt(const std::string &token) {
if (token == "0") {
return true;
}
auto i = 0u;
if (i < token.size() && token[i] == '-') {
++i;
}
if (i >= token.size() || token[i] < '1' || '9' < token[i]) {
return false;
}
for (i++; i < token.size(); i++) {
if (token[i] < '0' || '9' < token[i]) {
return false;
}
}
return true;
}

long long readInt(long long lo, long long hi) {
static regex_t re = regex_helpers::compile("^(0|-?[1-9][0-9]*)$");
std::string token = readToken();
assert(regex_helpers::match(re, token));
assert(validateInt(token));

long long parsedInt = stoll(token); // May throw.
assert(lo <= parsedInt && parsedInt <= hi);
Expand Down
22 changes: 20 additions & 2 deletions sample_files/problem_setting/identical_checker_interactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,28 @@ std::string readLine(char min_char = 0, char max_char = 127) {
return line;
}

bool validateInt(const std::string &token) {
if (token == "0") {
return true;
}
auto i = 0u;
if (i < token.size() && token[i] == '-') {
++i;
}
if (i >= token.size() || token[i] < '1' || '9' < token[i]) {
return false;
}
for (i++; i < token.size(); i++) {
if (token[i] < '0' || '9' < token[i]) {
return false;
}
}
return true;
}

long long readInt(long long lo, long long hi) {
static regex_t re = regex_helpers::compile("^(0|-?[1-9][0-9]*)$");
std::string token = readToken();
assertWA(regex_helpers::match(re, token));
assertWA(validateInt(token));

long long parsedInt;
try {
Expand Down
22 changes: 20 additions & 2 deletions sample_files/problem_setting/standard_checker_interactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,28 @@ std::string readToken(char min_char = 0, char max_char = 127) {
return token;
}

bool validateInt(const std::string &token) {
if (token == "0") {
return true;
}
auto i = 0u;
if (i < token.size() && token[i] == '-') {
++i;
}
if (i >= token.size() || token[i] < '1' || '9' < token[i]) {
return false;
}
for (i++; i < token.size(); i++) {
if (token[i] < '0' || '9' < token[i]) {
return false;
}
}
return true;
}

long long readInt(long long lo, long long hi) {
static regex_t re = regex_helpers::compile("^(0|-?[1-9][0-9]*)$");
std::string token = readToken();
assertWA(regex_helpers::match(re, token));
assertWA(validateInt(token));

long long parsedInt;
try {
Expand Down
22 changes: 20 additions & 2 deletions sample_files/problem_setting/validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,28 @@ std::string readLine(char min_char = 0, char max_char = 127) {
return line;
}

bool validateInt(const std::string &token) {
if (token == "0") {
return true;
}
auto i = 0u;
if (i < token.size() && token[i] == '-') {
++i;
}
if (i >= token.size() || token[i] < '1' || '9' < token[i]) {
return false;
}
for (i++; i < token.size(); i++) {
if (token[i] < '0' || '9' < token[i]) {
return false;
}
}
return true;
}

long long readInt(long long lo, long long hi) {
static regex_t re = regex_helpers::compile("^(0|-?[1-9][0-9]*)$");
std::string token = readToken();
assert(regex_helpers::match(re, token));
assert(validateInt(token));

long long parsedInt = stoll(token); // May throw.
assert(lo <= parsedInt && parsedInt <= hi);
Expand Down
Loading