1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX 29*cdf0e10cSrcweir #define INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir namespace utl { 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir /** Iterator to be used with a digit grouping as obtained through 36*cdf0e10cSrcweir LocaleDataWrapper::getDigitGrouping(). 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir The iterator advances over the digit groupings, returning the number of 39*cdf0e10cSrcweir digits per group. If the last group was encountered the iterator will 40*cdf0e10cSrcweir always return the last grouping. 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir Grouping values are sanitized to be 0 <= value <= SAL_MAX_UINT16, even if 43*cdf0e10cSrcweir originally Int32, to be able to easily cast it down to String's xub_StrLen. 44*cdf0e10cSrcweir This shouldn't make any difference in practice. 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir Usage example with a string buffer containing a decimal representation of 47*cdf0e10cSrcweir an integer number. Note that of course this loop could be optimized to not 48*cdf0e10cSrcweir count single characters but hunks of groups instead using the get() method, 49*cdf0e10cSrcweir this is just for illustrating usage. Anyway, for double values it is highly 50*cdf0e10cSrcweir more efficient to use ::rtl::math::doubleToString() and pass the grouping 51*cdf0e10cSrcweir sequence, instead of using this iterator and inserting charcters into 52*cdf0e10cSrcweir strings. 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir DigitGroupingIterator aGrouping(...) 55*cdf0e10cSrcweir sal_Int32 nCount = 0; 56*cdf0e10cSrcweir sal_Int32 n = aBuffer.getLength(); 57*cdf0e10cSrcweir // >1 because we don't want to insert a separator if there is no leading digit. 58*cdf0e10cSrcweir while (n-- > 1) 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir if (++nCount >= aGrouping.getPos()) 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir aBuffer.insert( n, cSeparator); 63*cdf0e10cSrcweir nGroupDigits = aGrouping.advance(); 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir */ 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir class DigitGroupingIterator 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< sal_Int32 > maGroupings; 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir sal_Int32 mnGroup; // current active grouping 74*cdf0e10cSrcweir sal_Int32 mnDigits; // current active digits per group 75*cdf0e10cSrcweir sal_Int32 mnNextPos; // position (in digits) of next grouping 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir void setInfinite() 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir mnGroup = maGroupings.getLength(); 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir bool isInfinite() const 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir return mnGroup >= maGroupings.getLength(); 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir sal_Int32 getGrouping() const 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir if (mnGroup < maGroupings.getLength()) 90*cdf0e10cSrcweir { 91*cdf0e10cSrcweir sal_Int32 n = maGroupings[mnGroup]; 92*cdf0e10cSrcweir OSL_ENSURE( 0 <= n && n <= SAL_MAX_UINT16, "DigitGroupingIterator::getGrouping: far out"); 93*cdf0e10cSrcweir if (n < 0) 94*cdf0e10cSrcweir n = 0; // sanitize ... 95*cdf0e10cSrcweir else if (n > SAL_MAX_UINT16) 96*cdf0e10cSrcweir n = SAL_MAX_UINT16; // limit for use with xub_StrLen 97*cdf0e10cSrcweir return n; 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir return 0; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir void setPos() 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir // someone might be playing jokes on us, so check for overflow 105*cdf0e10cSrcweir if (mnNextPos <= SAL_MAX_INT32 - mnDigits) 106*cdf0e10cSrcweir mnNextPos += mnDigits; 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir void setDigits() 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir sal_Int32 nPrev = mnDigits; 112*cdf0e10cSrcweir mnDigits = getGrouping(); 113*cdf0e10cSrcweir if (!mnDigits) 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir mnDigits = nPrev; 116*cdf0e10cSrcweir setInfinite(); 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir setPos(); 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir void initGrouping() 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir mnDigits = 3; // just in case of constructed with empty grouping 124*cdf0e10cSrcweir mnGroup = 0; 125*cdf0e10cSrcweir mnNextPos = 0; 126*cdf0e10cSrcweir setDigits(); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir // not implemented, prevent usage 130*cdf0e10cSrcweir DigitGroupingIterator(); 131*cdf0e10cSrcweir DigitGroupingIterator( const DigitGroupingIterator & ); 132*cdf0e10cSrcweir DigitGroupingIterator & operator=( const DigitGroupingIterator & ); 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir public: 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir explicit DigitGroupingIterator( const ::com::sun::star::uno::Sequence< sal_Int32 > & rGroupings ) 137*cdf0e10cSrcweir : maGroupings( rGroupings) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir initGrouping(); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir /** Advance iterator to next grouping. */ 143*cdf0e10cSrcweir DigitGroupingIterator & advance() 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir if (isInfinite()) 146*cdf0e10cSrcweir setPos(); 147*cdf0e10cSrcweir else 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir ++mnGroup; 150*cdf0e10cSrcweir setDigits(); 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir return *this; 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir /** Obtain current grouping. Always > 0. */ 156*cdf0e10cSrcweir sal_Int32 get() const 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir return mnDigits; 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir /** The next position (in integer digits) from the right where to insert a 162*cdf0e10cSrcweir group separator. */ 163*cdf0e10cSrcweir sal_Int32 getPos() 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir return mnNextPos; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir /** Reset iterator to start again from the right beginning. */ 169*cdf0e10cSrcweir void reset() 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir initGrouping(); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir /** Create a sequence of bool values containing positions where to add a 175*cdf0e10cSrcweir separator when iterating forward over a string and copying digit per 176*cdf0e10cSrcweir digit. For example, for grouping in thousands and nIntegerDigits==7 the 177*cdf0e10cSrcweir sequence returned would be {1,0,0,1,0,0,0} so the caller would add a 178*cdf0e10cSrcweir separator after the 1st and the 4th digit. */ 179*cdf0e10cSrcweir static ::com::sun::star::uno::Sequence< sal_Bool > createForwardSequence( 180*cdf0e10cSrcweir sal_Int32 nIntegerDigits, 181*cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< sal_Int32 > & rGroupings ) 182*cdf0e10cSrcweir { 183*cdf0e10cSrcweir if (nIntegerDigits <= 0) 184*cdf0e10cSrcweir return ::com::sun::star::uno::Sequence< sal_Bool >(); 185*cdf0e10cSrcweir DigitGroupingIterator aIterator( rGroupings); 186*cdf0e10cSrcweir ::com::sun::star::uno::Sequence< sal_Bool > aSeq( nIntegerDigits); 187*cdf0e10cSrcweir sal_Bool* pArr = aSeq.getArray(); 188*cdf0e10cSrcweir for (sal_Int32 j = 0; --nIntegerDigits >= 0; ++j) 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir if (j == aIterator.getPos()) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir pArr[nIntegerDigits] = sal_True; 193*cdf0e10cSrcweir aIterator.advance(); 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir else 196*cdf0e10cSrcweir pArr[nIntegerDigits] = sal_False; 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir return aSeq; 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir }; 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir } // namespace utl 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir #endif // INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX 205