LibreOffice Module unotools (master) 1
digitgroupingiterator.hxx
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#ifndef INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
21#define INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
22
23#include <com/sun/star/uno/Sequence.hxx>
24#include <sal/log.hxx>
25
26namespace utl {
27
62{
63 const css::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
70 {
71 mnGroup = maGroupings.getLength();
72 }
73
74 bool isInfinite() const
75 {
76 return mnGroup >= maGroupings.getLength();
77 }
78
79 sal_Int32 getGrouping() const
80 {
81 if (mnGroup < maGroupings.getLength())
82 {
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;
88 }
89 return 0;
90 }
91
92 void setPos()
93 {
94 // someone might be playing jokes on us, so check for overflow
97 }
98
99 void setDigits()
100 {
101 sal_Int32 nPrev = mnDigits;
103 if (!mnDigits)
104 {
105 mnDigits = nPrev;
106 setInfinite();
107 }
108 setPos();
109 }
110
112 {
113 mnDigits = 3; // just in case of constructed with empty grouping
114 mnGroup = 0;
115 mnNextPos = 0;
116 setDigits();
117 }
118
121
122public:
123
124 explicit DigitGroupingIterator( const css::uno::Sequence< sal_Int32 > & rGroupings )
125 : maGroupings( rGroupings)
126 {
127 initGrouping();
128 }
129
132 {
133 if (isInfinite())
134 setPos();
135 else
136 {
137 ++mnGroup;
138 setDigits();
139 }
140 return *this;
141 }
142
144 sal_Int32 get() const
145 {
146 return mnDigits;
147 }
148
151 sal_Int32 getPos() const
152 {
153 return mnNextPos;
154 }
155
157 void reset()
158 {
159 initGrouping();
160 }
161
167 static css::uno::Sequence< sal_Bool > createForwardSequence(
168 sal_Int32 nIntegerDigits,
169 const css::uno::Sequence< sal_Int32 > & rGroupings )
170 {
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)
177 {
178 if (j == aIterator.getPos())
179 {
180 pArr[nIntegerDigits] = true;
181 aIterator.advance();
182 }
183 else
184 pArr[nIntegerDigits] = false;
185 }
186 return aSeq;
187 }
188};
189
190} // namespace utl
191
192#endif // INCLUDED_UNOTOOLS_DIGITGROUPINGITERATOR_HXX
193
194/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Iterator to be used with a digit grouping as obtained through LocaleDataWrapper::getDigitGrouping().
void reset()
Reset iterator to start again from the right beginning.
const css::uno::Sequence< sal_Int32 > maGroupings
DigitGroupingIterator & advance()
Advance iterator to next grouping.
sal_Int32 get() const
Obtain current grouping.
DigitGroupingIterator(const css::uno::Sequence< sal_Int32 > &rGroupings)
DigitGroupingIterator(const DigitGroupingIterator &)=delete
sal_Int32 getPos() const
The next position (in integer digits) from the right where to insert a group separator.
static css::uno::Sequence< sal_Bool > createForwardSequence(sal_Int32 nIntegerDigits, const css::uno::Sequence< sal_Int32 > &rGroupings)
Create a sequence of bool values containing positions where to add a separator when iterating forward...
DigitGroupingIterator & operator=(const DigitGroupingIterator &)=delete
sal_Int64 n
Sequence< sal_Int8 > aSeq
#define SAL_WARN_IF(condition, area, stream)
#define SAL_MAX_INT32
unsigned char sal_Bool