Skip to content

Commit

Permalink
avoid sscanf to parse into double or identifier
Browse files Browse the repository at this point in the history
- takes very long if input buffer is long
  • Loading branch information
svigerske committed Apr 5, 2022
1 parent 2d69749 commit a73976f
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions extern/filereaderlp/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,24 +934,24 @@ void Reader::readnexttoken(bool& done) {
assert(this->linebufferpos == this->linebuffer.size());
return;
}

// check for double value
double constant;
int ncharconsumed;
int nread = sscanf(this->linebuffer.data()+this->linebufferpos, "%lf%n", &constant, &ncharconsumed);
if (nread == 1) {
const char* startptr = this->linebuffer.data()+this->linebufferpos;
char* endptr;
double constant = strtod(startptr, &endptr);
if (endptr != startptr) {
this->rawtokens.push_back(std::unique_ptr<RawToken>(new RawConstantToken(constant)));
this->linebufferpos += ncharconsumed;
this->linebufferpos += endptr - startptr;
return;
}

// assume it's an (section/variable/constraint) identifier
char stringbuffer[LP_MAX_NAME_LENGTH+1];
nread = sscanf(this->linebuffer.data()+this->linebufferpos, "%[^][\t\n\\:+<>^= /-]%n",
stringbuffer, &ncharconsumed);
if (nread == 1) {
this->rawtokens.push_back(std::unique_ptr<RawToken>(new RawStringToken(stringbuffer)));
this->linebufferpos += ncharconsumed;
auto endpos = this->linebuffer.find_first_of("\t\n\\:+<>^= /-", this->linebufferpos);
if( endpos == std::string::npos )
endpos = this->linebuffer.size(); // take complete rest of string
if( endpos > this->linebufferpos ) {
this->rawtokens.push_back(std::unique_ptr<RawToken>(new RawStringToken(std::string(this->linebuffer, this->linebufferpos, endpos - this->linebufferpos))));
this->linebufferpos = endpos;
return;
}

Expand Down

0 comments on commit a73976f

Please sign in to comment.