tests: fix bugs and inefficiencies in PrepareSavefile.

pull/97/head
whitequark 2016-11-02 03:43:29 +00:00
parent 505f503cc3
commit 0e72c606ab
1 changed files with 23 additions and 15 deletions

View File

@ -102,17 +102,22 @@ static std::string PrepareSavefile(std::string data) {
// Round everything to 2**30 ~ 1e9 // Round everything to 2**30 ~ 1e9
const double precision = pow(2, 30); const double precision = pow(2, 30);
size_t newline = 0; size_t lineBegin = 0;
while(newline < std::string::npos) { while(lineBegin < data.length()) {
size_t nextNewline = data.find('\n', newline + 1); size_t nextLineBegin = data.find('\n', lineBegin);
if(nextLineBegin == std::string::npos) {
nextLineBegin = data.length();
} else {
nextLineBegin++;
}
size_t eqPos = data.find('=', newline + 1); size_t eqPos = data.find('=', lineBegin);
if(eqPos < nextNewline) { if(eqPos < nextLineBegin) {
std::string key = data.substr(newline + 1, eqPos - newline - 1), std::string key = data.substr(lineBegin, eqPos - lineBegin),
value = data.substr(eqPos + 1, nextNewline - eqPos - 1); value = data.substr(eqPos + 1, nextLineBegin - eqPos - 2);
for(int i = 0; SolveSpaceUI::SAVED[i].type != 0; i++) { for(int i = 0; SolveSpaceUI::SAVED[i].type != 0; i++) {
if(SolveSpaceUI::SAVED[i].desc != key) continue;
if(SolveSpaceUI::SAVED[i].fmt != 'f') continue; if(SolveSpaceUI::SAVED[i].fmt != 'f') continue;
if(SolveSpaceUI::SAVED[i].desc != key) continue;
double f = strtod(value.c_str(), NULL); double f = strtod(value.c_str(), NULL);
f = round(f * precision) / precision; f = round(f * precision) / precision;
std::string newValue = ssprintf("%.20f", f); std::string newValue = ssprintf("%.20f", f);
@ -122,16 +127,19 @@ static std::string PrepareSavefile(std::string data) {
} }
} }
size_t spPos = data.find(' ', newline + 1); size_t spPos = data.find(' ', lineBegin);
if(spPos < nextNewline) { if(spPos < nextLineBegin) {
std::string cmd = data.substr(newline + 1, spPos - newline - 1); std::string cmd = data.substr(lineBegin, spPos - lineBegin);
if(cmd == "Surface" || cmd == "SCtrl" || cmd == "TrimBy" || cmd == "AddSurface" || if(!cmd.empty()) {
cmd == "Curve" || cmd == "CCtrl" || cmd == "CurvePt" || cmd == "AddCurve") { if(cmd == "Surface" || cmd == "SCtrl" || cmd == "TrimBy" ||
data.replace(newline + 1, nextNewline, nextNewline - newline - 1, ' '); cmd == "Curve" || cmd == "CCtrl" || cmd == "CurvePt") {
data.erase(lineBegin, nextLineBegin - lineBegin);
nextLineBegin = lineBegin;
}
} }
} }
newline = nextNewline; lineBegin = nextLineBegin;
} }
return data; return data;
} }