strmatch.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (c) 2001-2012, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. **********************************************************************
  8. * Date Name Description
  9. * 07/23/01 aliu Creation.
  10. **********************************************************************
  11. */
  12. #include "unicode/utypes.h"
  13. #if !UCONFIG_NO_TRANSLITERATION
  14. #include "strmatch.h"
  15. #include "rbt_data.h"
  16. #include "util.h"
  17. #include "unicode/uniset.h"
  18. #include "unicode/utf16.h"
  19. U_NAMESPACE_BEGIN
  20. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(StringMatcher)
  21. StringMatcher::StringMatcher(const UnicodeString& theString,
  22. int32_t start,
  23. int32_t limit,
  24. int32_t segmentNum,
  25. const TransliterationRuleData& theData) :
  26. data(&theData),
  27. segmentNumber(segmentNum),
  28. matchStart(-1),
  29. matchLimit(-1)
  30. {
  31. theString.extractBetween(start, limit, pattern);
  32. }
  33. StringMatcher::StringMatcher(const StringMatcher& o) :
  34. UnicodeFunctor(o),
  35. UnicodeMatcher(o),
  36. UnicodeReplacer(o),
  37. pattern(o.pattern),
  38. data(o.data),
  39. segmentNumber(o.segmentNumber),
  40. matchStart(o.matchStart),
  41. matchLimit(o.matchLimit)
  42. {
  43. }
  44. /**
  45. * Destructor
  46. */
  47. StringMatcher::~StringMatcher() {
  48. }
  49. /**
  50. * Implement UnicodeFunctor
  51. */
  52. StringMatcher* StringMatcher::clone() const {
  53. return new StringMatcher(*this);
  54. }
  55. /**
  56. * UnicodeFunctor API. Cast 'this' to a UnicodeMatcher* pointer
  57. * and return the pointer.
  58. */
  59. UnicodeMatcher* StringMatcher::toMatcher() const {
  60. StringMatcher *nonconst_this = const_cast<StringMatcher *>(this);
  61. UnicodeMatcher *nonconst_base = static_cast<UnicodeMatcher *>(nonconst_this);
  62. return nonconst_base;
  63. }
  64. /**
  65. * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
  66. * and return the pointer.
  67. */
  68. UnicodeReplacer* StringMatcher::toReplacer() const {
  69. StringMatcher *nonconst_this = const_cast<StringMatcher *>(this);
  70. UnicodeReplacer *nonconst_base = static_cast<UnicodeReplacer *>(nonconst_this);
  71. return nonconst_base;
  72. }
  73. /**
  74. * Implement UnicodeMatcher
  75. */
  76. UMatchDegree StringMatcher::matches(const Replaceable& text,
  77. int32_t& offset,
  78. int32_t limit,
  79. UBool incremental) {
  80. int32_t i;
  81. int32_t cursor = offset;
  82. if (limit < cursor) {
  83. // Match in the reverse direction
  84. for (i=pattern.length()-1; i>=0; --i) {
  85. char16_t keyChar = pattern.charAt(i);
  86. UnicodeMatcher* subm = data->lookupMatcher(keyChar);
  87. if (subm == 0) {
  88. if (cursor > limit &&
  89. keyChar == text.charAt(cursor)) {
  90. --cursor;
  91. } else {
  92. return U_MISMATCH;
  93. }
  94. } else {
  95. UMatchDegree m =
  96. subm->matches(text, cursor, limit, incremental);
  97. if (m != U_MATCH) {
  98. return m;
  99. }
  100. }
  101. }
  102. // Record the match position, but adjust for a normal
  103. // forward start, limit, and only if a prior match does not
  104. // exist -- we want the rightmost match.
  105. if (matchStart < 0) {
  106. matchStart = cursor+1;
  107. matchLimit = offset+1;
  108. }
  109. } else {
  110. for (i=0; i<pattern.length(); ++i) {
  111. if (incremental && cursor == limit) {
  112. // We've reached the context limit without a mismatch and
  113. // without completing our match.
  114. return U_PARTIAL_MATCH;
  115. }
  116. char16_t keyChar = pattern.charAt(i);
  117. UnicodeMatcher* subm = data->lookupMatcher(keyChar);
  118. if (subm == 0) {
  119. // Don't need the cursor < limit check if
  120. // incremental is true (because it's done above); do need
  121. // it otherwise.
  122. if (cursor < limit &&
  123. keyChar == text.charAt(cursor)) {
  124. ++cursor;
  125. } else {
  126. return U_MISMATCH;
  127. }
  128. } else {
  129. UMatchDegree m =
  130. subm->matches(text, cursor, limit, incremental);
  131. if (m != U_MATCH) {
  132. return m;
  133. }
  134. }
  135. }
  136. // Record the match position
  137. matchStart = offset;
  138. matchLimit = cursor;
  139. }
  140. offset = cursor;
  141. return U_MATCH;
  142. }
  143. /**
  144. * Implement UnicodeMatcher
  145. */
  146. UnicodeString& StringMatcher::toPattern(UnicodeString& result,
  147. UBool escapeUnprintable) const
  148. {
  149. result.truncate(0);
  150. UnicodeString str, quoteBuf;
  151. if (segmentNumber > 0) {
  152. result.append((char16_t)40); /*(*/
  153. }
  154. for (int32_t i=0; i<pattern.length(); ++i) {
  155. char16_t keyChar = pattern.charAt(i);
  156. const UnicodeMatcher* m = data->lookupMatcher(keyChar);
  157. if (m == 0) {
  158. ICU_Utility::appendToRule(result, keyChar, false, escapeUnprintable, quoteBuf);
  159. } else {
  160. ICU_Utility::appendToRule(result, m->toPattern(str, escapeUnprintable),
  161. true, escapeUnprintable, quoteBuf);
  162. }
  163. }
  164. if (segmentNumber > 0) {
  165. result.append((char16_t)41); /*)*/
  166. }
  167. // Flush quoteBuf out to result
  168. ICU_Utility::appendToRule(result, -1,
  169. true, escapeUnprintable, quoteBuf);
  170. return result;
  171. }
  172. /**
  173. * Implement UnicodeMatcher
  174. */
  175. UBool StringMatcher::matchesIndexValue(uint8_t v) const {
  176. if (pattern.length() == 0) {
  177. return true;
  178. }
  179. UChar32 c = pattern.char32At(0);
  180. const UnicodeMatcher *m = data->lookupMatcher(c);
  181. return (m == 0) ? ((c & 0xFF) == v) : m->matchesIndexValue(v);
  182. }
  183. /**
  184. * Implement UnicodeMatcher
  185. */
  186. void StringMatcher::addMatchSetTo(UnicodeSet& toUnionTo) const {
  187. UChar32 ch;
  188. for (int32_t i=0; i<pattern.length(); i+=U16_LENGTH(ch)) {
  189. ch = pattern.char32At(i);
  190. const UnicodeMatcher* matcher = data->lookupMatcher(ch);
  191. if (matcher == nullptr) {
  192. toUnionTo.add(ch);
  193. } else {
  194. matcher->addMatchSetTo(toUnionTo);
  195. }
  196. }
  197. }
  198. /**
  199. * UnicodeReplacer API
  200. */
  201. int32_t StringMatcher::replace(Replaceable& text,
  202. int32_t start,
  203. int32_t limit,
  204. int32_t& /*cursor*/) {
  205. int32_t outLen = 0;
  206. // Copy segment with out-of-band data
  207. int32_t dest = limit;
  208. // If there was no match, that means that a quantifier
  209. // matched zero-length. E.g., x (a)* y matched "xy".
  210. if (matchStart >= 0) {
  211. if (matchStart != matchLimit) {
  212. text.copy(matchStart, matchLimit, dest);
  213. outLen = matchLimit - matchStart;
  214. }
  215. }
  216. text.handleReplaceBetween(start, limit, UnicodeString()); // delete original text
  217. return outLen;
  218. }
  219. /**
  220. * UnicodeReplacer API
  221. */
  222. UnicodeString& StringMatcher::toReplacerPattern(UnicodeString& rule,
  223. UBool /*escapeUnprintable*/) const {
  224. // assert(segmentNumber > 0);
  225. rule.truncate(0);
  226. rule.append((char16_t)0x0024 /*$*/);
  227. ICU_Utility::appendNumber(rule, segmentNumber, 10, 1);
  228. return rule;
  229. }
  230. /**
  231. * Remove any match info. This must be called before performing a
  232. * set of matches with this segment.
  233. */
  234. void StringMatcher::resetMatch() {
  235. matchStart = matchLimit = -1;
  236. }
  237. /**
  238. * Union the set of all characters that may output by this object
  239. * into the given set.
  240. * @param toUnionTo the set into which to union the output characters
  241. */
  242. void StringMatcher::addReplacementSetTo(UnicodeSet& /*toUnionTo*/) const {
  243. // The output of this replacer varies; it is the source text between
  244. // matchStart and matchLimit. Since this varies depending on the
  245. // input text, we can't compute it here. We can either do nothing
  246. // or we can add ALL characters to the set. It's probably more useful
  247. // to do nothing.
  248. }
  249. /**
  250. * Implement UnicodeFunctor
  251. */
  252. void StringMatcher::setData(const TransliterationRuleData* d) {
  253. data = d;
  254. int32_t i = 0;
  255. while (i<pattern.length()) {
  256. UChar32 c = pattern.char32At(i);
  257. UnicodeFunctor* f = data->lookup(c);
  258. if (f != nullptr) {
  259. f->setData(data);
  260. }
  261. i += U16_LENGTH(c);
  262. }
  263. }
  264. U_NAMESPACE_END
  265. #endif /* #if !UCONFIG_NO_TRANSLITERATION */
  266. //eof