listformatter.h 4.8 KB

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