From 9522a0b7c16414fce2103cf58bfdd63aaf0cb01b Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Thu, 3 Nov 2022 14:58:32 -0400 Subject: DRTVWR-575: Fix llcommon assumptions that size_t fits in 4 bytes. It's a little distressing how often we have historically coded S32 or U32 to pass a length or index. There are more such assumptions in other viewer subdirectories, but this is a start. --- indra/llcommon/llkeybind.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'indra/llcommon/llkeybind.cpp') diff --git a/indra/llcommon/llkeybind.cpp b/indra/llcommon/llkeybind.cpp index 38696c2258..f47ba41bae 100644 --- a/indra/llcommon/llkeybind.cpp +++ b/indra/llcommon/llkeybind.cpp @@ -180,10 +180,10 @@ LLKeyBind::LLKeyBind(const LLSD &key_bind) bool LLKeyBind::operator==(const LLKeyBind& rhs) { - U32 size = mData.size(); + auto size = mData.size(); if (size != rhs.mData.size()) return false; - for (U32 i = 0; i < size; i++) + for (size_t i = 0; i < size; i++) { if (mData[i] != rhs.mData[i]) return false; } @@ -193,7 +193,7 @@ bool LLKeyBind::operator==(const LLKeyBind& rhs) bool LLKeyBind::operator!=(const LLKeyBind& rhs) { - U32 size = mData.size(); + auto size = mData.size(); if (size != rhs.mData.size()) return true; for (U32 i = 0; i < size; i++) @@ -215,7 +215,7 @@ bool LLKeyBind::isEmpty() const LLSD LLKeyBind::asLLSD() const { - S32 last = mData.size() - 1; + auto last = mData.size() - 1; while (mData[last].empty()) { last--; @@ -380,7 +380,7 @@ void LLKeyBind::resetKeyData(S32 index) void LLKeyBind::trimEmpty() { - S32 last = mData.size() - 1; + auto last = mData.size() - 1; while (last >= 0 && mData[last].empty()) { mData.erase(mData.begin() + last); @@ -388,7 +388,7 @@ void LLKeyBind::trimEmpty() } } -U32 LLKeyBind::getDataCount() +size_t LLKeyBind::getDataCount() { return mData.size(); } -- cgit v1.2.3 From 8f6ffd489df9d6346e04070ad8209f2432ef3416 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Tue, 6 Dec 2022 13:04:35 -0500 Subject: DRTVWR-575: Introduce LLKeyBind::endNonEmpty() and use it to replace dubious loops in asLLSD() and trimEmpty(). --- indra/llcommon/llkeybind.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'indra/llcommon/llkeybind.cpp') diff --git a/indra/llcommon/llkeybind.cpp b/indra/llcommon/llkeybind.cpp index f47ba41bae..12e57ae94b 100644 --- a/indra/llcommon/llkeybind.cpp +++ b/indra/llcommon/llkeybind.cpp @@ -30,6 +30,7 @@ #include "llsd.h" #include "llsdutil.h" +#include LLKeyData::LLKeyData() : @@ -213,19 +214,23 @@ bool LLKeyBind::isEmpty() const return true; } -LLSD LLKeyBind::asLLSD() const +LLKeyBind::data_vector_t::const_iterator LLKeyBind::endNonEmpty() const { - auto last = mData.size() - 1; - while (mData[last].empty()) - { - last--; - } + // search backwards for last non-empty entry, then turn back into forwards + // iterator (.base() call) + return std::find_if_not(mData.rbegin(), mData.rend(), + [](const auto& kdata){ return kdata.empty(); }).base(); +} +LLSD LLKeyBind::asLLSD() const +{ LLSD data; - for (S32 i = 0; i <= last; ++i) + auto end{ endNonEmpty() }; + for (auto it = mData.begin(); it < end; ++it) { - // append even if empty to not affect visual representation - data.append(mData[i].asLLSD()); + // append intermediate entries even if empty to not affect visual + // representation + data.append(it->asLLSD()); } return data; } @@ -380,16 +385,10 @@ void LLKeyBind::resetKeyData(S32 index) void LLKeyBind::trimEmpty() { - auto last = mData.size() - 1; - while (last >= 0 && mData[last].empty()) - { - mData.erase(mData.begin() + last); - last--; - } + mData.erase(endNonEmpty(), mData.end()); } size_t LLKeyBind::getDataCount() { return mData.size(); } - -- cgit v1.2.3 From 9e743c99fb69db5694c388ae33656c62e3fa0b8e Mon Sep 17 00:00:00 2001 From: Fawrsk Date: Sat, 7 Jan 2023 00:38:12 -0400 Subject: Cleanup for loops in llcommon to use C++11 range based for loops --- indra/llcommon/llkeybind.cpp | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'indra/llcommon/llkeybind.cpp') diff --git a/indra/llcommon/llkeybind.cpp b/indra/llcommon/llkeybind.cpp index 12e57ae94b..b89160cc55 100644 --- a/indra/llcommon/llkeybind.cpp +++ b/indra/llcommon/llkeybind.cpp @@ -207,9 +207,9 @@ bool LLKeyBind::operator!=(const LLKeyBind& rhs) bool LLKeyBind::isEmpty() const { - for (data_vector_t::const_iterator iter = mData.begin(); iter != mData.end(); iter++) + for (const LLKeyData& key_data : mData) { - if (!iter->isEmpty()) return false; + if (!key_data.isEmpty()) return false; } return true; } @@ -225,12 +225,11 @@ LLKeyBind::data_vector_t::const_iterator LLKeyBind::endNonEmpty() const LLSD LLKeyBind::asLLSD() const { LLSD data; - auto end{ endNonEmpty() }; - for (auto it = mData.begin(); it < end; ++it) + for (const LLKeyData& key_data : mData) { // append intermediate entries even if empty to not affect visual // representation - data.append(it->asLLSD()); + data.append(key_data.asLLSD()); } return data; } @@ -243,9 +242,9 @@ bool LLKeyBind::canHandle(EMouseClickType mouse, KEY key, MASK mask) const return false; } - for (data_vector_t::const_iterator iter = mData.begin(); iter != mData.end(); iter++) + for (const LLKeyData& key_data : mData) { - if (iter->canHandle(mouse, key, mask)) + if (key_data.canHandle(mouse, key, mask)) { return true; } @@ -267,12 +266,12 @@ bool LLKeyBind::hasKeyData(EMouseClickType mouse, KEY key, MASK mask, bool ignor { if (mouse != CLICK_NONE || key != KEY_NONE) { - for (data_vector_t::const_iterator iter = mData.begin(); iter != mData.end(); iter++) + for (const LLKeyData& key_data : mData) { - if (iter->mKey == key - && iter->mMask == mask - && iter->mMouse == mouse - && iter->mIgnoreMasks == ignore) + if (key_data.mKey == key + && key_data.mMask == mask + && key_data.mMouse == mouse + && key_data.mIgnoreMasks == ignore) { return true; } @@ -354,16 +353,16 @@ void LLKeyBind::replaceKeyData(const LLKeyData& data, U32 index) { // if both click and key are none (isEmpty()), we are inserting a placeholder, we don't want to reset anything // otherwise reset identical key - for (data_vector_t::iterator iter = mData.begin(); iter != mData.end(); iter++) + for (LLKeyData& key_data : mData) { - if (iter->mKey == data.mKey - && iter->mMouse == data.mMouse - && iter->mIgnoreMasks == data.mIgnoreMasks - && iter->mMask == data.mMask) + if (key_data.mKey == data.mKey + && key_data.mMouse == data.mMouse + && key_data.mIgnoreMasks == data.mIgnoreMasks + && key_data.mMask == data.mMask) { // Replacing only fully equal combinations even in case 'ignore' is set // Reason: Simplicity and user might decide to do a 'move' command as W and Shift+Ctrl+W, and 'run' as Shift+W - iter->reset(); + key_data.reset(); break; } } -- cgit v1.2.3