summaryrefslogtreecommitdiff
path: root/indra
diff options
context:
space:
mode:
authorNat Goodspeed <nat@lindenlab.com>2022-12-06 13:04:35 -0500
committerNat Goodspeed <nat@lindenlab.com>2022-12-06 13:04:35 -0500
commit8f6ffd489df9d6346e04070ad8209f2432ef3416 (patch)
tree7460eec2902305e34f785e1cd3c2bfc4781ef113 /indra
parent8ff81fc7a5ffe9122dac4ceb705908cbf0361095 (diff)
DRTVWR-575: Introduce LLKeyBind::endNonEmpty()
and use it to replace dubious loops in asLLSD() and trimEmpty().
Diffstat (limited to 'indra')
-rw-r--r--indra/llcommon/llkeybind.cpp31
-rw-r--r--indra/llcommon/llkeybind.h2
2 files changed, 17 insertions, 16 deletions
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 <algorithm>
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();
}
-
diff --git a/indra/llcommon/llkeybind.h b/indra/llcommon/llkeybind.h
index f63ad1d64e..488f509411 100644
--- a/indra/llcommon/llkeybind.h
+++ b/indra/llcommon/llkeybind.h
@@ -100,6 +100,8 @@ public:
private:
typedef std::vector<LLKeyData> data_vector_t;
data_vector_t mData;
+
+ data_vector_t::const_iterator endNonEmpty() const;
};