mirror of
https://github.com/notepad-plus-plus/notepad-plus-plus.git
synced 2025-07-28 16:24:27 +02:00
Improve numeric sorting. No need to read lines from Scintilla, we already
have them in a vector.
This commit is contained in:
parent
eee7c4f16e
commit
0eca4db949
@ -749,9 +749,9 @@ generic_string stringJoin(const std::vector<generic_string> &strings, const gene
|
|||||||
return joined;
|
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;
|
return INT_MIN;
|
||||||
}
|
}
|
||||||
@ -760,3 +760,24 @@ int stoi_CountNewlinesAsMinimum(const generic_string &input, const generic_strin
|
|||||||
return std::stoi(input);
|
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;
|
||||||
|
}
|
@ -190,6 +190,7 @@ generic_string stringToUpper(generic_string strToConvert);
|
|||||||
generic_string stringReplace(generic_string subject, const generic_string& search, const generic_string& replace);
|
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);
|
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);
|
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
|
#endif //M30_IDE_COMMUN_H
|
||||||
|
@ -2958,27 +2958,6 @@ void ScintillaEditView::insertNewLineBelowCurrentLine()
|
|||||||
execute(SCI_SETEMPTYSELECTION, execute(SCI_POSITIONFROMLINE, current_line + 1));
|
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)
|
void ScintillaEditView::sortLines(size_t fromLine, size_t toLine, bool isDescending)
|
||||||
{
|
{
|
||||||
if (fromLine >= toLine)
|
if (fromLine >= toLine)
|
||||||
@ -3000,16 +2979,15 @@ void ScintillaEditView::sortLines(size_t fromLine, size_t toLine, bool isDescend
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert(toLine - fromLine + 1 == splitText.size());
|
assert(toLine - fromLine + 1 == splitText.size());
|
||||||
const bool isNumericSort = allLinesAreNumeric(fromLine, toLine);
|
const bool isNumericSort = allLinesAreNumericOrEmpty(splitText);
|
||||||
const generic_string newLine = getEOLString();
|
std::sort(splitText.begin(), splitText.end(), [isDescending, isNumericSort](generic_string a, generic_string b)
|
||||||
std::sort(splitText.begin(), splitText.end(), [isDescending, isNumericSort, newLine](generic_string a, generic_string b)
|
|
||||||
{
|
{
|
||||||
if (isDescending)
|
if (isDescending)
|
||||||
{
|
{
|
||||||
if (isNumericSort)
|
if (isNumericSort)
|
||||||
{
|
{
|
||||||
int numA = stoi_CountNewlinesAsMinimum(a, newLine);
|
int numA = stoi_CountEmptyLinesAsMinimum(a);
|
||||||
int numB = stoi_CountNewlinesAsMinimum(b, newLine);
|
int numB = stoi_CountEmptyLinesAsMinimum(b);
|
||||||
return numA > numB;
|
return numA > numB;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -3021,8 +2999,8 @@ void ScintillaEditView::sortLines(size_t fromLine, size_t toLine, bool isDescend
|
|||||||
{
|
{
|
||||||
if (isNumericSort)
|
if (isNumericSort)
|
||||||
{
|
{
|
||||||
int numA = stoi_CountNewlinesAsMinimum(a, newLine);
|
int numA = stoi_CountEmptyLinesAsMinimum(a);
|
||||||
int numB = stoi_CountNewlinesAsMinimum(b, newLine);
|
int numB = stoi_CountEmptyLinesAsMinimum(b);
|
||||||
return numA < numB;
|
return numA < numB;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -638,7 +638,6 @@ public:
|
|||||||
void scrollPosToCenter(int pos);
|
void scrollPosToCenter(int pos);
|
||||||
generic_string getEOLString();
|
generic_string getEOLString();
|
||||||
void sortLines(size_t fromLine, size_t toLine, bool isDescending);
|
void sortLines(size_t fromLine, size_t toLine, bool isDescending);
|
||||||
bool allLinesAreNumeric(size_t fromLine, size_t toLine);
|
|
||||||
void changeTextDirection(bool isRTL);
|
void changeTextDirection(bool isRTL);
|
||||||
bool isTextDirectionRTL() const;
|
bool isTextDirectionRTL() const;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user