ucasemap_titlecase_brkiter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2011, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * file name: ucasemap_titlecase_brkiter.cpp
  9. * encoding: UTF-8
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * created on: 2011jun02
  14. * created by: Markus W. Scherer
  15. *
  16. * Titlecasing functions that are based on BreakIterator
  17. * were moved here to break dependency cycles among parts of the common library.
  18. */
  19. #include "unicode/utypes.h"
  20. #if !UCONFIG_NO_BREAK_ITERATION
  21. #include "unicode/brkiter.h"
  22. #include "unicode/ubrk.h"
  23. #include "unicode/casemap.h"
  24. #include "unicode/ucasemap.h"
  25. #include "cmemory.h"
  26. #include "ucase.h"
  27. #include "ucasemap_imp.h"
  28. U_NAMESPACE_BEGIN
  29. void CaseMap::utf8ToTitle(
  30. const char *locale, uint32_t options, BreakIterator *iter,
  31. StringPiece src, ByteSink &sink, Edits *edits,
  32. UErrorCode &errorCode) {
  33. if (U_FAILURE(errorCode)) {
  34. return;
  35. }
  36. UText utext = UTEXT_INITIALIZER;
  37. utext_openUTF8(&utext, src.data(), src.length(), &errorCode);
  38. LocalPointer<BreakIterator> ownedIter;
  39. iter = ustrcase_getTitleBreakIterator(nullptr, locale, options, iter, ownedIter, errorCode);
  40. if (iter == nullptr) {
  41. utext_close(&utext);
  42. return;
  43. }
  44. iter->setText(&utext, errorCode);
  45. ucasemap_mapUTF8(
  46. ustrcase_getCaseLocale(locale), options, iter,
  47. src.data(), src.length(),
  48. ucasemap_internalUTF8ToTitle, sink, edits, errorCode);
  49. utext_close(&utext);
  50. }
  51. int32_t CaseMap::utf8ToTitle(
  52. const char *locale, uint32_t options, BreakIterator *iter,
  53. const char *src, int32_t srcLength,
  54. char *dest, int32_t destCapacity, Edits *edits,
  55. UErrorCode &errorCode) {
  56. if (U_FAILURE(errorCode)) {
  57. return 0;
  58. }
  59. UText utext=UTEXT_INITIALIZER;
  60. utext_openUTF8(&utext, src, srcLength, &errorCode);
  61. LocalPointer<BreakIterator> ownedIter;
  62. iter = ustrcase_getTitleBreakIterator(nullptr, locale, options, iter, ownedIter, errorCode);
  63. if(iter==nullptr) {
  64. utext_close(&utext);
  65. return 0;
  66. }
  67. iter->setText(&utext, errorCode);
  68. int32_t length=ucasemap_mapUTF8(
  69. ustrcase_getCaseLocale(locale), options, iter,
  70. dest, destCapacity,
  71. src, srcLength,
  72. ucasemap_internalUTF8ToTitle, edits, errorCode);
  73. utext_close(&utext);
  74. return length;
  75. }
  76. U_NAMESPACE_END
  77. U_NAMESPACE_USE
  78. U_CAPI const UBreakIterator * U_EXPORT2
  79. ucasemap_getBreakIterator(const UCaseMap *csm) {
  80. return reinterpret_cast<UBreakIterator *>(csm->iter);
  81. }
  82. U_CAPI void U_EXPORT2
  83. ucasemap_setBreakIterator(UCaseMap *csm, UBreakIterator *iterToAdopt, UErrorCode *pErrorCode) {
  84. if(U_FAILURE(*pErrorCode)) {
  85. return;
  86. }
  87. delete csm->iter;
  88. csm->iter=reinterpret_cast<BreakIterator *>(iterToAdopt);
  89. }
  90. U_CAPI int32_t U_EXPORT2
  91. ucasemap_utf8ToTitle(UCaseMap *csm,
  92. char *dest, int32_t destCapacity,
  93. const char *src, int32_t srcLength,
  94. UErrorCode *pErrorCode) {
  95. if (U_FAILURE(*pErrorCode)) {
  96. return 0;
  97. }
  98. UText utext=UTEXT_INITIALIZER;
  99. utext_openUTF8(&utext, (const char *)src, srcLength, pErrorCode);
  100. if (U_FAILURE(*pErrorCode)) {
  101. return 0;
  102. }
  103. if(csm->iter==nullptr) {
  104. LocalPointer<BreakIterator> ownedIter;
  105. BreakIterator *iter = ustrcase_getTitleBreakIterator(
  106. nullptr, csm->locale, csm->options, nullptr, ownedIter, *pErrorCode);
  107. if (iter == nullptr) {
  108. utext_close(&utext);
  109. return 0;
  110. }
  111. csm->iter = ownedIter.orphan();
  112. }
  113. csm->iter->setText(&utext, *pErrorCode);
  114. int32_t length=ucasemap_mapUTF8(
  115. csm->caseLocale, csm->options, csm->iter,
  116. dest, destCapacity,
  117. src, srcLength,
  118. ucasemap_internalUTF8ToTitle, nullptr, *pErrorCode);
  119. utext_close(&utext);
  120. return length;
  121. }
  122. #endif // !UCONFIG_NO_BREAK_ITERATION