From c827f6fe64ebb00ed834fab394d54605422edaee Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 4 Feb 2022 16:13:54 +0100 Subject: [PATCH] Fix expected alphanumeric comparison logic in string. --- core/string.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/string.cpp b/core/string.cpp index f6d8edf..20ad316 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -325,19 +325,21 @@ void String::replace(const String &find_str, const String &with, const int count } int String::compare(const String &other) const { + int cmp_size = MIN(size(), other.size()); + + for (int i = 0; i < cmp_size; ++i) { + if (_data[i] < other._data[i]) { + return 1; + } else if (_data[i] > other._data[i]) { + return 2; + } + } + if (size() < other.size()) { return 1; } else if (size() > other.size()) { return 2; } else { - for (int i = 0; i < _size; ++i) { - if (_data[i] < other._data[i]) { - return 1; - } else if (_data[i] > other._data[i]) { - return 2; - } - } - return 0; } }