1/*
2 * Copyright (C) 2015, International Business Machines
3 * Corporation and others.  All Rights Reserved.
4 *
5 * file name: digitgrouping.cpp
6 */
7
8#include "unicode/utypes.h"
9
10#include "digitgrouping.h"
11#include "smallintformatter.h"
12
13U_NAMESPACE_BEGIN
14
15UBool DigitGrouping::isSeparatorAt(
16        int32_t digitsLeftOfDecimal, int32_t digitPos) const {
17    if (!isGroupingEnabled(digitsLeftOfDecimal) || digitPos < fGrouping) {
18        return FALSE;
19    }
20    return ((digitPos - fGrouping) % getGrouping2() == 0);
21}
22
23int32_t DigitGrouping::getSeparatorCount(int32_t digitsLeftOfDecimal) const {
24    if (!isGroupingEnabled(digitsLeftOfDecimal)) {
25        return 0;
26    }
27    return (digitsLeftOfDecimal - 1 - fGrouping) / getGrouping2() + 1;
28}
29
30UBool DigitGrouping::isGroupingEnabled(int32_t digitsLeftOfDecimal) const {
31    return (isGroupingUsed()
32            && digitsLeftOfDecimal >= fGrouping + getMinGrouping());
33}
34
35UBool DigitGrouping::isNoGrouping(
36        int32_t positiveValue, const IntDigitCountRange &range) const {
37    return getSeparatorCount(
38            SmallIntFormatter::estimateDigitCount(positiveValue, range)) == 0;
39}
40
41int32_t DigitGrouping::getGrouping2() const {
42    return (fGrouping2 > 0 ? fGrouping2 : fGrouping);
43}
44
45int32_t DigitGrouping::getMinGrouping() const {
46    return (fMinGrouping > 0 ? fMinGrouping : 1);
47}
48
49void
50DigitGrouping::clear() {
51    fMinGrouping = 0;
52    fGrouping = 0;
53    fGrouping2 = 0;
54}
55
56U_NAMESPACE_END
57
58