bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / unotools / digitgroupingiterator.hxx
blob7206f72f1374f4f40877cb5159606aaec449b9fa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
21 #define INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <sal/log.hxx>
26 namespace utl {
28 /** Iterator to be used with a digit grouping as obtained through
29 LocaleDataWrapper::getDigitGrouping().
31 The iterator advances over the digit groupings, returning the number of
32 digits per group. If the last group was encountered the iterator will
33 always return the last grouping.
35 Grouping values are sanitized to be >= 0, even if originally signed
36 sal_Int32.
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 characters into
44 strings.
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)
52 if (++nCount >= aGrouping.getPos())
54 aBuffer.insert( n, cSeparator);
55 nGroupDigits = aGrouping.advance();
61 class DigitGroupingIterator
63 const css::uno::Sequence< sal_Int32 > maGroupings;
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
69 void setInfinite()
71 mnGroup = maGroupings.getLength();
74 bool isInfinite() const
76 return mnGroup >= maGroupings.getLength();
79 sal_Int32 getGrouping() const
81 if (mnGroup < maGroupings.getLength())
83 sal_Int32 n = maGroupings[mnGroup];
84 SAL_WARN_IF( n < 0, "unotools.i18n", "DigitGroupingIterator::getGrouping: negative grouping");
85 if (n < 0)
86 n = 0; // sanitize ...
87 return n;
89 return 0;
92 void setPos()
94 // someone might be playing jokes on us, so check for overflow
95 if (mnNextPos <= SAL_MAX_INT32 - mnDigits)
96 mnNextPos += mnDigits;
99 void setDigits()
101 sal_Int32 nPrev = mnDigits;
102 mnDigits = getGrouping();
103 if (!mnDigits)
105 mnDigits = nPrev;
106 setInfinite();
108 setPos();
111 void initGrouping()
113 mnDigits = 3; // just in case of constructed with empty grouping
114 mnGroup = 0;
115 mnNextPos = 0;
116 setDigits();
119 DigitGroupingIterator( const DigitGroupingIterator & ) = delete;
120 DigitGroupingIterator & operator=( const DigitGroupingIterator & ) = delete;
122 public:
124 explicit DigitGroupingIterator( const css::uno::Sequence< sal_Int32 > & rGroupings )
125 : maGroupings( rGroupings)
127 initGrouping();
130 /** Advance iterator to next grouping. */
131 DigitGroupingIterator & advance()
133 if (isInfinite())
134 setPos();
135 else
137 ++mnGroup;
138 setDigits();
140 return *this;
143 /** Obtain current grouping. Always > 0. */
144 sal_Int32 get() const
146 return mnDigits;
149 /** The next position (in integer digits) from the right where to insert a
150 group separator. */
151 sal_Int32 getPos() const
153 return mnNextPos;
156 /** Reset iterator to start again from the right beginning. */
157 void reset()
159 initGrouping();
162 /** Create a sequence of bool values containing positions where to add a
163 separator when iterating forward over a string and copying digit per
164 digit. For example, for grouping in thousands and nIntegerDigits==7 the
165 sequence returned would be {1,0,0,1,0,0,0} so the caller would add a
166 separator after the 1st and the 4th digit. */
167 static css::uno::Sequence< sal_Bool > createForwardSequence(
168 sal_Int32 nIntegerDigits,
169 const css::uno::Sequence< sal_Int32 > & rGroupings )
171 if (nIntegerDigits <= 0)
172 return css::uno::Sequence< sal_Bool >();
173 DigitGroupingIterator aIterator( rGroupings);
174 css::uno::Sequence< sal_Bool > aSeq( nIntegerDigits);
175 sal_Bool* pArr = aSeq.getArray();
176 for (sal_Int32 j = 0; --nIntegerDigits >= 0; ++j)
178 if (j == aIterator.getPos())
180 pArr[nIntegerDigits] = true;
181 aIterator.advance();
183 else
184 pArr[nIntegerDigits] = false;
186 return aSeq;
190 } // namespace utl
192 #endif // INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */