diff options
author | Arkadiy Illarionov <qarkai@gmail.com> | 2019-09-07 23:10:33 +0300 |
---|---|---|
committer | Arkadiy Illarionov <qarkai@gmail.com> | 2019-10-21 19:41:43 +0200 |
commit | 00e2f118d7f4f070ebddf16b2cf4e89cf9d551a7 (patch) | |
tree | 2f48bf4455360d0f08d8096317ea31012debffbc /i18npool/source/transliteration | |
parent | ad3e00237f48c52dbd2833f21f5e2f5acfdd4167 (diff) |
Simplify Sequence iterations in i18npool
Use range-based loops, STL and comphelper functions.
Change-Id: Ibbc1c14e921585819872f26e8def2a60594e6a63
Reviewed-on: https://gerrit.libreoffice.org/78754
Tested-by: Jenkins
Reviewed-by: Arkadiy Illarionov <qarkai@gmail.com>
Diffstat (limited to 'i18npool/source/transliteration')
8 files changed, 85 insertions, 148 deletions
diff --git a/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx b/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx index d706f59f5528..ada76a735b80 100644 --- a/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx +++ b/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx @@ -7,6 +7,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include <comphelper/sequence.hxx> #include <rtl/ustrbuf.hxx> #include <transliteration_Ignore.hxx> #include <unicode/translit.h> @@ -59,10 +60,11 @@ ignoreDiacritics_CTL::foldingImpl(const OUString& rInStr, sal_Int32 nStartPos, if (useOffset) { OUStringBuffer aOutBuf(nCount); - rOffset.realloc(nCount); + + std::vector<sal_Int32> aOffset; + aOffset.reserve(nCount); sal_Int32 nPosition = nStartPos; - sal_Int32 nOffset = 0; while (nPosition < nStartPos + nCount) { sal_Int32 nIndex = nPosition; @@ -70,19 +72,14 @@ ignoreDiacritics_CTL::foldingImpl(const OUString& rInStr, sal_Int32 nStartPos, icu::UnicodeString aUStr(nChar); m_transliterator->transliterate(aUStr); - if (nOffset + aUStr.length() > rOffset.getLength()) - rOffset.realloc(rOffset.getLength() + aUStr.length()); - sal_Int32* pOffset = rOffset.getArray(); - aOutBuf.append(reinterpret_cast<const sal_Unicode*>(aUStr.getBuffer()), aUStr.length()); - for (const sal_Int32 nOffsetEnd = nOffset+aUStr.length(); nOffset < nOffsetEnd; nOffset++) - pOffset[nOffset] = nPosition; + std::fill_n(std::back_inserter(aOffset), aUStr.length(), nPosition); nPosition = nIndex; } - rOffset.realloc(aOutBuf.getLength()); + rOffset = comphelper::containerToSequence(aOffset); return aOutBuf.makeStringAndClear(); } else diff --git a/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx b/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx index f466cf9d2693..0be8c094c876 100644 --- a/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx @@ -21,6 +21,8 @@ #include <transliteration_Ignore.hxx> +#include <numeric> + using namespace com::sun::star::uno; using namespace com::sun::star::lang; @@ -72,13 +74,10 @@ ignoreIandEfollowedByYa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 sta sal_Unicode * dst = newStr->buffer; const sal_Unicode * src = inStr.getStr() + startPos; - sal_Int32 *p = nullptr; - sal_Int32 position = 0; if (useOffset) { // Allocate nCount length to offset argument. offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } @@ -96,10 +95,6 @@ ignoreIandEfollowedByYa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 sta if (currentChar == 0x30E3 || // KATAKANA LETTER SMALL YA currentChar == 0x30E4) { // KATAKANA LETTER YA if (aTable[ previousChar ] != previousChar) { - if (useOffset) { - *p ++ = position++; - *p ++ = position++; - } *dst ++ = previousChar; *dst ++ = 0x30A2; // KATAKANA LETTER A previousChar = *src ++; @@ -108,15 +103,11 @@ ignoreIandEfollowedByYa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 sta } } - if (useOffset) - *p ++ = position++; *dst ++ = previousChar; previousChar = currentChar; } if (nCount == 0) { - if (useOffset) - *p = position; *dst ++ = previousChar; } diff --git a/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx b/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx index 9395daa8ed5c..66e53845196e 100644 --- a/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx @@ -21,6 +21,8 @@ #include <transliteration_Ignore.hxx> +#include <numeric> + using namespace com::sun::star::uno; using namespace com::sun::star::lang; @@ -90,13 +92,10 @@ ignoreIterationMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 startPo sal_Unicode * dst = newStr->buffer; const sal_Unicode * src = inStr.getStr() + startPos; - sal_Int32 * p = nullptr; - sal_Int32 position = 0; if (useOffset) { // Allocate nCount length to offset argument. offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } @@ -118,15 +117,11 @@ ignoreIterationMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 startPo currentChar = aTable[ previousChar ]; break; } - if (useOffset) - *p ++ = position ++; *dst ++ = previousChar; previousChar = currentChar; } if (nCount == 0) { - if (useOffset) - *p = position; *dst ++ = previousChar; } @@ -136,7 +131,6 @@ ignoreIterationMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 startPo if (useOffset) offset.realloc(newStr->length); return OUString(newStr, SAL_NO_ACQUIRE); // take ownership - } } diff --git a/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx b/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx index 1c9c9e491eb0..53a2f058d0a9 100644 --- a/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx @@ -19,6 +19,8 @@ #include <transliteration_Ignore.hxx> +#include <numeric> + using namespace com::sun::star::uno; using namespace com::sun::star::lang; @@ -33,13 +35,10 @@ ignoreKiKuFollowedBySa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 star sal_Unicode * dst = newStr->buffer; const sal_Unicode * src = inStr.getStr() + startPos; - sal_Int32 *p = nullptr; - sal_Int32 position = 0; if (useOffset) { // Allocate nCount length to offset argument. offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } @@ -54,10 +53,6 @@ ignoreKiKuFollowedBySa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 star if (previousChar == 0x30AF ) { // KATAKANA LETTER KU if (0x30B5 <= currentChar && // KATAKANA LETTER SA currentChar <= 0x30BE) { // KATAKANA LETTER ZO - if (useOffset) { - *p ++ = position++; - *p ++ = position++; - } *dst ++ = 0x30AD; // KATAKANA LETTER KI *dst ++ = currentChar; previousChar = *src ++; @@ -66,15 +61,11 @@ ignoreKiKuFollowedBySa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 star } } - if (useOffset) - *p ++ = position++; *dst ++ = previousChar; previousChar = currentChar; } if (nCount == 0) { - if (useOffset) - *p = position; *dst ++ = previousChar; } diff --git a/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx b/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx index ca1cb82d407a..91358dc60a3d 100644 --- a/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx @@ -19,6 +19,8 @@ #include <transliteration_Ignore.hxx> +#include <numeric> + using namespace com::sun::star::uno; using namespace com::sun::star::lang; @@ -295,14 +297,10 @@ ignoreProlongedSoundMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 st sal_Unicode * dst = newStr->buffer; const sal_Unicode * src = inStr.getStr() + startPos; - sal_Int32 *p = nullptr; - sal_Int32 position = 0; - if (useOffset) { // Allocate nCount length to offset argument. offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } @@ -324,15 +322,11 @@ ignoreProlongedSoundMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 st } } - if (useOffset) - *p ++ = position ++; *dst ++ = previousChar; previousChar = currentChar; } if (nCount == 0) { - if (useOffset) - *p = position; *dst ++ = previousChar; } diff --git a/i18npool/source/transliteration/transliterationImpl.cxx b/i18npool/source/transliteration/transliterationImpl.cxx index fc51730834b1..195f7a789643 100644 --- a/i18npool/source/transliteration/transliterationImpl.cxx +++ b/i18npool/source/transliteration/transliterationImpl.cxx @@ -25,11 +25,13 @@ #include <com/sun/star/i18n/TransliterationType.hpp> #include <com/sun/star/i18n/TransliterationModulesExtra.hpp> +#include <comphelper/sequence.hxx> #include <cppuhelper/supportsservice.hxx> #include <rtl/instance.hxx> #include <rtl/ustring.hxx> #include <algorithm> +#include <numeric> using namespace com::sun::star::uno; using namespace com::sun::star::i18n; @@ -256,8 +258,8 @@ TransliterationImpl::loadModulesByImplNames(const Sequence< OUString >& implName throw ERROR; clear(); - for (sal_Int32 i = 0; i < implNameList.getLength(); i++) - if (loadModuleByName(implNameList[i], bodyCascade[numCascade], rLocale)) + for (const auto& rName : implNameList) + if (loadModuleByName(rName, bodyCascade[numCascade], rLocale)) numCascade++; } @@ -266,19 +268,18 @@ Sequence<OUString> SAL_CALL TransliterationImpl::getAvailableModules( const Locale& rLocale, sal_Int16 sType ) { const Sequence<OUString> &translist = mxLocaledata->getTransliterations(rLocale); - Sequence<OUString> r(translist.getLength()); + std::vector<OUString> r; + r.reserve(translist.getLength()); Reference<XExtendedTransliteration> body; - sal_Int32 n = 0; - for (sal_Int32 i = 0; i < translist.getLength(); i++) + for (const auto& rTrans : translist) { - if (loadModuleByName(translist[i], body, rLocale)) { + if (loadModuleByName(rTrans, body, rLocale)) { if (body->getType() & sType) - r[n++] = translist[i]; + r.push_back(rTrans); body.clear(); } } - r.realloc(n); - return r; + return comphelper::containerToSequence(r); } @@ -310,9 +311,8 @@ TransliterationImpl::transliterate( const OUString& inStr, sal_Int32 startPos, s else { OUString tmpStr = inStr.copy(startPos, nCount); - sal_Int32 * pArr = offset.getArray(); - for (sal_Int32 j = 0; j < nCount; j++) - pArr[j] = startPos + j; + + std::iota(offset.begin(), offset.end(), startPos); sal_Int16 from = 0, to = 1; Sequence<sal_Int32> off[2]; @@ -370,11 +370,10 @@ TransliterationImpl::folding( const OUString& inStr, sal_Int32 startPos, sal_Int else { OUString tmpStr = inStr.copy(startPos, nCount); - sal_Int32 * pArr = offset.getArray(); - for (sal_Int32 j = 0; j < nCount; j++) - pArr[j] = startPos + j; - sal_Int16 from = 0, to = 1, tmp; + std::iota(offset.begin(), offset.end(), startPos); + + sal_Int16 from = 0, to = 1; Sequence<sal_Int32> off[2]; off[to] = offset; @@ -383,7 +382,7 @@ TransliterationImpl::folding( const OUString& inStr, sal_Int32 startPos, sal_Int nCount = tmpStr.getLength(); - tmp = from; from = to; to = tmp; + std::swap(from, to); for (sal_Int32 j = 0; j < nCount; j++) off[to][j] = off[from][off[to][j]]; } @@ -476,16 +475,16 @@ TransliterationImpl::equals( for (i = 0; i < nLen; ++i, ++p1, ++p2 ) { if (*p1 != *p2) { // return number of matched code points so far - nMatch1 = (i < offset1.getLength()) ? offset1[i] : i; - nMatch2 = (i < offset2.getLength()) ? offset2[i] : i; + nMatch1 = (i < offset1.getLength()) ? offset1.getConstArray()[i] : i; + nMatch2 = (i < offset2.getLength()) ? offset2.getConstArray()[i] : i; return false; } } // i==nLen if ( tmpStr1.getLength() != tmpStr2.getLength() ) { // return number of matched code points so far - nMatch1 = (i <= offset1.getLength()) ? offset1[i-1] + 1 : i; - nMatch2 = (i <= offset2.getLength()) ? offset2[i-1] + 1 : i; + nMatch1 = (i <= offset1.getLength()) ? offset1.getConstArray()[i-1] + 1 : i; + nMatch2 = (i <= offset2.getLength()) ? offset2.getConstArray()[i-1] + 1 : i; return false; } else { nMatch1 = nCount1; @@ -494,8 +493,6 @@ TransliterationImpl::equals( } } -#define MaxOutput 2 - Sequence< OUString > TransliterationImpl::getRange(const Sequence< OUString > &inStrs, const sal_Int32 length, sal_Int16 _numCascade) @@ -504,18 +501,20 @@ TransliterationImpl::getRange(const Sequence< OUString > &inStrs, return inStrs; sal_Int32 j_tmp = 0; - Sequence< OUString > ostr(MaxOutput*length); + constexpr sal_Int32 nMaxOutput = 2; + const sal_Int32 nMaxOutputLength = nMaxOutput*length; + std::vector<OUString> ostr; + ostr.reserve(nMaxOutputLength); for (sal_Int32 j = 0; j < length; j+=2) { const Sequence< OUString >& temp = bodyCascade[_numCascade]->transliterateRange(inStrs[j], inStrs[j+1]); - for ( sal_Int32 k = 0; k < temp.getLength(); k++) { - if ( j_tmp >= MaxOutput*length ) throw ERROR; - ostr[j_tmp++] = temp[k]; + for (const auto& rStr : temp) { + if ( j_tmp++ >= nMaxOutputLength ) throw ERROR; + ostr.push_back(rStr); } } - ostr.realloc(j_tmp); - return getRange(ostr, j_tmp, ++_numCascade); + return getRange(comphelper::containerToSequence(ostr), j_tmp, ++_numCascade); } diff --git a/i18npool/source/transliteration/transliteration_OneToOne.cxx b/i18npool/source/transliteration/transliteration_OneToOne.cxx index 484a34db0b0f..f865a4640a80 100644 --- a/i18npool/source/transliteration/transliteration_OneToOne.cxx +++ b/i18npool/source/transliteration/transliteration_OneToOne.cxx @@ -22,6 +22,8 @@ #include <transliteration_OneToOne.hxx> #include <i18nutil/oneToOneMapping.hxx> +#include <numeric> + using namespace com::sun::star::i18n; using namespace com::sun::star::uno; @@ -64,20 +66,15 @@ transliteration_OneToOne::transliterateImpl( const OUString& inStr, sal_Int32 st const sal_Unicode * src = inStr.getStr() + startPos; // Allocate nCount length to offset argument. - sal_Int32 *p = nullptr; - sal_Int32 position = 0; if (useOffset) { offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } // Translation while (nCount -- > 0) { sal_Unicode c = *src++; *dst ++ = func ? func( c) : (*table)[ c ]; - if (useOffset) - *p ++ = position ++; } *dst = u'\0'; diff --git a/i18npool/source/transliteration/transliteration_body.cxx b/i18npool/source/transliteration/transliteration_body.cxx index 6d6c710b57c2..b168a5e37b3a 100644 --- a/i18npool/source/transliteration/transliteration_body.cxx +++ b/i18npool/source/transliteration/transliteration_body.cxx @@ -23,11 +23,13 @@ #include <com/sun/star/i18n/MultipleCharsOutputException.hpp> #include <com/sun/star/i18n/TransliterationType.hpp> #include <comphelper/processfactory.hxx> +#include <comphelper/sequence.hxx> #include <characterclassificationImpl.hxx> #include <transliteration_body.hxx> #include <memory> +#include <numeric> using namespace ::com::sun::star::uno; using namespace ::com::sun::star::i18n; @@ -92,80 +94,59 @@ Transliteration_body::transliterateImpl( { const sal_Unicode *in = inStr.getStr() + startPos; - // Two different blocks to eliminate the if(useOffset) condition inside the - // inner k loop. Yes, on massive use even such small things do count. - if ( useOffset ) + // We could assume that most calls result in identical string lengths, + // thus using a preallocated OUStringBuffer could be an easy way + // to assemble the return string without too much hassle. However, + // for single characters the OUStringBuffer::append() method is quite + // expensive compared to a simple array operation, so it pays here + // to copy the final result instead. + + // Allocate the max possible buffer. Try to use stack instead of heap, + // which would have to be reallocated most times anyways. + constexpr sal_Int32 nLocalBuf = 2048; + sal_Unicode aLocalBuf[ nLocalBuf * NMAPPINGMAX ], *out = aLocalBuf; + std::unique_ptr<sal_Unicode[]> pHeapBuf; + if (nCount > nLocalBuf) { - sal_Int32 nOffCount = 0, i; - for (i = 0; i < nCount; i++) - { - // take care of TOGGLE_CASE transliteration: - MappingType nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] ); - - const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType ); - nOffCount += map.nmap; - } - rtl_uString* pStr = rtl_uString_alloc(nOffCount); - sal_Unicode* out = pStr->buffer; + pHeapBuf.reset(new sal_Unicode[ nCount * NMAPPINGMAX ]); + out = pHeapBuf.get(); + } - if ( nOffCount != offset.getLength() ) - offset.realloc( nOffCount ); + sal_Int32 j = 0; + // Two different blocks to eliminate the if(useOffset) condition inside the loop. + // Yes, on massive use even such small things do count. + if ( useOffset ) + { + std::vector<sal_Int32> aVec; + aVec.reserve(std::max<sal_Int32>(nLocalBuf, nCount) * NMAPPINGMAX); - sal_Int32 j = 0; - sal_Int32 * pArr = offset.getArray(); - for (i = 0; i < nCount; i++) + for (sal_Int32 i = 0; i < nCount; i++) { // take care of TOGGLE_CASE transliteration: MappingType nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] ); const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType ); - for (sal_Int32 k = 0; k < map.nmap; k++) - { - pArr[j] = i + startPos; - out[j++] = map.map[k]; - } + std::fill_n(std::back_inserter(aVec), map.nmap, i + startPos); + std::copy_n(map.map, map.nmap, out + j); + j += map.nmap; } - out[j] = 0; - return OUString( pStr, SAL_NO_ACQUIRE ); + offset = comphelper::containerToSequence(aVec); } else { - // In the simple case of no offset sequence used we can eliminate the - // first getValue() loop. We could also assume that most calls result - // in identical string lengths, thus using a preallocated - // OUStringBuffer could be an easy way to assemble the return string - // without too much hassle. However, for single characters the - // OUStringBuffer::append() method is quite expensive compared to a - // simple array operation, so it pays here to copy the final result - // instead. - - // Allocate the max possible buffer. Try to use stack instead of heap, - // which would have to be reallocated most times anyways. - const sal_Int32 nLocalBuf = 2048; - sal_Unicode aLocalBuf[ nLocalBuf * NMAPPINGMAX ], *out = aLocalBuf; - std::unique_ptr<sal_Unicode[]> pHeapBuf; - if ( nCount > nLocalBuf ) { - pHeapBuf.reset(new sal_Unicode[ nCount * NMAPPINGMAX ]); - out = pHeapBuf.get(); - } - - sal_Int32 j = 0; for ( sal_Int32 i = 0; i < nCount; i++) { // take care of TOGGLE_CASE transliteration: MappingType nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] ); const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType ); - for (sal_Int32 k = 0; k < map.nmap; k++) - { - out[j++] = map.map[k]; - } + std::copy_n(map.map, map.nmap, out + j); + j += map.nmap; } - - OUString aRet( out, j ); - return aRet; } + + return OUString(out, j); } OUString SAL_CALL @@ -279,15 +260,8 @@ static OUString transliterate_titlecase_Impl( aRes += xCharClassImpl->toLower( aText, 1, aText.getLength() - 1, rLocale ); offset.realloc( aRes.getLength() ); - sal_Int32 *pOffset = offset.getArray(); - sal_Int32 nLen = offset.getLength(); - for (sal_Int32 i = 0; i < nLen; ++i) - { - sal_Int32 nIdx = 0; - if (i >= nResolvedLen) - nIdx = i - nResolvedLen + 1; - pOffset[i] = nIdx; - } + sal_Int32* pOffset = std::fill_n(offset.begin(), nResolvedLen, 0); + std::iota(pOffset, offset.end(), 1); } return aRes; } |