unorm.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. * Copyright (c) 1996-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. ******************************************************************************
  8. * File unorm.cpp
  9. *
  10. * Created by: Vladimir Weinstein 12052000
  11. *
  12. * Modification history :
  13. *
  14. * Date Name Description
  15. * 02/01/01 synwee Added normalization quickcheck enum and method.
  16. * 02/12/01 synwee Commented out quickcheck util api has been approved
  17. * Added private method for doing FCD checks
  18. * 02/23/01 synwee Modified quickcheck and checkFCE to run through
  19. * string for codepoints < 0x300 for the normalization
  20. * mode NFC.
  21. * 05/25/01+ Markus Scherer total rewrite, implement all normalization here
  22. * instead of just wrappers around normlzr.cpp,
  23. * load unorm.dat, support Unicode 3.1 with
  24. * supplementary code points, etc.
  25. * 2009-nov..2010-jan Markus Scherer total rewrite, new Normalizer2 API & code
  26. */
  27. #include "unicode/utypes.h"
  28. #if !UCONFIG_NO_NORMALIZATION
  29. #include "unicode/udata.h"
  30. #include "unicode/ustring.h"
  31. #include "unicode/uiter.h"
  32. #include "unicode/unorm.h"
  33. #include "unicode/unorm2.h"
  34. #include "normalizer2impl.h"
  35. #include "unormimp.h"
  36. #include "uprops.h"
  37. #include "ustr_imp.h"
  38. U_NAMESPACE_USE
  39. /* quick check functions ---------------------------------------------------- */
  40. U_CAPI UNormalizationCheckResult U_EXPORT2
  41. unorm_quickCheck(const char16_t *src,
  42. int32_t srcLength,
  43. UNormalizationMode mode,
  44. UErrorCode *pErrorCode) {
  45. const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
  46. return unorm2_quickCheck((const UNormalizer2 *)n2, src, srcLength, pErrorCode);
  47. }
  48. U_CAPI UNormalizationCheckResult U_EXPORT2
  49. unorm_quickCheckWithOptions(const char16_t *src, int32_t srcLength,
  50. UNormalizationMode mode, int32_t options,
  51. UErrorCode *pErrorCode) {
  52. const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
  53. if(options&UNORM_UNICODE_3_2) {
  54. FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*pErrorCode));
  55. return unorm2_quickCheck(
  56. reinterpret_cast<const UNormalizer2 *>(static_cast<Normalizer2 *>(&fn2)),
  57. src, srcLength, pErrorCode);
  58. } else {
  59. return unorm2_quickCheck((const UNormalizer2 *)n2, src, srcLength, pErrorCode);
  60. }
  61. }
  62. U_CAPI UBool U_EXPORT2
  63. unorm_isNormalized(const char16_t *src, int32_t srcLength,
  64. UNormalizationMode mode,
  65. UErrorCode *pErrorCode) {
  66. const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
  67. return unorm2_isNormalized((const UNormalizer2 *)n2, src, srcLength, pErrorCode);
  68. }
  69. U_CAPI UBool U_EXPORT2
  70. unorm_isNormalizedWithOptions(const char16_t *src, int32_t srcLength,
  71. UNormalizationMode mode, int32_t options,
  72. UErrorCode *pErrorCode) {
  73. const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
  74. if(options&UNORM_UNICODE_3_2) {
  75. FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*pErrorCode));
  76. return unorm2_isNormalized(
  77. reinterpret_cast<const UNormalizer2 *>(static_cast<Normalizer2 *>(&fn2)),
  78. src, srcLength, pErrorCode);
  79. } else {
  80. return unorm2_isNormalized((const UNormalizer2 *)n2, src, srcLength, pErrorCode);
  81. }
  82. }
  83. /* normalize() API ---------------------------------------------------------- */
  84. /** Public API for normalizing. */
  85. U_CAPI int32_t U_EXPORT2
  86. unorm_normalize(const char16_t *src, int32_t srcLength,
  87. UNormalizationMode mode, int32_t options,
  88. char16_t *dest, int32_t destCapacity,
  89. UErrorCode *pErrorCode) {
  90. const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
  91. if(options&UNORM_UNICODE_3_2) {
  92. FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*pErrorCode));
  93. return unorm2_normalize(
  94. reinterpret_cast<const UNormalizer2 *>(static_cast<Normalizer2 *>(&fn2)),
  95. src, srcLength, dest, destCapacity, pErrorCode);
  96. } else {
  97. return unorm2_normalize((const UNormalizer2 *)n2,
  98. src, srcLength, dest, destCapacity, pErrorCode);
  99. }
  100. }
  101. /* iteration functions ------------------------------------------------------ */
  102. static int32_t
  103. _iterate(UCharIterator *src, UBool forward,
  104. char16_t *dest, int32_t destCapacity,
  105. const Normalizer2 *n2,
  106. UBool doNormalize, UBool *pNeededToNormalize,
  107. UErrorCode *pErrorCode) {
  108. if(U_FAILURE(*pErrorCode)) {
  109. return 0;
  110. }
  111. if(destCapacity<0 || (dest==nullptr && destCapacity>0) || src==nullptr) {
  112. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  113. return 0;
  114. }
  115. if(pNeededToNormalize!=nullptr) {
  116. *pNeededToNormalize=false;
  117. }
  118. if(!(forward ? src->hasNext(src) : src->hasPrevious(src))) {
  119. return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
  120. }
  121. UnicodeString buffer;
  122. UChar32 c;
  123. if(forward) {
  124. /* get one character and ignore its properties */
  125. buffer.append(uiter_next32(src));
  126. /* get all following characters until we see a boundary */
  127. while((c=uiter_next32(src))>=0) {
  128. if(n2->hasBoundaryBefore(c)) {
  129. /* back out the latest movement to stop at the boundary */
  130. src->move(src, -U16_LENGTH(c), UITER_CURRENT);
  131. break;
  132. } else {
  133. buffer.append(c);
  134. }
  135. }
  136. } else {
  137. while((c=uiter_previous32(src))>=0) {
  138. /* always write this character to the front of the buffer */
  139. buffer.insert(0, c);
  140. /* stop if this just-copied character is a boundary */
  141. if(n2->hasBoundaryBefore(c)) {
  142. break;
  143. }
  144. }
  145. }
  146. UnicodeString destString(dest, 0, destCapacity);
  147. if(buffer.length()>0 && doNormalize) {
  148. n2->normalize(buffer, destString, *pErrorCode).extract(dest, destCapacity, *pErrorCode);
  149. if(pNeededToNormalize!=nullptr && U_SUCCESS(*pErrorCode)) {
  150. *pNeededToNormalize= destString!=buffer;
  151. }
  152. return destString.length();
  153. } else {
  154. /* just copy the source characters */
  155. return buffer.extract(dest, destCapacity, *pErrorCode);
  156. }
  157. }
  158. static int32_t
  159. unorm_iterate(UCharIterator *src, UBool forward,
  160. char16_t *dest, int32_t destCapacity,
  161. UNormalizationMode mode, int32_t options,
  162. UBool doNormalize, UBool *pNeededToNormalize,
  163. UErrorCode *pErrorCode) {
  164. const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
  165. if(options&UNORM_UNICODE_3_2) {
  166. const UnicodeSet *uni32 = uniset_getUnicode32Instance(*pErrorCode);
  167. if(U_FAILURE(*pErrorCode)) {
  168. return 0;
  169. }
  170. FilteredNormalizer2 fn2(*n2, *uni32);
  171. return _iterate(src, forward, dest, destCapacity,
  172. &fn2, doNormalize, pNeededToNormalize, pErrorCode);
  173. }
  174. return _iterate(src, forward, dest, destCapacity,
  175. n2, doNormalize, pNeededToNormalize, pErrorCode);
  176. }
  177. U_CAPI int32_t U_EXPORT2
  178. unorm_previous(UCharIterator *src,
  179. char16_t *dest, int32_t destCapacity,
  180. UNormalizationMode mode, int32_t options,
  181. UBool doNormalize, UBool *pNeededToNormalize,
  182. UErrorCode *pErrorCode) {
  183. return unorm_iterate(src, false,
  184. dest, destCapacity,
  185. mode, options,
  186. doNormalize, pNeededToNormalize,
  187. pErrorCode);
  188. }
  189. U_CAPI int32_t U_EXPORT2
  190. unorm_next(UCharIterator *src,
  191. char16_t *dest, int32_t destCapacity,
  192. UNormalizationMode mode, int32_t options,
  193. UBool doNormalize, UBool *pNeededToNormalize,
  194. UErrorCode *pErrorCode) {
  195. return unorm_iterate(src, true,
  196. dest, destCapacity,
  197. mode, options,
  198. doNormalize, pNeededToNormalize,
  199. pErrorCode);
  200. }
  201. /* Concatenation of normalized strings -------------------------------------- */
  202. static int32_t
  203. _concatenate(const char16_t *left, int32_t leftLength,
  204. const char16_t *right, int32_t rightLength,
  205. char16_t *dest, int32_t destCapacity,
  206. const Normalizer2 *n2,
  207. UErrorCode *pErrorCode) {
  208. if(U_FAILURE(*pErrorCode)) {
  209. return 0;
  210. }
  211. if(destCapacity<0 || (dest==nullptr && destCapacity>0) ||
  212. left==nullptr || leftLength<-1 || right==nullptr || rightLength<-1) {
  213. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  214. return 0;
  215. }
  216. /* check for overlapping right and destination */
  217. if( dest!=nullptr &&
  218. ((right>=dest && right<(dest+destCapacity)) ||
  219. (rightLength>0 && dest>=right && dest<(right+rightLength)))
  220. ) {
  221. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  222. return 0;
  223. }
  224. /* allow left==dest */
  225. UnicodeString destString;
  226. if(left==dest) {
  227. destString.setTo(dest, leftLength, destCapacity);
  228. } else {
  229. destString.setTo(dest, 0, destCapacity);
  230. destString.append(left, leftLength);
  231. }
  232. return n2->append(destString, UnicodeString(rightLength<0, right, rightLength), *pErrorCode).
  233. extract(dest, destCapacity, *pErrorCode);
  234. }
  235. U_CAPI int32_t U_EXPORT2
  236. unorm_concatenate(const char16_t *left, int32_t leftLength,
  237. const char16_t *right, int32_t rightLength,
  238. char16_t *dest, int32_t destCapacity,
  239. UNormalizationMode mode, int32_t options,
  240. UErrorCode *pErrorCode) {
  241. const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
  242. if(options&UNORM_UNICODE_3_2) {
  243. const UnicodeSet *uni32 = uniset_getUnicode32Instance(*pErrorCode);
  244. if(U_FAILURE(*pErrorCode)) {
  245. return 0;
  246. }
  247. FilteredNormalizer2 fn2(*n2, *uni32);
  248. return _concatenate(left, leftLength, right, rightLength,
  249. dest, destCapacity, &fn2, pErrorCode);
  250. }
  251. return _concatenate(left, leftLength, right, rightLength,
  252. dest, destCapacity, n2, pErrorCode);
  253. }
  254. #endif /* #if !UCONFIG_NO_NORMALIZATION */