name2uni.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. * 06/07/01 aliu Creation.
  10. **********************************************************************
  11. */
  12. #include "unicode/utypes.h"
  13. #if !UCONFIG_NO_TRANSLITERATION
  14. #include "unicode/unifilt.h"
  15. #include "unicode/uchar.h"
  16. #include "unicode/uniset.h"
  17. #include "unicode/utf16.h"
  18. #include "cmemory.h"
  19. #include "name2uni.h"
  20. #include "patternprops.h"
  21. #include "uprops.h"
  22. #include "uinvchar.h"
  23. #include "util.h"
  24. U_NAMESPACE_BEGIN
  25. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NameUnicodeTransliterator)
  26. static const char16_t OPEN[] = {92,78,126,123,126,0}; // "\N~{~"
  27. static const char16_t OPEN_DELIM = 92; // '\\' first char of OPEN
  28. static const char16_t CLOSE_DELIM = 125; // '}'
  29. static const char16_t SPACE = 32; // ' '
  30. U_CDECL_BEGIN
  31. // USetAdder implementation
  32. // Does not use uset.h to reduce code dependencies
  33. static void U_CALLCONV
  34. _set_add(USet *set, UChar32 c) {
  35. uset_add(set, c);
  36. }
  37. // These functions aren't used.
  38. /*static void U_CALLCONV
  39. _set_addRange(USet *set, UChar32 start, UChar32 end) {
  40. ((UnicodeSet *)set)->add(start, end);
  41. }
  42. static void U_CALLCONV
  43. _set_addString(USet *set, const char16_t *str, int32_t length) {
  44. ((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));
  45. }*/
  46. U_CDECL_END
  47. /**
  48. * Constructs a transliterator with the default delimiters '{' and
  49. * '}'.
  50. */
  51. NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter* adoptedFilter) :
  52. Transliterator(UNICODE_STRING("Name-Any", 8), adoptedFilter) {
  53. UnicodeSet *legalPtr = &legal;
  54. // Get the legal character set
  55. USetAdder sa = {
  56. (USet *)legalPtr, // USet* == UnicodeSet*
  57. _set_add,
  58. nullptr, // Don't need _set_addRange
  59. nullptr, // Don't need _set_addString
  60. nullptr, // Don't need remove()
  61. nullptr
  62. };
  63. uprv_getCharNameCharacters(&sa);
  64. }
  65. /**
  66. * Destructor.
  67. */
  68. NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
  69. /**
  70. * Copy constructor.
  71. */
  72. NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator& o) :
  73. Transliterator(o), legal(o.legal) {}
  74. /**
  75. * Assignment operator.
  76. */
  77. /*NameUnicodeTransliterator& NameUnicodeTransliterator::operator=(
  78. const NameUnicodeTransliterator& o) {
  79. Transliterator::operator=(o);
  80. // not necessary: the legal sets should all be the same -- legal=o.legal;
  81. return *this;
  82. }*/
  83. /**
  84. * Transliterator API.
  85. */
  86. NameUnicodeTransliterator* NameUnicodeTransliterator::clone() const {
  87. return new NameUnicodeTransliterator(*this);
  88. }
  89. /**
  90. * Implements {@link Transliterator#handleTransliterate}.
  91. */
  92. void NameUnicodeTransliterator::handleTransliterate(Replaceable& text, UTransPosition& offsets,
  93. UBool isIncremental) const {
  94. // The failure mode, here and below, is to behave like Any-Null,
  95. // if either there is no name data (max len == 0) or there is no
  96. // memory (malloc() => nullptr).
  97. int32_t maxLen = uprv_getMaxCharNameLength();
  98. if (maxLen == 0) {
  99. offsets.start = offsets.limit;
  100. return;
  101. }
  102. // Accommodate the longest possible name
  103. ++maxLen; // allow for temporary trailing space
  104. char* cbuf = (char*) uprv_malloc(maxLen);
  105. if (cbuf == nullptr) {
  106. offsets.start = offsets.limit;
  107. return;
  108. }
  109. UnicodeString openPat(true, OPEN, -1);
  110. UnicodeString str, name;
  111. int32_t cursor = offsets.start;
  112. int32_t limit = offsets.limit;
  113. // Modes:
  114. // 0 - looking for open delimiter
  115. // 1 - after open delimiter
  116. int32_t mode = 0;
  117. int32_t openPos = -1; // open delim candidate pos
  118. UChar32 c;
  119. while (cursor < limit) {
  120. c = text.char32At(cursor);
  121. switch (mode) {
  122. case 0: // looking for open delimiter
  123. if (c == OPEN_DELIM) { // quick check first
  124. openPos = cursor;
  125. int32_t i =
  126. ICU_Utility::parsePattern(openPat, text, cursor, limit);
  127. if (i >= 0 && i < limit) {
  128. mode = 1;
  129. name.truncate(0);
  130. cursor = i;
  131. continue; // *** reprocess char32At(cursor)
  132. }
  133. }
  134. break;
  135. case 1: // after open delimiter
  136. // Look for legal chars. If \s+ is found, convert it
  137. // to a single space. If closeDelimiter is found, exit
  138. // the loop. If any other character is found, exit the
  139. // loop. If the limit is reached, exit the loop.
  140. // Convert \s+ => SPACE. This assumes there are no
  141. // runs of >1 space characters in names.
  142. if (PatternProps::isWhiteSpace(c)) {
  143. // Ignore leading whitespace
  144. if (name.length() > 0 &&
  145. name.charAt(name.length()-1) != SPACE) {
  146. name.append(SPACE);
  147. // If we are too long then abort. maxLen includes
  148. // temporary trailing space, so use '>'.
  149. if (name.length() > maxLen) {
  150. mode = 0;
  151. }
  152. }
  153. break;
  154. }
  155. if (c == CLOSE_DELIM) {
  156. int32_t len = name.length();
  157. // Delete trailing space, if any
  158. if (len > 0 &&
  159. name.charAt(len-1) == SPACE) {
  160. --len;
  161. }
  162. if (uprv_isInvariantUString(name.getBuffer(), len)) {
  163. cbuf[0] = 0;
  164. name.extract(0, len, cbuf, maxLen, US_INV);
  165. UErrorCode status = U_ZERO_ERROR;
  166. c = u_charFromName(U_EXTENDED_CHAR_NAME, cbuf, &status);
  167. if (U_SUCCESS(status)) {
  168. // Lookup succeeded
  169. // assert(U16_LENGTH(CLOSE_DELIM) == 1);
  170. cursor++; // advance over CLOSE_DELIM
  171. str.truncate(0);
  172. str.append(c);
  173. text.handleReplaceBetween(openPos, cursor, str);
  174. // Adjust indices for the change in the length of
  175. // the string. Do not assume that str.length() ==
  176. // 1, in case of surrogates.
  177. int32_t delta = cursor - openPos - str.length();
  178. cursor -= delta;
  179. limit -= delta;
  180. // assert(cursor == openPos + str.length());
  181. }
  182. }
  183. // If the lookup failed, we leave things as-is and
  184. // still switch to mode 0 and continue.
  185. mode = 0;
  186. openPos = -1; // close off candidate
  187. continue; // *** reprocess char32At(cursor)
  188. }
  189. // Check if c is a legal char. We assume here that
  190. // legal.contains(OPEN_DELIM) is false, so when we abort a
  191. // name, we don't have to go back to openPos+1.
  192. if (legal.contains(c)) {
  193. name.append(c);
  194. // If we go past the longest possible name then abort.
  195. // maxLen includes temporary trailing space, so use '>='.
  196. if (name.length() >= maxLen) {
  197. mode = 0;
  198. }
  199. }
  200. // Invalid character
  201. else {
  202. --cursor; // Backup and reprocess this character
  203. mode = 0;
  204. }
  205. break;
  206. }
  207. cursor += U16_LENGTH(c);
  208. }
  209. offsets.contextLimit += limit - offsets.limit;
  210. offsets.limit = limit;
  211. // In incremental mode, only advance the cursor up to the last
  212. // open delimiter candidate.
  213. offsets.start = (isIncremental && openPos >= 0) ? openPos : cursor;
  214. uprv_free(cbuf);
  215. }
  216. U_NAMESPACE_END
  217. #endif /* #if !UCONFIG_NO_TRANSLITERATION */