From a73976f9409d96822b1b8fe1e0f7c8634ff5c588 Mon Sep 17 00:00:00 2001 From: Stefan Vigerske Date: Tue, 5 Apr 2022 16:10:59 +0200 Subject: [PATCH 1/2] avoid sscanf to parse into double or identifier - takes very long if input buffer is long --- extern/filereaderlp/reader.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/extern/filereaderlp/reader.cpp b/extern/filereaderlp/reader.cpp index d7e1493599..f7b601cee9 100644 --- a/extern/filereaderlp/reader.cpp +++ b/extern/filereaderlp/reader.cpp @@ -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(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(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(new RawStringToken(std::string(this->linebuffer, this->linebufferpos, endpos - this->linebufferpos)))); + this->linebufferpos = endpos; return; } From 0409b348013a348686de143326d891b79ba5d294 Mon Sep 17 00:00:00 2001 From: Stefan Vigerske Date: Tue, 5 Apr 2022 16:38:01 +0200 Subject: [PATCH 2/2] remove unused constants --- extern/filereaderlp/def.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/extern/filereaderlp/def.hpp b/extern/filereaderlp/def.hpp index 91e887220d..8783613278 100644 --- a/extern/filereaderlp/def.hpp +++ b/extern/filereaderlp/def.hpp @@ -10,9 +10,6 @@ void inline lpassert(bool condition) { } } -const unsigned int LP_MAX_NAME_LENGTH = 255; -const unsigned int LP_MAX_LINE_LENGTH = 560; - const std::string LP_KEYWORD_MIN[] = {"minimize", "min", "minimum"}; const std::string LP_KEYWORD_MAX[] = {"maximize", "max", "maximum"}; const std::string LP_KEYWORD_ST[] = {"subject to", "such that", "st", "s.t."};