Home | History | Annotate | Download | only in i18n
      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 * digitaffixesandpadding.h
      9 *
     10 * created on: 2015jan06
     11 * created by: Travis Keep
     12 */
     13 
     14 #ifndef __DIGITAFFIXESANDPADDING_H__
     15 #define __DIGITAFFIXESANDPADDING_H__
     16 
     17 #include "unicode/utypes.h"
     18 
     19 #if !UCONFIG_NO_FORMATTING
     20 
     21 #include "unicode/uobject.h"
     22 #include "pluralaffix.h"
     23 
     24 U_NAMESPACE_BEGIN
     25 
     26 class DigitList;
     27 class ValueFormatter;
     28 class UnicodeString;
     29 class FieldPositionHandler;
     30 class PluralRules;
     31 class VisibleDigitsWithExponent;
     32 
     33 /**
     34  * A formatter of numbers. This class can format any numerical value
     35  * except for not a number (NaN), positive infinity, and negative infinity.
     36  * This class manages prefixes, suffixes, and padding but delegates the
     37  * formatting of actual positive values to a ValueFormatter.
     38  */
     39 class U_I18N_API DigitAffixesAndPadding : public UMemory {
     40 public:
     41 
     42 /**
     43  * Equivalent to DecimalFormat EPadPosition, but redeclared here to prevent
     44  * depending on DecimalFormat which would cause a circular dependency.
     45  */
     46 enum EPadPosition {
     47     kPadBeforePrefix,
     48     kPadAfterPrefix,
     49     kPadBeforeSuffix,
     50     kPadAfterSuffix
     51 };
     52 
     53 /**
     54  * The positive prefix
     55  */
     56 PluralAffix fPositivePrefix;
     57 
     58 /**
     59  * The positive suffix
     60  */
     61 PluralAffix fPositiveSuffix;
     62 
     63 /**
     64  * The negative suffix
     65  */
     66 PluralAffix fNegativePrefix;
     67 
     68 /**
     69  * The negative suffix
     70  */
     71 PluralAffix fNegativeSuffix;
     72 
     73 /**
     74  * The padding position
     75  */
     76 EPadPosition fPadPosition;
     77 
     78 /**
     79  * The padding character.
     80  */
     81 UChar32 fPadChar;
     82 
     83 /**
     84  * The field width in code points. The format method inserts instances of
     85  * the padding character as needed in the desired padding position so that
     86  * the entire formatted string contains this many code points. If the
     87  * formatted string already exceeds this many code points, the format method
     88  * inserts no padding.
     89  */
     90 int32_t fWidth;
     91 
     92 /**
     93  * Pad position is before prefix; padding character is '*' field width is 0.
     94  * The affixes are all the empty string with no annotated fields with just
     95  * the 'other' plural variation.
     96  */
     97 DigitAffixesAndPadding()
     98         : fPadPosition(kPadBeforePrefix), fPadChar(0x2a), fWidth(0) { }
     99 
    100 /**
    101  * Returns TRUE if this object is equal to rhs.
    102  */
    103 UBool equals(const DigitAffixesAndPadding &rhs) const {
    104     return (fPositivePrefix.equals(rhs.fPositivePrefix) &&
    105             fPositiveSuffix.equals(rhs.fPositiveSuffix) &&
    106             fNegativePrefix.equals(rhs.fNegativePrefix) &&
    107             fNegativeSuffix.equals(rhs.fNegativeSuffix) &&
    108             fPadPosition == rhs.fPadPosition &&
    109             fWidth == rhs.fWidth &&
    110             fPadChar == rhs.fPadChar);
    111 }
    112 
    113 /**
    114  * Returns TRUE if a plural rules instance is needed to complete the
    115  * formatting by detecting if any of the affixes have multiple plural
    116  * variations.
    117  */
    118 UBool needsPluralRules() const;
    119 
    120 /**
    121  * Formats value and appends to appendTo.
    122  *
    123  * @param value the value to format. May be NaN or ininite.
    124  * @param formatter handles the details of formatting the actual value.
    125  * @param handler records field positions
    126  * @param optPluralRules the plural rules, but may be NULL if
    127  *   needsPluralRules returns FALSE.
    128  * @appendTo formatted string appended here.
    129  * @status any error returned here.
    130  */
    131 UnicodeString &format(
    132         const VisibleDigitsWithExponent &value,
    133         const ValueFormatter &formatter,
    134         FieldPositionHandler &handler,
    135         const PluralRules *optPluralRules,
    136         UnicodeString &appendTo,
    137         UErrorCode &status) const;
    138 
    139 /**
    140  * For testing only.
    141  */
    142 UnicodeString &format(
    143         DigitList &value,
    144         const ValueFormatter &formatter,
    145         FieldPositionHandler &handler,
    146         const PluralRules *optPluralRules,
    147         UnicodeString &appendTo,
    148         UErrorCode &status) const;
    149 
    150 /**
    151  * Formats a 32-bit integer and appends to appendTo. When formatting an
    152  * integer, this method is preferred to plain format as it can run
    153  * several times faster under certain conditions.
    154  *
    155  * @param value the value to format.
    156  * @param formatter handles the details of formatting the actual value.
    157  * @param handler records field positions
    158  * @param optPluralRules the plural rules, but may be NULL if
    159  *   needsPluralRules returns FALSE.
    160  * @appendTo formatted string appended here.
    161  * @status any error returned here.
    162  */
    163 UnicodeString &formatInt32(
    164         int32_t value,
    165         const ValueFormatter &formatter,
    166         FieldPositionHandler &handler,
    167         const PluralRules *optPluralRules,
    168         UnicodeString &appendTo,
    169         UErrorCode &status) const;
    170 
    171 private:
    172 UnicodeString &appendPadding(int32_t paddingCount, UnicodeString &appendTo) const;
    173 
    174 };
    175 
    176 
    177 U_NAMESPACE_END
    178 #endif /* #if !UCONFIG_NO_FORMATTING */
    179 #endif  // __DIGITAFFIXANDPADDING_H__
    180