listformatter.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. *******************************************************************************
  3. *
  4. * Copyright (C) 2012-2016, International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *
  7. *******************************************************************************
  8. * file name: listformatter.h
  9. * encoding: US-ASCII
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * created on: 20120426
  14. * created by: Umesh P. Nair
  15. */
  16. #ifndef __LISTFORMATTER_H__
  17. #define __LISTFORMATTER_H__
  18. #include "unicode/utypes.h"
  19. #include "unicode/unistr.h"
  20. #include "unicode/locid.h"
  21. U_NAMESPACE_BEGIN
  22. /** @internal */
  23. class Hashtable;
  24. /** @internal */
  25. struct ListFormatInternal;
  26. /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
  27. /** @internal */
  28. struct ListFormatData : public UMemory {
  29. UnicodeString twoPattern;
  30. UnicodeString startPattern;
  31. UnicodeString middlePattern;
  32. UnicodeString endPattern;
  33. ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end) :
  34. twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end) {}
  35. };
  36. /**
  37. * \file
  38. * \brief C++ API: API for formatting a list.
  39. */
  40. /**
  41. * An immutable class for formatting a list, using data from CLDR (or supplied
  42. * separately).
  43. *
  44. * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
  45. * as "Alice, Bob, Charlie and Delta" in English.
  46. *
  47. * The ListFormatter class is not intended for public subclassing.
  48. * @stable ICU 50
  49. */
  50. class U_COMMON_API ListFormatter : public UObject{
  51. public:
  52. /**
  53. * Copy constructor.
  54. * @stable ICU 52
  55. */
  56. ListFormatter(const ListFormatter&);
  57. /**
  58. * Assignment operator.
  59. * @stable ICU 52
  60. */
  61. ListFormatter& operator=(const ListFormatter& other);
  62. /**
  63. * Creates a ListFormatter appropriate for the default locale.
  64. *
  65. * @param errorCode ICU error code, set if no data available for default locale.
  66. * @return Pointer to a ListFormatter object for the default locale,
  67. * created from internal data derived from CLDR data.
  68. * @stable ICU 50
  69. */
  70. static ListFormatter* createInstance(UErrorCode& errorCode);
  71. /**
  72. * Creates a ListFormatter appropriate for a locale.
  73. *
  74. * @param locale The locale.
  75. * @param errorCode ICU error code, set if no data available for the given locale.
  76. * @return A ListFormatter object created from internal data derived from
  77. * CLDR data.
  78. * @stable ICU 50
  79. */
  80. static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
  81. #ifndef U_HIDE_INTERNAL_API
  82. /**
  83. * Creates a ListFormatter appropriate for a locale and style.
  84. *
  85. * @param locale The locale.
  86. * @param style the style, either "standard", "duration", or "duration-short"
  87. * @param errorCode ICU error code, set if no data available for the given locale.
  88. * @return A ListFormatter object created from internal data derived from
  89. * CLDR data.
  90. * @internal
  91. */
  92. static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
  93. #endif /* U_HIDE_INTERNAL_API */
  94. /**
  95. * Destructor.
  96. *
  97. * @stable ICU 50
  98. */
  99. virtual ~ListFormatter();
  100. /**
  101. * Formats a list of strings.
  102. *
  103. * @param items An array of strings to be combined and formatted.
  104. * @param n_items Length of the array items.
  105. * @param appendTo The string to which the result should be appended to.
  106. * @param errorCode ICU error code, set if there is an error.
  107. * @return Formatted string combining the elements of items, appended to appendTo.
  108. * @stable ICU 50
  109. */
  110. UnicodeString& format(const UnicodeString items[], int32_t n_items,
  111. UnicodeString& appendTo, UErrorCode& errorCode) const;
  112. #ifndef U_HIDE_INTERNAL_API
  113. /**
  114. @internal for MeasureFormat
  115. */
  116. UnicodeString& format(
  117. const UnicodeString items[],
  118. int32_t n_items,
  119. UnicodeString& appendTo,
  120. int32_t index,
  121. int32_t &offset,
  122. UErrorCode& errorCode) const;
  123. /**
  124. * @internal constructor made public for testing.
  125. */
  126. ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
  127. /**
  128. * @internal constructor made public for testing.
  129. */
  130. ListFormatter(const ListFormatInternal* listFormatterInternal);
  131. #endif /* U_HIDE_INTERNAL_API */
  132. private:
  133. static void initializeHash(UErrorCode& errorCode);
  134. static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
  135. ListFormatter();
  136. ListFormatInternal* owned;
  137. const ListFormatInternal* data;
  138. };
  139. U_NAMESPACE_END
  140. #endif