From 83de4a9da26229872dc2fff4eb75ed11e30e6e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20J=C3=B6nsson?= Date: Mon, 11 May 2015 09:26:31 +0200 Subject: [PATCH] Use long long in numeric sort (ie 64 bit numbers). --- PowerEditor/src/MISC/Common/Common.cpp | 18 +++++++++--------- PowerEditor/src/MISC/Common/Common.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index e1e7c663..96d97eb2 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -749,7 +749,7 @@ generic_string stringJoin(const std::vector& strings, const gene return joined; } -int stoiStrict(const generic_string& input) +long long stollStrict(const generic_string& input) { if (input.empty()) { @@ -774,7 +774,7 @@ int stoiStrict(const generic_string& input) throw std::invalid_argument("Invalid character found."); } - return std::stoi(input); + return std::stoll(input); } } @@ -786,7 +786,7 @@ bool allLinesAreNumericOrEmpty(const std::vector& lines) { if (!line.empty()) { - stoiStrict(line); + stollStrict(line); } } catch (std::invalid_argument&) @@ -833,9 +833,9 @@ std::vector numericSort(std::vector input, bool { // Pre-condition: all strings in "input" are either empty or convertible to int with stoiStrict. // Note that empty lines are filtered out and added back manually to the output at the end. - std::vector nonEmptyinputAsInts; + std::vector nonEmptyInputAsNumbers; size_t nofEmptyLines = 0; - nonEmptyinputAsInts.reserve(input.size()); + nonEmptyInputAsNumbers.reserve(input.size()); for (const generic_string& line : input) { if (line.empty()) @@ -844,11 +844,11 @@ std::vector numericSort(std::vector input, bool } else { - nonEmptyinputAsInts.push_back(stoiStrict(line)); + nonEmptyInputAsNumbers.push_back(stollStrict(line)); } } assert(nonEmptyinputAsInts.size() + nofEmptyLines == input.size()); - std::sort(nonEmptyinputAsInts.begin(), nonEmptyinputAsInts.end(), [isDescending](int a, int b) + std::sort(nonEmptyInputAsNumbers.begin(), nonEmptyInputAsNumbers.end(), [isDescending](long long a, long long b) { if (isDescending) { @@ -866,9 +866,9 @@ std::vector numericSort(std::vector input, bool { output.insert(output.end(), empties.begin(), empties.end()); } - for (const int& sortedInt : nonEmptyinputAsInts) + for (const long long& sortedNumber : nonEmptyInputAsNumbers) { - output.push_back(std::to_wstring(sortedInt)); + output.push_back(std::to_wstring(sortedNumber)); } if (isDescending) { diff --git a/PowerEditor/src/MISC/Common/Common.h b/PowerEditor/src/MISC/Common/Common.h index a86e6329..2e90fea6 100644 --- a/PowerEditor/src/MISC/Common/Common.h +++ b/PowerEditor/src/MISC/Common/Common.h @@ -190,7 +190,7 @@ generic_string stringToUpper(generic_string strToConvert); generic_string stringReplace(generic_string subject, const generic_string& search, const generic_string& replace); std::vector stringSplit(const generic_string& input, const generic_string& delimiter); generic_string stringJoin(const std::vector& strings, const generic_string& separator); -int stoiStrict(const generic_string& input); +long long stollStrict(const generic_string& input); bool allLinesAreNumericOrEmpty(const std::vector& lines); std::vector repeatString(const generic_string& text, const size_t count);