LibreOffice Module sc (master) 1
userlist.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
23
24#include <global.hxx>
25#include <userlist.hxx>
28#include <com/sun/star/i18n/Calendar2.hpp>
29
30#include <algorithm>
31#include <utility>
32
34 maReal(std::move(aReal)), maUpper(ScGlobal::getCharClass().uppercase(maReal)) {}
35
37{
38 maSubStrings.clear();
39 sal_Int32 nIndex = 0;
40 do
41 {
42 OUString aSub = aStr.getToken(0, ScGlobal::cListDelimiter, nIndex);
43 if (!aSub.isEmpty())
44 maSubStrings.emplace_back(std::move(aSub));
45 } while (nIndex >= 0);
46}
47
49 aStr(std::move(_aStr))
50{
51 InitTokens();
52}
53
54void ScUserListData::SetString( const OUString& rStr )
55{
56 aStr = rStr;
57 InitTokens();
58}
59
60bool ScUserListData::GetSubIndex(const OUString& rSubStr, sal_uInt16& rIndex, bool& bMatchCase) const
61{
62 // First, case sensitive search.
63 auto itr = ::std::find_if(maSubStrings.begin(), maSubStrings.end(),
64 [&rSubStr](const SubStr& item) { return item.maReal == rSubStr; });
65 if (itr != maSubStrings.end())
66 {
67 rIndex = ::std::distance(maSubStrings.begin(), itr);
68 bMatchCase = true;
69 return true;
70 }
71
72 // When that fails, do a case insensitive search.
73 bMatchCase = false;
74 OUString aUpStr = ScGlobal::getCharClass().uppercase(rSubStr);
75 itr = ::std::find_if(maSubStrings.begin(), maSubStrings.end(),
76 [&aUpStr](const SubStr& item) { return item.maUpper == aUpStr; });
77 if (itr != maSubStrings.end())
78 {
79 rIndex = ::std::distance(maSubStrings.begin(), itr);
80 return true;
81 }
82 return false;
83}
84
85OUString ScUserListData::GetSubStr(sal_uInt16 nIndex) const
86{
87 if (nIndex < maSubStrings.size())
88 return maSubStrings[nIndex].maReal;
89 else
90 return OUString();
91}
92
93sal_Int32 ScUserListData::Compare(const OUString& rSubStr1, const OUString& rSubStr2) const
94{
95 sal_uInt16 nIndex1, nIndex2;
96 bool bMatchCase;
97 bool bFound1 = GetSubIndex(rSubStr1, nIndex1, bMatchCase);
98 bool bFound2 = GetSubIndex(rSubStr2, nIndex2, bMatchCase);
99 if (bFound1)
100 {
101 if (bFound2)
102 {
103 if (nIndex1 < nIndex2)
104 return -1;
105 else if (nIndex1 > nIndex2)
106 return 1;
107 else
108 return 0;
109 }
110 else
111 return -1;
112 }
113 else if (bFound2)
114 return 1;
115 else
116 return ScGlobal::GetCaseTransliteration().compareString( rSubStr1, rSubStr2 );
117}
118
119sal_Int32 ScUserListData::ICompare(const OUString& rSubStr1, const OUString& rSubStr2) const
120{
121 sal_uInt16 nIndex1, nIndex2;
122 bool bMatchCase;
123 bool bFound1 = GetSubIndex(rSubStr1, nIndex1, bMatchCase);
124 bool bFound2 = GetSubIndex(rSubStr2, nIndex2, bMatchCase);
125 if (bFound1)
126 {
127 if (bFound2)
128 {
129 if (nIndex1 < nIndex2)
130 return -1;
131 else if (nIndex1 > nIndex2)
132 return 1;
133 else
134 return 0;
135 }
136 else
137 return -1;
138 }
139 else if (bFound2)
140 return 1;
141 else
142 return ScGlobal::GetTransliteration().compareString( rSubStr1, rSubStr2 );
143}
144
145ScUserList::ScUserList(bool initDefault)
146{
147 if (initDefault)
148 AddDefaults();
149}
150
152{
154 for (const auto& rCalendar : ScGlobal::getLocaleData().getAllCalendars())
155 {
156 if (const auto& xCal = rCalendar.Days; xCal.hasElements())
157 {
158 OUStringBuffer aDayShortBuf(32), aDayLongBuf(64);
159 sal_Int32 i;
160 sal_Int32 nLen = xCal.getLength();
161 sal_Int16 nStart = sal::static_int_cast<sal_Int16>(nLen);
162 while (nStart > 0)
163 {
164 if (xCal[--nStart].ID == rCalendar.StartOfWeek)
165 break;
166 }
167 sal_Int16 nLast = sal::static_int_cast<sal_Int16>( (nStart + nLen - 1) % nLen );
168 for (i = nStart; i != nLast; i = (i+1) % nLen)
169 {
170 aDayShortBuf.append(xCal[i].AbbrevName);
171 aDayShortBuf.append(cDelimiter);
172 aDayLongBuf.append(xCal[i].FullName);
173 aDayLongBuf.append(cDelimiter);
174 }
175 aDayShortBuf.append(xCal[i].AbbrevName);
176 aDayLongBuf.append(xCal[i].FullName);
177
178 OUString aDayShort = aDayShortBuf.makeStringAndClear();
179 OUString aDayLong = aDayLongBuf.makeStringAndClear();
180
181 if ( !HasEntry( aDayShort ) )
182 emplace_back(aDayShort);
183 if ( !HasEntry( aDayLong ) )
184 emplace_back(aDayLong);
185 }
186
187 if (const auto& xCal = rCalendar.Months; xCal.hasElements())
188 {
189 OUStringBuffer aMonthShortBuf(128), aMonthLongBuf(128);
190 sal_Int32 i;
191 sal_Int32 nLen = xCal.getLength() - 1;
192 for (i = 0; i < nLen; i++)
193 {
194 aMonthShortBuf.append(xCal[i].AbbrevName);
195 aMonthShortBuf.append(cDelimiter);
196 aMonthLongBuf.append(xCal[i].FullName);
197 aMonthLongBuf.append(cDelimiter);
198 }
199 aMonthShortBuf.append(xCal[i].AbbrevName);
200 aMonthLongBuf.append(xCal[i].FullName);
201
202 OUString aMonthShort = aMonthShortBuf.makeStringAndClear();
203 OUString aMonthLong = aMonthLongBuf.makeStringAndClear();
204
205 if ( !HasEntry( aMonthShort ) )
206 emplace_back(aMonthShort);
207 if ( !HasEntry( aMonthLong ) )
208 emplace_back(aMonthLong);
209 }
210 }
211}
212
213const ScUserListData* ScUserList::GetData(const OUString& rSubStr) const
214{
215 const ScUserListData* pFirstCaseInsensitive = nullptr;
216 sal_uInt16 nIndex;
217 bool bMatchCase = false;
218
219 for (const auto& rxItem : maData)
220 {
221 if (rxItem.GetSubIndex(rSubStr, nIndex, bMatchCase))
222 {
223 if (bMatchCase)
224 return &rxItem;
225 if (!pFirstCaseInsensitive)
226 pFirstCaseInsensitive = &rxItem;
227 }
228 }
229
230 return pFirstCaseInsensitive;
231}
232
233bool ScUserList::operator==( const ScUserList& r ) const
234{
235 return std::equal(maData.begin(), maData.end(), r.maData.begin(), r.maData.end(),
236 [](const ScUserListData& lhs, const ScUserListData& rhs) {
237 return lhs.GetString() == rhs.GetString();
238 });
239}
240
241bool ScUserList::HasEntry( std::u16string_view rStr ) const
242{
243 return ::std::any_of(maData.begin(), maData.end(),
244 [&] (ScUserListData const& pData)
245 { return pData.GetString() == rStr; } );
246}
247
248/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString uppercase(const OUString &rStr, sal_Int32 nPos, sal_Int32 nCount) const
static constexpr sal_Unicode cListDelimiter
Definition: global.hxx:561
::utl::TransliterationWrapper & GetCaseTransliteration()
Definition: global.cxx:1038
static SC_DLLPUBLIC ::utl::TransliterationWrapper & GetTransliteration()
Definition: global.cxx:1026
static SC_DLLPUBLIC const LocaleDataWrapper & getLocaleData()
Definition: global.cxx:1055
static SC_DLLPUBLIC const CharClass & getCharClass()
Definition: global.cxx:1064
Stores individual user-defined sort list.
Definition: userlist.hxx:32
OUString GetSubStr(sal_uInt16 nIndex) const
Definition: userlist.cxx:85
sal_Int32 Compare(const OUString &rSubStr1, const OUString &rSubStr2) const
Definition: userlist.cxx:93
OUString aStr
Definition: userlist.hxx:42
sal_Int32 ICompare(const OUString &rSubStr1, const OUString &rSubStr2) const
Definition: userlist.cxx:119
SAL_DLLPRIVATE void InitTokens()
Definition: userlist.cxx:36
std::vector< SubStr > maSubStrings
Definition: userlist.hxx:41
void SetString(const OUString &rStr)
Definition: userlist.cxx:54
bool GetSubIndex(const OUString &rSubStr, sal_uInt16 &rIndex, bool &bMatchCase) const
Definition: userlist.cxx:60
ScUserListData(OUString aStr)
Definition: userlist.cxx:48
Collection of user-defined sort lists.
Definition: userlist.hxx:62
ScUserList(bool initDefault=true)
Definition: userlist.cxx:145
void emplace_back(Args &&... args)
Definition: userlist.hxx:85
DataType maData
Definition: userlist.hxx:64
bool HasEntry(std::u16string_view rStr) const
If the list in rStr is already inserted.
Definition: userlist.cxx:241
bool operator==(const ScUserList &r) const
Definition: userlist.cxx:233
void AddDefaults()
Definition: userlist.cxx:151
const ScUserListData * GetData(const OUString &rSubStr) const
Definition: userlist.cxx:213
sal_Int32 compareString(const OUString &rStr1, const OUString &rStr2) const
sal_Int32 nIndex
std::unique_ptr< sal_Int32[]> pData
int i
PyRef getCharClass(const Runtime &)
SubStr(OUString &&aReal)
Definition: userlist.cxx:33
const sal_Unicode cDelimiter
Definition: tpusrlst.cxx:45
sal_uInt16 sal_Unicode