strrepl.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (c) 2002-2011, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. **********************************************************************
  8. * Date Name Description
  9. * 01/21/2002 aliu Creation.
  10. **********************************************************************
  11. */
  12. #ifndef STRREPL_H
  13. #define STRREPL_H
  14. #include "unicode/utypes.h"
  15. #if !UCONFIG_NO_TRANSLITERATION
  16. #include "unicode/unifunct.h"
  17. #include "unicode/unirepl.h"
  18. #include "unicode/unistr.h"
  19. U_NAMESPACE_BEGIN
  20. class TransliterationRuleData;
  21. /**
  22. * A replacer that produces static text as its output. The text may
  23. * contain transliterator stand-in characters that represent nested
  24. * UnicodeReplacer objects, making it possible to encode a tree of
  25. * replacers in a StringReplacer. A StringReplacer that contains such
  26. * stand-ins is called a <em>complex</em> StringReplacer. A complex
  27. * StringReplacer has a slower processing loop than a non-complex one.
  28. * @author Alan Liu
  29. */
  30. class StringReplacer : public UnicodeFunctor, public UnicodeReplacer {
  31. private:
  32. /**
  33. * Output text, possibly containing stand-in characters that
  34. * represent nested UnicodeReplacers.
  35. */
  36. UnicodeString output;
  37. /**
  38. * Cursor position. Value is ignored if hasCursor is false.
  39. */
  40. int32_t cursorPos;
  41. /**
  42. * True if this object outputs a cursor position.
  43. */
  44. UBool hasCursor;
  45. /**
  46. * A complex object contains nested replacers and requires more
  47. * complex processing. StringReplacers are initially assumed to
  48. * be complex. If no nested replacers are seen during processing,
  49. * then isComplex is set to false, and future replacements are
  50. * short circuited for better performance.
  51. */
  52. UBool isComplex;
  53. /**
  54. * Object that translates stand-in characters in 'output' to
  55. * UnicodeReplacer objects.
  56. */
  57. const TransliterationRuleData* data;
  58. public:
  59. /**
  60. * Construct a StringReplacer that sets the emits the given output
  61. * text and sets the cursor to the given position.
  62. * @param theOutput text that will replace input text when the
  63. * replace() method is called. May contain stand-in characters
  64. * that represent nested replacers.
  65. * @param theCursorPos cursor position that will be returned by
  66. * the replace() method
  67. * @param theData transliterator context object that translates
  68. * stand-in characters to UnicodeReplacer objects
  69. */
  70. StringReplacer(const UnicodeString& theOutput,
  71. int32_t theCursorPos,
  72. const TransliterationRuleData* theData);
  73. /**
  74. * Construct a StringReplacer that sets the emits the given output
  75. * text and does not modify the cursor.
  76. * @param theOutput text that will replace input text when the
  77. * replace() method is called. May contain stand-in characters
  78. * that represent nested replacers.
  79. * @param theData transliterator context object that translates
  80. * stand-in characters to UnicodeReplacer objects
  81. */
  82. StringReplacer(const UnicodeString& theOutput,
  83. const TransliterationRuleData* theData);
  84. /**
  85. * Copy constructor.
  86. */
  87. StringReplacer(const StringReplacer& other);
  88. /**
  89. * Destructor
  90. */
  91. virtual ~StringReplacer();
  92. /**
  93. * Implement UnicodeFunctor
  94. */
  95. virtual StringReplacer* clone() const;
  96. /**
  97. * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
  98. * and return the pointer.
  99. */
  100. virtual UnicodeReplacer* toReplacer() const;
  101. /**
  102. * UnicodeReplacer API
  103. */
  104. virtual int32_t replace(Replaceable& text,
  105. int32_t start,
  106. int32_t limit,
  107. int32_t& cursor);
  108. /**
  109. * UnicodeReplacer API
  110. */
  111. virtual UnicodeString& toReplacerPattern(UnicodeString& result,
  112. UBool escapeUnprintable) const;
  113. /**
  114. * Implement UnicodeReplacer
  115. */
  116. virtual void addReplacementSetTo(UnicodeSet& toUnionTo) const;
  117. /**
  118. * UnicodeFunctor API
  119. */
  120. virtual void setData(const TransliterationRuleData*);
  121. /**
  122. * ICU "poor man's RTTI", returns a UClassID for this class.
  123. */
  124. static UClassID U_EXPORT2 getStaticClassID();
  125. /**
  126. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  127. */
  128. virtual UClassID getDynamicClassID() const;
  129. };
  130. U_NAMESPACE_END
  131. #endif /* #if !UCONFIG_NO_TRANSLITERATION */
  132. #endif
  133. //eof