funcrepl.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-2012, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. **********************************************************************
  8. * Date Name Description
  9. * 02/04/2002 aliu Creation.
  10. **********************************************************************
  11. */
  12. #include "unicode/utypes.h"
  13. #if !UCONFIG_NO_TRANSLITERATION
  14. #include "unicode/translit.h"
  15. #include "unicode/uniset.h"
  16. #include "funcrepl.h"
  17. static const char16_t AMPERSAND = 38; // '&'
  18. static const char16_t OPEN[] = {40,32,0}; // "( "
  19. static const char16_t CLOSE[] = {32,41,0}; // " )"
  20. U_NAMESPACE_BEGIN
  21. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer)
  22. /**
  23. * Construct a replacer that takes the output of the given
  24. * replacer, passes it through the given transliterator, and emits
  25. * the result as output.
  26. */
  27. FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit,
  28. UnicodeFunctor* adoptedReplacer) {
  29. translit = adoptedTranslit;
  30. replacer = adoptedReplacer;
  31. }
  32. /**
  33. * Copy constructor.
  34. */
  35. FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) :
  36. UnicodeFunctor(other),
  37. UnicodeReplacer(other)
  38. {
  39. translit = other.translit->clone();
  40. replacer = other.replacer->clone();
  41. }
  42. /**
  43. * Destructor
  44. */
  45. FunctionReplacer::~FunctionReplacer() {
  46. delete translit;
  47. delete replacer;
  48. }
  49. /**
  50. * Implement UnicodeFunctor
  51. */
  52. FunctionReplacer* FunctionReplacer::clone() const {
  53. return new FunctionReplacer(*this);
  54. }
  55. /**
  56. * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
  57. * and return the pointer.
  58. */
  59. UnicodeReplacer* FunctionReplacer::toReplacer() const {
  60. FunctionReplacer *nonconst_this = const_cast<FunctionReplacer *>(this);
  61. UnicodeReplacer *nonconst_base = static_cast<UnicodeReplacer *>(nonconst_this);
  62. return nonconst_base;
  63. }
  64. /**
  65. * UnicodeReplacer API
  66. */
  67. int32_t FunctionReplacer::replace(Replaceable& text,
  68. int32_t start,
  69. int32_t limit,
  70. int32_t& cursor)
  71. {
  72. // First delegate to subordinate replacer
  73. int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor);
  74. limit = start + len;
  75. // Now transliterate
  76. limit = translit->transliterate(text, start, limit);
  77. return limit - start;
  78. }
  79. /**
  80. * UnicodeReplacer API
  81. */
  82. UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule,
  83. UBool escapeUnprintable) const {
  84. UnicodeString str;
  85. rule.truncate(0);
  86. rule.append(AMPERSAND);
  87. rule.append(translit->getID());
  88. rule.append(OPEN, 2);
  89. rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable));
  90. rule.append(CLOSE, 2);
  91. return rule;
  92. }
  93. /**
  94. * Implement UnicodeReplacer
  95. */
  96. void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const {
  97. UnicodeSet set;
  98. toUnionTo.addAll(translit->getTargetSet(set));
  99. }
  100. /**
  101. * UnicodeFunctor API
  102. */
  103. void FunctionReplacer::setData(const TransliterationRuleData* d) {
  104. replacer->setData(d);
  105. }
  106. U_NAMESPACE_END
  107. #endif /* #if !UCONFIG_NO_TRANSLITERATION */
  108. //eof