Sort numerically (not lexicographically) if all lines are integers.

This commit is contained in:
Andreas Jönsson 2015-05-09 11:02:58 +02:00
parent 6e84be21f4
commit eee7c4f16e
4 changed files with 70 additions and 3 deletions

View File

@ -747,4 +747,16 @@ generic_string stringJoin(const std::vector<generic_string> &strings, const gene
}
}
return joined;
}
int stoi_CountNewlinesAsMinimum(const generic_string &input, const generic_string &newLine)
{
if (input.empty() || input == newLine)
{
return INT_MIN;
}
else
{
return std::stoi(input);
}
}

View File

@ -190,5 +190,6 @@ 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);
#endif //M30_IDE_COMMUN_H

View File

@ -1922,6 +1922,17 @@ void ScintillaEditView::getLine(int lineNumber, TCHAR * line, int lineBufferLen)
delete [] lineA;
}
generic_string ScintillaEditView::getLine(int lineNumber)
{
int lineLen = execute(SCI_LINELENGTH, lineNumber);
const int bufSize = 1 + lineLen;
_TCHAR *buf = new _TCHAR[bufSize];
getLine(lineNumber, buf, bufSize);
generic_string text = buf;
delete[] buf;
return text;
}
void ScintillaEditView::addText(int length, const char *buf)
{
execute(SCI_ADDTEXT, length, (LPARAM)buf);
@ -2947,6 +2958,27 @@ 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)
@ -2968,15 +3000,35 @@ void ScintillaEditView::sortLines(size_t fromLine, size_t toLine, bool isDescend
}
}
assert(toLine - fromLine + 1 == splitText.size());
std::sort(splitText.begin(), splitText.end(), [isDescending](generic_string a, generic_string b)
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)
{
if (isDescending)
{
return a.compare(b) > 0;
if (isNumericSort)
{
int numA = stoi_CountNewlinesAsMinimum(a, newLine);
int numB = stoi_CountNewlinesAsMinimum(b, newLine);
return numA > numB;
}
else
{
return a.compare(b) > 0;
}
}
else
{
return a.compare(b) < 0;
if (isNumericSort)
{
int numA = stoi_CountNewlinesAsMinimum(a, newLine);
int numB = stoi_CountNewlinesAsMinimum(b, newLine);
return numA < numB;
}
else
{
return a.compare(b) < 0;
}
}
});
const generic_string joined = stringJoin(splitText, getEOLString());

View File

@ -271,6 +271,7 @@ public:
void showAutoComletion(int lenEntered, const TCHAR * list);
void showCallTip(int startPos, const TCHAR * def);
void getLine(int lineNumber, TCHAR * line, int lineBufferLen);
generic_string getLine(int lineNumber);
void addText(int length, const char *buf);
void insertNewLineAboveCurrentLine();
@ -637,6 +638,7 @@ 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;