unifilt.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * Date Name Description
  9. * 07/18/01 aliu Creation.
  10. **********************************************************************
  11. */
  12. #include "unicode/unifilt.h"
  13. #include "unicode/rep.h"
  14. #include "unicode/utf16.h"
  15. U_NAMESPACE_BEGIN
  16. UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(UnicodeFilter)
  17. /* Define this here due to the lack of another file.
  18. It can't be defined in the header */
  19. UnicodeMatcher::~UnicodeMatcher() {}
  20. UnicodeFilter::~UnicodeFilter() {}
  21. /**
  22. * UnicodeFunctor API.
  23. * Note that UnicodeMatcher is a base class of UnicodeFilter.
  24. */
  25. UnicodeMatcher* UnicodeFilter::toMatcher() const {
  26. return const_cast<UnicodeFilter *>(this);
  27. }
  28. void UnicodeFilter::setData(const TransliterationRuleData*) {}
  29. /**
  30. * Default implementation of UnicodeMatcher::matches() for Unicode
  31. * filters. Matches a single code point at offset (either one or
  32. * two 16-bit code units).
  33. */
  34. UMatchDegree UnicodeFilter::matches(const Replaceable& text,
  35. int32_t& offset,
  36. int32_t limit,
  37. UBool incremental) {
  38. UChar32 c;
  39. if (offset < limit &&
  40. contains(c = text.char32At(offset))) {
  41. offset += U16_LENGTH(c);
  42. return U_MATCH;
  43. }
  44. if (offset > limit &&
  45. contains(c = text.char32At(offset))) {
  46. // Backup offset by 1, unless the preceding character is a
  47. // surrogate pair -- then backup by 2 (keep offset pointing at
  48. // the lead surrogate).
  49. --offset;
  50. if (offset >= 0) {
  51. offset -= U16_LENGTH(text.char32At(offset)) - 1;
  52. }
  53. return U_MATCH;
  54. }
  55. if (incremental && offset == limit) {
  56. return U_PARTIAL_MATCH;
  57. }
  58. return U_MISMATCH;
  59. }
  60. U_NAMESPACE_END
  61. //eof