summaryrefslogtreecommitdiff
path: root/cui
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2021-04-28 12:37:19 +0200
committerMike Kaganski <mike.kaganski@collabora.com>2021-04-29 07:17:14 +0200
commitb6ab5914d8b2fc7041430796890f086f8e3e6369 (patch)
treeb4aae0f0352d8e1c61edaae53b354cc2cc2fcef2 /cui
parentb27de500b1412bacefc7144b5222c307ca1ae0fe (diff)
tdf#135997: make sure that the two lists are same length
This fixes the strange assumption that when searching the two lists (character names and font names) independently, the two found positions will necessarily correspond to each other. Instead, the positions of the match must be the same, which is implemented now. Also the input from configuration is sanitized. Change-Id: I920de7414387e181e11183b8a22776a72b6be419 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114722 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'cui')
-rw-r--r--cui/source/dialogs/cuicharmap.cxx93
1 files changed, 58 insertions, 35 deletions
diff --git a/cui/source/dialogs/cuicharmap.cxx b/cui/source/dialogs/cuicharmap.cxx
index 83931f69e9e2..c8bf07cc5794 100644
--- a/cui/source/dialogs/cuicharmap.cxx
+++ b/cui/source/dialogs/cuicharmap.cxx
@@ -186,7 +186,7 @@ short SvxCharacterMap::run()
if( SvxShowCharSet::getSelectedChar() == ' ')
{
m_xOKBtn->set_sensitive(false);
- setFavButtonState(OUString(), OUString());
+ setFavButtonState(u"", u"");
}
else
{
@@ -235,6 +235,11 @@ void SvxCharacterMap::getRecentCharacterList()
{
maRecentCharFontList.push_back(s);
}
+
+ // tdf#135997: make sure that the two lists are same length
+ const auto nCommonLength = std::min(maRecentCharList.size(), maRecentCharFontList.size());
+ maRecentCharList.resize(nCommonLength);
+ maRecentCharFontList.resize(nCommonLength);
}
@@ -255,11 +260,46 @@ void SvxCharacterMap::getFavCharacterList()
{
maFavCharFontList.push_back(s);
}
+
+ // tdf#135997: make sure that the two lists are same length
+ const auto nCommonLength = std::min(maFavCharList.size(), maFavCharFontList.size());
+ maFavCharList.resize(nCommonLength);
+ maFavCharFontList.resize(nCommonLength);
+}
+
+static std::pair<std::deque<OUString>::const_iterator, std::deque<OUString>::const_iterator>
+findInPair(std::u16string_view str1, const std::deque<OUString>& rContainer1,
+ std::u16string_view str2, const std::deque<OUString>& rContainer2)
+{
+ assert(rContainer1.size() == rContainer2.size());
+
+ if (auto it1 = std::find(rContainer1.begin(), rContainer1.end(), str1);
+ it1 != rContainer1.end())
+ {
+ auto it2 = rContainer2.begin() + (it1 - rContainer1.begin());
+ if (*it2 == str2)
+ return { it1, it2 };
+ }
+ return { rContainer1.end(), rContainer2.end() };
+}
+
+std::pair<std::deque<OUString>::const_iterator, std::deque<OUString>::const_iterator>
+SvxCharacterMap::getRecentChar(std::u16string_view sTitle, std::u16string_view rFont) const
+{
+ return findInPair(sTitle, maRecentCharList, rFont, maRecentCharFontList);
+}
+
+std::pair<std::deque<OUString>::const_iterator, std::deque<OUString>::const_iterator>
+SvxCharacterMap::getFavChar(std::u16string_view sTitle, std::u16string_view rFont) const
+{
+ return findInPair(sTitle, maFavCharList, rFont, maFavCharFontList);
}
void SvxCharacterMap::updateRecentCharControl()
{
+ assert(maRecentCharList.size() == maRecentCharFontList.size());
+
int i = 0;
for ( std::deque< OUString >::iterator it = maRecentCharList.begin(), it2 = maRecentCharFontList.begin();
it != maRecentCharList.end() && it2 != maRecentCharFontList.end();
@@ -281,12 +321,9 @@ void SvxCharacterMap::updateRecentCharControl()
void SvxCharacterMap::updateRecentCharacterList(const OUString& sTitle, const OUString& rFont)
{
- auto itChar = std::find(maRecentCharList.begin(), maRecentCharList.end(), sTitle);
-
- auto itChar2 = std::find(maRecentCharFontList.begin(), maRecentCharFontList.end(), rFont);
-
// if recent char to be added is already in list, remove it
- if( itChar != maRecentCharList.end() && itChar2 != maRecentCharFontList.end() )
+ if( const auto& [itChar, itChar2] = getRecentChar(sTitle, rFont);
+ itChar != maRecentCharList.end() && itChar2 != maRecentCharFontList.end() )
{
maRecentCharList.erase( itChar );
maRecentCharFontList.erase( itChar2);
@@ -321,12 +358,9 @@ void SvxCharacterMap::updateRecentCharacterList(const OUString& sTitle, const OU
void SvxCharacterMap::updateFavCharacterList(const OUString& sTitle, const OUString& rFont)
{
- auto itChar = std::find(maFavCharList.begin(), maFavCharList.end(), sTitle);
-
- auto itChar2 = std::find(maFavCharFontList.begin(), maFavCharFontList.end(), rFont);
-
// if Fav char to be added is already in list, remove it
- if( itChar != maFavCharList.end() && itChar2 != maFavCharFontList.end() )
+ if( const auto& [itChar, itChar2] = getFavChar(sTitle, rFont);
+ itChar != maFavCharList.end() && itChar2 != maFavCharFontList.end() )
{
maFavCharList.erase( itChar );
maFavCharFontList.erase( itChar2);
@@ -359,6 +393,8 @@ void SvxCharacterMap::updateFavCharacterList(const OUString& sTitle, const OUStr
void SvxCharacterMap::updateFavCharControl()
{
+ assert(maFavCharList.size() == maFavCharFontList.size());
+
int i = 0;
for ( std::deque< OUString >::iterator it = maFavCharList.begin(), it2 = maFavCharFontList.begin();
it != maFavCharList.end() && it2 != maFavCharFontList.end();
@@ -380,14 +416,11 @@ void SvxCharacterMap::updateFavCharControl()
m_xSearchSet->getFavCharacterList();
}
-void SvxCharacterMap::deleteFavCharacterFromList(const OUString& sTitle, const OUString& rFont)
+void SvxCharacterMap::deleteFavCharacterFromList(std::u16string_view sTitle, std::u16string_view rFont)
{
- auto itChar = std::find(maFavCharList.begin(), maFavCharList.end(), sTitle);
-
- auto itChar2 = std::find(maFavCharFontList.begin(), maFavCharFontList.end(), rFont);
-
- // if Fav char to be added is already in list, remove it
- if( itChar != maFavCharList.end() && itChar2 != maFavCharFontList.end() )
+ // if Fav char is found, remove it
+ if( const auto& [itChar, itChar2] = getFavChar(sTitle, rFont);
+ itChar != maFavCharList.end() && itChar2 != maFavCharFontList.end() )
{
maFavCharList.erase( itChar );
maFavCharFontList.erase( itChar2);
@@ -529,24 +562,16 @@ void SvxCharacterMap::init()
m_xSearchText->connect_changed(LINK(this, SvxCharacterMap, SearchUpdateHdl));
}
-bool SvxCharacterMap::isFavChar(const OUString& sTitle, const OUString& rFont)
+bool SvxCharacterMap::isFavChar(std::u16string_view sTitle, std::u16string_view rFont)
{
- auto isFavCharTitleExists = std::any_of(maFavCharList.begin(),
- maFavCharList.end(),
- [sTitle] (const OUString & a) { return a == sTitle; });
-
- auto isFavCharFontExists = std::any_of(maFavCharFontList.begin(),
- maFavCharFontList.end(),
- [rFont] (const OUString & a) { return a == rFont; });
-
- // if Fav char to be added is already in list, remove it
- return isFavCharTitleExists && isFavCharFontExists;
+ const auto& [itChar, itFont] = getFavChar(sTitle, rFont);
+ return itChar != maFavCharList.end() && itFont != maFavCharFontList.end();
}
-void SvxCharacterMap::setFavButtonState(const OUString& sTitle, const OUString& rFont)
+void SvxCharacterMap::setFavButtonState(std::u16string_view sTitle, std::u16string_view rFont)
{
- if(sTitle.isEmpty() || rFont.isEmpty())
+ if(sTitle.empty() || rFont.empty())
{
m_xFavouritesBtn->set_sensitive(false);
return;
@@ -746,13 +771,11 @@ IMPL_LINK_NOARG(SvxCharacterMap, SubsetSelectHdl, weld::ComboBox&, void)
IMPL_LINK(SvxCharacterMap, RecentClearClickHdl, SvxCharView*, rView, void)
{
const OUString& sTitle = rView->GetText();
- auto itChar = std::find(maRecentCharList.begin(), maRecentCharList.end(), sTitle);
-
OUString sFont = rView->GetFont().GetFamilyName();
- auto itChar2 = std::find(maRecentCharFontList.begin(), maRecentCharFontList.end(), sFont);
// if recent char to be added is already in list, remove it
- if( itChar != maRecentCharList.end() && itChar2 != maRecentCharFontList.end() )
+ if( const auto& [itChar, itChar2] = getRecentChar(sTitle, sFont);
+ itChar != maRecentCharList.end() && itChar2 != maRecentCharFontList.end() )
{
maRecentCharList.erase( itChar );
maRecentCharFontList.erase( itChar2);