Improve numeric sorting. No need to read lines from Scintilla, we already

have them in a vector.
This commit is contained in:
Andreas Jönsson 2015-05-09 11:25:07 +02:00
parent eee7c4f16e
commit 0eca4db949
4 changed files with 31 additions and 32 deletions

View File

@ -749,9 +749,9 @@ generic_string stringJoin(const std::vector<generic_string> &strings, const gene
return joined;
}
int stoi_CountNewlinesAsMinimum(const generic_string &input, const generic_string &newLine)
int stoi_CountEmptyLinesAsMinimum(const generic_string &input)
{
if (input.empty() || input == newLine)
if (input.empty())
{
return INT_MIN;
}
@ -759,4 +759,25 @@ int stoi_CountNewlinesAsMinimum(const generic_string &input, const generic_strin
{
return std::stoi(input);
}
}
bool allLinesAreNumericOrEmpty(const std::vector<generic_string> &lines)
{
const auto endit = lines.end();
for (auto it = lines.begin(); it != endit; ++it)
{
try
{
stoi_CountEmptyLinesAsMinimum(*it);
}
catch (std::invalid_argument&)
{
return false;
}
catch (std::out_of_range&)
{
return false;
}
}
return true;
}

View File

@ -190,6 +190,7 @@ generic_string stringToUpper(generic_string strToConvert);
generic_string stringReplace(generic_string subject, const generic_string& search, const generic_string& replace);
std::vector<generic_string> stringSplit(const generic_string& input, const generic_string &delimiter);
generic_string stringJoin(const std::vector<generic_string> &strings, const generic_string &separator);
int stoi_CountNewlinesAsMinimum(const generic_string &input, const generic_string &newLine);
int stoi_CountEmptyLinesAsMinimum(const generic_string &input);
bool allLinesAreNumericOrEmpty(const std::vector<generic_string> &lines);
#endif //M30_IDE_COMMUN_H

View File

@ -2958,27 +2958,6 @@ void ScintillaEditView::insertNewLineBelowCurrentLine()
execute(SCI_SETEMPTYSELECTION, execute(SCI_POSITIONFROMLINE, current_line + 1));
}
bool ScintillaEditView::allLinesAreNumeric(size_t fromLine, size_t toLine)
{
const generic_string newLine = getEOLString();
for (size_t i = fromLine; i <= toLine; ++i)
{
try
{
stoi_CountNewlinesAsMinimum(getLine(i), newLine);
}
catch (std::invalid_argument&)
{
return false;
}
catch (std::out_of_range&)
{
return false;
}
}
return true;
}
void ScintillaEditView::sortLines(size_t fromLine, size_t toLine, bool isDescending)
{
if (fromLine >= toLine)
@ -3000,16 +2979,15 @@ void ScintillaEditView::sortLines(size_t fromLine, size_t toLine, bool isDescend
}
}
assert(toLine - fromLine + 1 == splitText.size());
const bool isNumericSort = allLinesAreNumeric(fromLine, toLine);
const generic_string newLine = getEOLString();
std::sort(splitText.begin(), splitText.end(), [isDescending, isNumericSort, newLine](generic_string a, generic_string b)
const bool isNumericSort = allLinesAreNumericOrEmpty(splitText);
std::sort(splitText.begin(), splitText.end(), [isDescending, isNumericSort](generic_string a, generic_string b)
{
if (isDescending)
{
if (isNumericSort)
{
int numA = stoi_CountNewlinesAsMinimum(a, newLine);
int numB = stoi_CountNewlinesAsMinimum(b, newLine);
int numA = stoi_CountEmptyLinesAsMinimum(a);
int numB = stoi_CountEmptyLinesAsMinimum(b);
return numA > numB;
}
else
@ -3021,8 +2999,8 @@ void ScintillaEditView::sortLines(size_t fromLine, size_t toLine, bool isDescend
{
if (isNumericSort)
{
int numA = stoi_CountNewlinesAsMinimum(a, newLine);
int numB = stoi_CountNewlinesAsMinimum(b, newLine);
int numA = stoi_CountEmptyLinesAsMinimum(a);
int numB = stoi_CountEmptyLinesAsMinimum(b);
return numA < numB;
}
else

View File

@ -638,7 +638,6 @@ public:
void scrollPosToCenter(int pos);
generic_string getEOLString();
void sortLines(size_t fromLine, size_t toLine, bool isDescending);
bool allLinesAreNumeric(size_t fromLine, size_t toLine);
void changeTextDirection(bool isRTL);
bool isTextDirectionRTL() const;