Line data Source code
1 : // © 2016 and later: Unicode, Inc. and others.
2 : // License & terms of use: http://www.unicode.org/copyright.html
3 : /*
4 : *******************************************************************************
5 : * Copyright (C) 2015, International Business Machines
6 : * Corporation and others. All Rights Reserved.
7 : *******************************************************************************
8 : * smallintformatter.h
9 : *
10 : * created on: 2015jan06
11 : * created by: Travis Keep
12 : */
13 :
14 : #ifndef __SMALLINTFORMATTER_H__
15 : #define __SMALLINTFORMATTER_H__
16 :
17 : #include "unicode/uobject.h"
18 : #include "unicode/utypes.h"
19 :
20 : U_NAMESPACE_BEGIN
21 :
22 : class UnicodeString;
23 :
24 : /**
25 : * A representation an acceptable range of digit counts for integers.
26 : */
27 : class U_I18N_API IntDigitCountRange : public UMemory {
28 : public:
29 : /**
30 : * No constraints: 0 up to INT32_MAX
31 : */
32 : IntDigitCountRange() : fMin(0), fMax(INT32_MAX) { }
33 : IntDigitCountRange(int32_t min, int32_t max);
34 : int32_t pin(int32_t digitCount) const;
35 0 : int32_t getMax() const { return fMax; }
36 0 : int32_t getMin() const { return fMin; }
37 : private:
38 : int32_t fMin;
39 : int32_t fMax;
40 : };
41 :
42 :
43 : /**
44 : * A formatter for small, positive integers.
45 : */
46 : class U_I18N_API SmallIntFormatter : public UMemory {
47 : public:
48 : /**
49 : * Estimates the actual digit count needed to format positiveValue
50 : * using the given range of digit counts.
51 : * Returns a value that is at least the actual digit count needed.
52 : *
53 : * @param positiveValue the value to format
54 : * @param range the acceptable range of digit counts.
55 : */
56 : static int32_t estimateDigitCount(
57 : int32_t positiveValue, const IntDigitCountRange &range);
58 :
59 : /**
60 : * Returns TRUE if this class can format positiveValue using
61 : * the given range of digit counts.
62 : *
63 : * @param positiveValue the value to format
64 : * @param range the acceptable range of digit counts.
65 : */
66 : static UBool canFormat(
67 : int32_t positiveValue, const IntDigitCountRange &range);
68 :
69 : /**
70 : * Formats positiveValue using the given range of digit counts.
71 : * Always uses standard digits '0' through '9'. Formatted value is
72 : * left padded with '0' as necessary to achieve minimum digit count.
73 : * Does not produce any grouping separators or trailing decimal point.
74 : * Calling format to format a value with a particular digit count range
75 : * when canFormat indicates that the same value and digit count range
76 : * cannot be formatted results in undefined behavior.
77 : *
78 : * @param positiveValue the value to format
79 : * @param range the acceptable range of digit counts.
80 : */
81 : static UnicodeString &format(
82 : int32_t positiveValue,
83 : const IntDigitCountRange &range,
84 : UnicodeString &appendTo);
85 :
86 : };
87 :
88 : U_NAMESPACE_END
89 :
90 : #endif // __SMALLINTFORMATTER_H__
|