titletrn.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-2011, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * Date Name Description
  9. * 05/24/01 aliu Creation.
  10. **********************************************************************
  11. */
  12. #include "unicode/utypes.h"
  13. #if !UCONFIG_NO_TRANSLITERATION
  14. #include "unicode/uchar.h"
  15. #include "unicode/uniset.h"
  16. #include "unicode/ustring.h"
  17. #include "unicode/utf16.h"
  18. #include "titletrn.h"
  19. #include "umutex.h"
  20. #include "ucase.h"
  21. #include "cpputils.h"
  22. U_NAMESPACE_BEGIN
  23. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TitlecaseTransliterator)
  24. TitlecaseTransliterator::TitlecaseTransliterator() :
  25. CaseMapTransliterator(UNICODE_STRING("Any-Title", 9), nullptr)
  26. {
  27. // Need to look back 2 characters in the case of "can't"
  28. setMaximumContextLength(2);
  29. }
  30. /**
  31. * Destructor.
  32. */
  33. TitlecaseTransliterator::~TitlecaseTransliterator() {
  34. }
  35. /**
  36. * Copy constructor.
  37. */
  38. TitlecaseTransliterator::TitlecaseTransliterator(const TitlecaseTransliterator& o) :
  39. CaseMapTransliterator(o)
  40. {
  41. }
  42. /**
  43. * Assignment operator.
  44. */
  45. /*TitlecaseTransliterator& TitlecaseTransliterator::operator=(
  46. const TitlecaseTransliterator& o) {
  47. CaseMapTransliterator::operator=(o);
  48. return *this;
  49. }*/
  50. /**
  51. * Transliterator API.
  52. */
  53. TitlecaseTransliterator* TitlecaseTransliterator::clone() const {
  54. return new TitlecaseTransliterator(*this);
  55. }
  56. /**
  57. * Implements {@link Transliterator#handleTransliterate}.
  58. */
  59. void TitlecaseTransliterator::handleTransliterate(
  60. Replaceable& text, UTransPosition& offsets,
  61. UBool isIncremental) const
  62. {
  63. // TODO reimplement, see ustrcase.c
  64. // using a real word break iterator
  65. // instead of just looking for a transition between cased and uncased characters
  66. // call CaseMapTransliterator::handleTransliterate() for lowercasing? (set fMap)
  67. // needs to take isIncremental into account because case mappings are context-sensitive
  68. // also detect when lowercasing function did not finish because of context
  69. if (offsets.start >= offsets.limit) {
  70. return;
  71. }
  72. // case type: >0 cased (UCASE_LOWER etc.) ==0 uncased <0 case-ignorable
  73. int32_t type;
  74. // Our mode; we are either converting letter toTitle or
  75. // toLower.
  76. UBool doTitle = true;
  77. // Determine if there is a preceding context of cased case-ignorable*,
  78. // in which case we want to start in toLower mode. If the
  79. // prior context is anything else (including empty) then start
  80. // in toTitle mode.
  81. UChar32 c;
  82. int32_t start;
  83. for (start = offsets.start - 1; start >= offsets.contextStart; start -= U16_LENGTH(c)) {
  84. c = text.char32At(start);
  85. type=ucase_getTypeOrIgnorable(c);
  86. if(type>0) { // cased
  87. doTitle=false;
  88. break;
  89. } else if(type==0) { // uncased but not ignorable
  90. break;
  91. }
  92. // else (type<0) case-ignorable: continue
  93. }
  94. // Convert things after a cased character toLower; things
  95. // after an uncased, non-case-ignorable character toTitle. Case-ignorable
  96. // characters are copied directly and do not change the mode.
  97. UCaseContext csc;
  98. uprv_memset(&csc, 0, sizeof(csc));
  99. csc.p = &text;
  100. csc.start = offsets.contextStart;
  101. csc.limit = offsets.contextLimit;
  102. UnicodeString tmp;
  103. const char16_t *s;
  104. int32_t textPos, delta, result;
  105. for(textPos=offsets.start; textPos<offsets.limit;) {
  106. csc.cpStart=textPos;
  107. c=text.char32At(textPos);
  108. csc.cpLimit=textPos+=U16_LENGTH(c);
  109. type=ucase_getTypeOrIgnorable(c);
  110. if(type>=0) { // not case-ignorable
  111. if(doTitle) {
  112. result=ucase_toFullTitle(c, utrans_rep_caseContextIterator, &csc, &s, UCASE_LOC_ROOT);
  113. } else {
  114. result=ucase_toFullLower(c, utrans_rep_caseContextIterator, &csc, &s, UCASE_LOC_ROOT);
  115. }
  116. doTitle = (UBool)(type==0); // doTitle=isUncased
  117. if(csc.b1 && isIncremental) {
  118. // fMap() tried to look beyond the context limit
  119. // wait for more input
  120. offsets.start=csc.cpStart;
  121. return;
  122. }
  123. if(result>=0) {
  124. // replace the current code point with its full case mapping result
  125. // see UCASE_MAX_STRING_LENGTH
  126. if(result<=UCASE_MAX_STRING_LENGTH) {
  127. // string s[result]
  128. tmp.setTo(false, s, result);
  129. delta=result-U16_LENGTH(c);
  130. } else {
  131. // single code point
  132. tmp.setTo(result);
  133. delta=tmp.length()-U16_LENGTH(c);
  134. }
  135. text.handleReplaceBetween(csc.cpStart, textPos, tmp);
  136. if(delta!=0) {
  137. textPos+=delta;
  138. csc.limit=offsets.contextLimit+=delta;
  139. offsets.limit+=delta;
  140. }
  141. }
  142. }
  143. }
  144. offsets.start=textPos;
  145. }
  146. U_NAMESPACE_END
  147. #endif /* #if !UCONFIG_NO_TRANSLITERATION */