LCOV - code coverage report
Current view: top level - solver/unxlngi6.pro/inc/unotools - digitgroupingiterator.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 56 62 90.3 %
Date: 2012-08-25 Functions: 13 13 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 18 36 50.0 %

           Branch data     Line data    Source code
       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                 :            : #ifndef INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
      21                 :            : #define INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
      22                 :            : 
      23                 :            : #include <com/sun/star/uno/Sequence.hxx>
      24                 :            : 
      25                 :            : namespace utl {
      26                 :            : 
      27                 :            : /** Iterator to be used with a digit grouping as obtained through
      28                 :            :     LocaleDataWrapper::getDigitGrouping().
      29                 :            : 
      30                 :            :     The iterator advances over the digit groupings, returning the number of
      31                 :            :     digits per group. If the last group was encountered the iterator will
      32                 :            :     always return the last grouping.
      33                 :            : 
      34                 :            :     Grouping values are sanitized to be 0 <= value <= SAL_MAX_UINT16, even if
      35                 :            :     originally Int32, to be able to easily cast it down to String's xub_StrLen.
      36                 :            :     This shouldn't make any difference in practice.
      37                 :            : 
      38                 :            :     Usage example with a string buffer containing a decimal representation of
      39                 :            :     an integer number. Note that of course this loop could be optimized to not
      40                 :            :     count single characters but hunks of groups instead using the get() method,
      41                 :            :     this is just for illustrating usage. Anyway, for double values it is highly
      42                 :            :     more efficient to use ::rtl::math::doubleToString() and pass the grouping
      43                 :            :     sequence, instead of using this iterator and inserting charcters into
      44                 :            :     strings.
      45                 :            : 
      46                 :            :     DigitGroupingIterator aGrouping(...)
      47                 :            :     sal_Int32 nCount = 0;
      48                 :            :     sal_Int32 n = aBuffer.getLength();
      49                 :            :     // >1 because we don't want to insert a separator if there is no leading digit.
      50                 :            :     while (n-- > 1)
      51                 :            :     {
      52                 :            :         if (++nCount >= aGrouping.getPos())
      53                 :            :         {
      54                 :            :             aBuffer.insert( n, cSeparator);
      55                 :            :             nGroupDigits = aGrouping.advance();
      56                 :            :         }
      57                 :            :     }
      58                 :            : 
      59                 :            :  */
      60                 :            : 
      61                 :     110114 : class DigitGroupingIterator
      62                 :            : {
      63                 :            :     const ::com::sun::star::uno::Sequence< sal_Int32 > maGroupings;
      64                 :            : 
      65                 :            :     sal_Int32   mnGroup;        // current active grouping
      66                 :            :     sal_Int32   mnDigits;       // current active digits per group
      67                 :            :     sal_Int32   mnNextPos;      // position (in digits) of next grouping
      68                 :            : 
      69                 :      71640 :     void setInfinite()
      70                 :            :     {
      71                 :      71640 :         mnGroup = maGroupings.getLength();
      72                 :      71640 :     }
      73                 :            : 
      74                 :      71640 :     bool isInfinite() const
      75                 :            :     {
      76                 :      71640 :         return mnGroup >= maGroupings.getLength();
      77                 :            :     }
      78                 :            : 
      79                 :     182575 :     sal_Int32 getGrouping() const
      80                 :            :     {
      81         [ +  - ]:     182575 :         if (mnGroup < maGroupings.getLength())
      82                 :            :         {
      83                 :     182575 :             sal_Int32 n = maGroupings[mnGroup];
      84                 :            :             OSL_ENSURE( 0 <= n && n <= SAL_MAX_UINT16, "DigitGroupingIterator::getGrouping: far out");
      85         [ -  + ]:     182575 :             if (n < 0)
      86                 :          0 :                 n = 0;                  // sanitize ...
      87         [ -  + ]:     182575 :             else if (n > SAL_MAX_UINT16)
      88                 :          0 :                 n = SAL_MAX_UINT16;     // limit for use with xub_StrLen
      89                 :     182575 :             return n;
      90                 :            :         }
      91                 :     182575 :         return 0;
      92                 :            :     }
      93                 :            : 
      94                 :     182575 :     void setPos()
      95                 :            :     {
      96                 :            :         // someone might be playing jokes on us, so check for overflow
      97         [ +  - ]:     182575 :         if (mnNextPos <= SAL_MAX_INT32 - mnDigits)
      98                 :     182575 :             mnNextPos += mnDigits;
      99                 :     182575 :     }
     100                 :            : 
     101                 :     182575 :     void setDigits()
     102                 :            :     {
     103                 :     182575 :         sal_Int32 nPrev = mnDigits;
     104                 :     182575 :         mnDigits = getGrouping();
     105         [ +  + ]:     182575 :         if (!mnDigits)
     106                 :            :         {
     107                 :      71640 :             mnDigits = nPrev;
     108                 :      71640 :             setInfinite();
     109                 :            :         }
     110                 :     182575 :         setPos();
     111                 :     182575 :     }
     112                 :            : 
     113                 :     110935 :     void initGrouping()
     114                 :            :     {
     115                 :     110935 :         mnDigits = 3;       // just in case of constructed with empty grouping
     116                 :     110935 :         mnGroup = 0;
     117                 :     110935 :         mnNextPos = 0;
     118                 :     110935 :         setDigits();
     119                 :     110935 :     }
     120                 :            : 
     121                 :            :     // not implemented, prevent usage
     122                 :            :     DigitGroupingIterator();
     123                 :            :     DigitGroupingIterator( const DigitGroupingIterator & );
     124                 :            :     DigitGroupingIterator & operator=( const DigitGroupingIterator & );
     125                 :            : 
     126                 :            : public:
     127                 :            : 
     128                 :     110114 :     explicit DigitGroupingIterator( const ::com::sun::star::uno::Sequence< sal_Int32 > & rGroupings )
     129                 :     110114 :         : maGroupings( rGroupings)
     130                 :            :     {
     131         [ +  - ]:     110114 :         initGrouping();
     132                 :     110114 :     }
     133                 :            : 
     134                 :            :     /** Advance iterator to next grouping. */
     135                 :      71640 :     DigitGroupingIterator & advance()
     136                 :            :     {
     137         [ -  + ]:      71640 :         if (isInfinite())
     138                 :          0 :             setPos();
     139                 :            :         else
     140                 :            :         {
     141                 :      71640 :             ++mnGroup;
     142                 :      71640 :             setDigits();
     143                 :            :         }
     144                 :      71640 :         return *this;
     145                 :            :     }
     146                 :            : 
     147                 :            :     /** Obtain current grouping. Always > 0. */
     148                 :        804 :     sal_Int32 get() const
     149                 :            :     {
     150                 :        804 :         return mnDigits;
     151                 :            :     }
     152                 :            : 
     153                 :            :     /** The next position (in integer digits) from the right where to insert a
     154                 :            :         group separator. */
     155                 :     331503 :     sal_Int32 getPos()
     156                 :            :     {
     157                 :     331503 :         return mnNextPos;
     158                 :            :     }
     159                 :            : 
     160                 :            :     /** Reset iterator to start again from the right beginning. */
     161                 :        821 :     void reset()
     162                 :            :     {
     163                 :        821 :         initGrouping();
     164                 :        821 :     }
     165                 :            : 
     166                 :            :     /** Create a sequence of bool values containing positions where to add a
     167                 :            :         separator when iterating forward over a string and copying digit per
     168                 :            :         digit. For example, for grouping in thousands and nIntegerDigits==7 the
     169                 :            :         sequence returned would be {1,0,0,1,0,0,0} so the caller would add a
     170                 :            :         separator after the 1st and the 4th digit. */
     171                 :      36223 :     static ::com::sun::star::uno::Sequence< sal_Bool > createForwardSequence(
     172                 :            :             sal_Int32 nIntegerDigits,
     173                 :            :             const ::com::sun::star::uno::Sequence< sal_Int32 > & rGroupings )
     174                 :            :     {
     175         [ -  + ]:      36223 :         if (nIntegerDigits <= 0)
     176         [ #  # ]:          0 :             return ::com::sun::star::uno::Sequence< sal_Bool >();
     177         [ +  - ]:      36223 :         DigitGroupingIterator aIterator( rGroupings);
     178         [ +  - ]:      36223 :         ::com::sun::star::uno::Sequence< sal_Bool > aSeq( nIntegerDigits);
     179         [ +  - ]:      36223 :         sal_Bool* pArr = aSeq.getArray();
     180         [ +  + ]:      99284 :         for (sal_Int32 j = 0; --nIntegerDigits >= 0; ++j)
     181                 :            :         {
     182         [ -  + ]:      63061 :             if (j == aIterator.getPos())
     183                 :            :             {
     184                 :          0 :                 pArr[nIntegerDigits] = sal_True;
     185         [ #  # ]:          0 :                 aIterator.advance();
     186                 :            :             }
     187                 :            :             else
     188                 :      63061 :                 pArr[nIntegerDigits] = sal_False;
     189                 :            :         }
     190 [ +  - ][ +  - ]:      36223 :         return aSeq;
                 [ +  - ]
     191                 :            :     }
     192                 :            : };
     193                 :            : 
     194                 :            : } // namespace utl
     195                 :            : 
     196                 :            : #endif // INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
     197                 :            : 
     198                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10