ICU 73.2  73.2
simpleformatter.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004 ******************************************************************************
00005 * Copyright (C) 2014-2016, International Business Machines
00006 * Corporation and others.  All Rights Reserved.
00007 ******************************************************************************
00008 * simpleformatter.h
00009 */
00010 
00011 #ifndef __SIMPLEFORMATTER_H__
00012 #define __SIMPLEFORMATTER_H__
00013 
00019 #include "unicode/utypes.h"
00020 
00021 #if U_SHOW_CPLUSPLUS_API
00022 
00023 #include "unicode/unistr.h"
00024 
00025 U_NAMESPACE_BEGIN
00026 
00027 // Forward declaration:
00028 namespace number {
00029 namespace impl {
00030 class SimpleModifier;
00031 }
00032 }
00033 
00062 class U_COMMON_API SimpleFormatter final : public UMemory {
00063 public:
00068     SimpleFormatter() : compiledPattern((char16_t)0) {}
00069 
00079     SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
00080         applyPattern(pattern, errorCode);
00081     }
00082 
00097     SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
00098                     UErrorCode &errorCode) {
00099         applyPatternMinMaxArguments(pattern, min, max, errorCode);
00100     }
00101 
00106     SimpleFormatter(const SimpleFormatter& other)
00107             : compiledPattern(other.compiledPattern) {}
00108 
00113     SimpleFormatter &operator=(const SimpleFormatter& other);
00114 
00119     ~SimpleFormatter();
00120 
00131     UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
00132         return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
00133     }
00134 
00150     UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
00151                                       int32_t min, int32_t max, UErrorCode &errorCode);
00152 
00157     int32_t getArgumentLimit() const {
00158         return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
00159     }
00160 
00173     UnicodeString &format(
00174             const UnicodeString &value0,
00175             UnicodeString &appendTo, UErrorCode &errorCode) const;
00176 
00190     UnicodeString &format(
00191             const UnicodeString &value0,
00192             const UnicodeString &value1,
00193             UnicodeString &appendTo, UErrorCode &errorCode) const;
00194 
00209     UnicodeString &format(
00210             const UnicodeString &value0,
00211             const UnicodeString &value1,
00212             const UnicodeString &value2,
00213             UnicodeString &appendTo, UErrorCode &errorCode) const;
00214 
00234     UnicodeString &formatAndAppend(
00235             const UnicodeString *const *values, int32_t valuesLength,
00236             UnicodeString &appendTo,
00237             int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
00238 
00260     UnicodeString &formatAndReplace(
00261             const UnicodeString *const *values, int32_t valuesLength,
00262             UnicodeString &result,
00263             int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
00264 
00270     UnicodeString getTextWithNoArguments() const {
00271         return getTextWithNoArguments(
00272             compiledPattern.getBuffer(),
00273             compiledPattern.length(),
00274             nullptr,
00275             0);
00276     }
00277 
00278 #ifndef U_HIDE_INTERNAL_API
00279 
00294     UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const {
00295         return getTextWithNoArguments(
00296             compiledPattern.getBuffer(),
00297             compiledPattern.length(),
00298             offsets,
00299             offsetsLength);
00300     }
00301 #endif // U_HIDE_INTERNAL_API
00302 
00303 private:
00313     UnicodeString compiledPattern;
00314 
00315     static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
00316                                               int32_t compiledPatternLength) {
00317         return compiledPatternLength == 0 ? 0 : compiledPattern[0];
00318     }
00319 
00320     static UnicodeString getTextWithNoArguments(
00321         const char16_t *compiledPattern,
00322         int32_t compiledPatternLength,
00323         int32_t *offsets,
00324         int32_t offsetsLength);
00325 
00326     static UnicodeString &format(
00327             const char16_t *compiledPattern, int32_t compiledPatternLength,
00328             const UnicodeString *const *values,
00329             UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
00330             int32_t *offsets, int32_t offsetsLength,
00331             UErrorCode &errorCode);
00332 
00333     // Give access to internals to SimpleModifier for number formatting
00334     friend class number::impl::SimpleModifier;
00335 };
00336 
00337 U_NAMESPACE_END
00338 
00339 #endif /* U_SHOW_CPLUSPLUS_API */
00340 
00341 #endif  // __SIMPLEFORMATTER_H__