simpleformatter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. ******************************************************************************
  3. * Copyright (C) 2014-2016, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. ******************************************************************************
  6. * simpleformatter.h
  7. */
  8. #ifndef __SIMPLEFORMATTER_H__
  9. #define __SIMPLEFORMATTER_H__
  10. /**
  11. * \file
  12. * \brief C++ API: Simple formatter, minimal subset of MessageFormat.
  13. */
  14. #include "unicode/utypes.h"
  15. #include "unicode/unistr.h"
  16. #ifndef U_HIDE_DRAFT_API
  17. U_NAMESPACE_BEGIN
  18. /**
  19. * Formats simple patterns like "{1} was born in {0}".
  20. * Minimal subset of MessageFormat; fast, simple, minimal dependencies.
  21. * Supports only numbered arguments with no type nor style parameters,
  22. * and formats only string values.
  23. * Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.
  24. *
  25. * Factory methods set error codes for syntax errors
  26. * and for too few or too many arguments/placeholders.
  27. *
  28. * SimpleFormatter objects are thread-safe except for assignment and applying new patterns.
  29. *
  30. * Example:
  31. * <pre>
  32. * UErrorCode errorCode = U_ZERO_ERROR;
  33. * SimpleFormatter fmt("{1} '{born}' in {0}", errorCode);
  34. * UnicodeString result;
  35. *
  36. * // Output: "paul {born} in england"
  37. * fmt.format("england", "paul", result, errorCode);
  38. * </pre>
  39. *
  40. * This class is not intended for public subclassing.
  41. *
  42. * @see MessageFormat
  43. * @see UMessagePatternApostropheMode
  44. * @draft ICU 57
  45. */
  46. class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {
  47. public:
  48. /**
  49. * Default constructor.
  50. * @draft ICU 57
  51. */
  52. SimpleFormatter() : compiledPattern((UChar)0) {}
  53. /**
  54. * Constructs a formatter from the pattern string.
  55. *
  56. * @param pattern The pattern string.
  57. * @param errorCode ICU error code in/out parameter.
  58. * Must fulfill U_SUCCESS before the function call.
  59. * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.
  60. * @draft ICU 57
  61. */
  62. SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
  63. applyPattern(pattern, errorCode);
  64. }
  65. /**
  66. * Constructs a formatter from the pattern string.
  67. * The number of arguments checked against the given limits is the
  68. * highest argument number plus one, not the number of occurrences of arguments.
  69. *
  70. * @param pattern The pattern string.
  71. * @param min The pattern must have at least this many arguments.
  72. * @param max The pattern must have at most this many arguments.
  73. * @param errorCode ICU error code in/out parameter.
  74. * Must fulfill U_SUCCESS before the function call.
  75. * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and
  76. * too few or too many arguments.
  77. * @draft ICU 57
  78. */
  79. SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
  80. UErrorCode &errorCode) {
  81. applyPatternMinMaxArguments(pattern, min, max, errorCode);
  82. }
  83. /**
  84. * Copy constructor.
  85. * @draft ICU 57
  86. */
  87. SimpleFormatter(const SimpleFormatter& other)
  88. : compiledPattern(other.compiledPattern) {}
  89. /**
  90. * Assignment operator.
  91. * @draft ICU 57
  92. */
  93. SimpleFormatter &operator=(const SimpleFormatter& other);
  94. /**
  95. * Destructor.
  96. * @draft ICU 57
  97. */
  98. ~SimpleFormatter();
  99. /**
  100. * Changes this object according to the new pattern.
  101. *
  102. * @param pattern The pattern string.
  103. * @param errorCode ICU error code in/out parameter.
  104. * Must fulfill U_SUCCESS before the function call.
  105. * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax.
  106. * @return TRUE if U_SUCCESS(errorCode).
  107. * @draft ICU 57
  108. */
  109. UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
  110. return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
  111. }
  112. /**
  113. * Changes this object according to the new pattern.
  114. * The number of arguments checked against the given limits is the
  115. * highest argument number plus one, not the number of occurrences of arguments.
  116. *
  117. * @param pattern The pattern string.
  118. * @param min The pattern must have at least this many arguments.
  119. * @param max The pattern must have at most this many arguments.
  120. * @param errorCode ICU error code in/out parameter.
  121. * Must fulfill U_SUCCESS before the function call.
  122. * Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and
  123. * too few or too many arguments.
  124. * @return TRUE if U_SUCCESS(errorCode).
  125. * @draft ICU 57
  126. */
  127. UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
  128. int32_t min, int32_t max, UErrorCode &errorCode);
  129. /**
  130. * @return The max argument number + 1.
  131. * @draft ICU 57
  132. */
  133. int32_t getArgumentLimit() const {
  134. return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
  135. }
  136. /**
  137. * Formats the given value, appending to the appendTo builder.
  138. * The argument value must not be the same object as appendTo.
  139. * getArgumentLimit() must be at most 1.
  140. *
  141. * @param value0 Value for argument {0}.
  142. * @param appendTo Gets the formatted pattern and value appended.
  143. * @param errorCode ICU error code in/out parameter.
  144. * Must fulfill U_SUCCESS before the function call.
  145. * @return appendTo
  146. * @draft ICU 57
  147. */
  148. UnicodeString &format(
  149. const UnicodeString &value0,
  150. UnicodeString &appendTo, UErrorCode &errorCode) const;
  151. /**
  152. * Formats the given values, appending to the appendTo builder.
  153. * An argument value must not be the same object as appendTo.
  154. * getArgumentLimit() must be at most 2.
  155. *
  156. * @param value0 Value for argument {0}.
  157. * @param value1 Value for argument {1}.
  158. * @param appendTo Gets the formatted pattern and values appended.
  159. * @param errorCode ICU error code in/out parameter.
  160. * Must fulfill U_SUCCESS before the function call.
  161. * @return appendTo
  162. * @draft ICU 57
  163. */
  164. UnicodeString &format(
  165. const UnicodeString &value0,
  166. const UnicodeString &value1,
  167. UnicodeString &appendTo, UErrorCode &errorCode) const;
  168. /**
  169. * Formats the given values, appending to the appendTo builder.
  170. * An argument value must not be the same object as appendTo.
  171. * getArgumentLimit() must be at most 3.
  172. *
  173. * @param value0 Value for argument {0}.
  174. * @param value1 Value for argument {1}.
  175. * @param value2 Value for argument {2}.
  176. * @param appendTo Gets the formatted pattern and values appended.
  177. * @param errorCode ICU error code in/out parameter.
  178. * Must fulfill U_SUCCESS before the function call.
  179. * @return appendTo
  180. * @draft ICU 57
  181. */
  182. UnicodeString &format(
  183. const UnicodeString &value0,
  184. const UnicodeString &value1,
  185. const UnicodeString &value2,
  186. UnicodeString &appendTo, UErrorCode &errorCode) const;
  187. /**
  188. * Formats the given values, appending to the appendTo string.
  189. *
  190. * @param values The argument values.
  191. * An argument value must not be the same object as appendTo.
  192. * Can be NULL if valuesLength==getArgumentLimit()==0.
  193. * @param valuesLength The length of the values array.
  194. * Must be at least getArgumentLimit().
  195. * @param appendTo Gets the formatted pattern and values appended.
  196. * @param offsets offsets[i] receives the offset of where
  197. * values[i] replaced pattern argument {i}.
  198. * Can be shorter or longer than values. Can be NULL if offsetsLength==0.
  199. * If there is no {i} in the pattern, then offsets[i] is set to -1.
  200. * @param offsetsLength The length of the offsets array.
  201. * @param errorCode ICU error code in/out parameter.
  202. * Must fulfill U_SUCCESS before the function call.
  203. * @return appendTo
  204. * @draft ICU 57
  205. */
  206. UnicodeString &formatAndAppend(
  207. const UnicodeString *const *values, int32_t valuesLength,
  208. UnicodeString &appendTo,
  209. int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
  210. /**
  211. * Formats the given values, replacing the contents of the result string.
  212. * May optimize by actually appending to the result if it is the same object
  213. * as the value corresponding to the initial argument in the pattern.
  214. *
  215. * @param values The argument values.
  216. * An argument value may be the same object as result.
  217. * Can be NULL if valuesLength==getArgumentLimit()==0.
  218. * @param valuesLength The length of the values array.
  219. * Must be at least getArgumentLimit().
  220. * @param result Gets its contents replaced by the formatted pattern and values.
  221. * @param offsets offsets[i] receives the offset of where
  222. * values[i] replaced pattern argument {i}.
  223. * Can be shorter or longer than values. Can be NULL if offsetsLength==0.
  224. * If there is no {i} in the pattern, then offsets[i] is set to -1.
  225. * @param offsetsLength The length of the offsets array.
  226. * @param errorCode ICU error code in/out parameter.
  227. * Must fulfill U_SUCCESS before the function call.
  228. * @return result
  229. * @draft ICU 57
  230. */
  231. UnicodeString &formatAndReplace(
  232. const UnicodeString *const *values, int32_t valuesLength,
  233. UnicodeString &result,
  234. int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
  235. /**
  236. * Returns the pattern text with none of the arguments.
  237. * Like formatting with all-empty string values.
  238. * @draft ICU 57
  239. */
  240. UnicodeString getTextWithNoArguments() const {
  241. return getTextWithNoArguments(compiledPattern.getBuffer(), compiledPattern.length());
  242. }
  243. private:
  244. /**
  245. * Binary representation of the compiled pattern.
  246. * Index 0: One more than the highest argument number.
  247. * Followed by zero or more arguments or literal-text segments.
  248. *
  249. * An argument is stored as its number, less than ARG_NUM_LIMIT.
  250. * A literal-text segment is stored as its length (at least 1) offset by ARG_NUM_LIMIT,
  251. * followed by that many chars.
  252. */
  253. UnicodeString compiledPattern;
  254. static inline int32_t getArgumentLimit(const UChar *compiledPattern,
  255. int32_t compiledPatternLength) {
  256. return compiledPatternLength == 0 ? 0 : compiledPattern[0];
  257. }
  258. static UnicodeString getTextWithNoArguments(const UChar *compiledPattern, int32_t compiledPatternLength);
  259. static UnicodeString &format(
  260. const UChar *compiledPattern, int32_t compiledPatternLength,
  261. const UnicodeString *const *values,
  262. UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
  263. int32_t *offsets, int32_t offsetsLength,
  264. UErrorCode &errorCode);
  265. };
  266. U_NAMESPACE_END
  267. #endif /* U_HIDE_DRAFT_API */
  268. #endif // __SIMPLEFORMATTER_H__