uset_props.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2002-2011, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: uset_props.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2004aug30
  16. * created by: Markus W. Scherer
  17. *
  18. * C wrappers around UnicodeSet functions that are implemented in
  19. * uniset_props.cpp, split off for modularization.
  20. */
  21. #include "unicode/utypes.h"
  22. #include "unicode/uobject.h"
  23. #include "unicode/uset.h"
  24. #include "unicode/uniset.h"
  25. #include "cmemory.h"
  26. #include "unicode/ustring.h"
  27. #include "unicode/parsepos.h"
  28. U_NAMESPACE_USE
  29. U_CAPI USet* U_EXPORT2
  30. uset_openPattern(const char16_t* pattern, int32_t patternLength,
  31. UErrorCode* ec)
  32. {
  33. UnicodeString pat(patternLength==-1, pattern, patternLength);
  34. UnicodeSet* set = new UnicodeSet(pat, *ec);
  35. /* test for nullptr */
  36. if(set == 0) {
  37. *ec = U_MEMORY_ALLOCATION_ERROR;
  38. return 0;
  39. }
  40. if (U_FAILURE(*ec)) {
  41. delete set;
  42. set = nullptr;
  43. }
  44. return (USet*) set;
  45. }
  46. U_CAPI USet* U_EXPORT2
  47. uset_openPatternOptions(const char16_t* pattern, int32_t patternLength,
  48. uint32_t options,
  49. UErrorCode* ec)
  50. {
  51. UnicodeString pat(patternLength==-1, pattern, patternLength);
  52. UnicodeSet* set = new UnicodeSet(pat, options, nullptr, *ec);
  53. /* test for nullptr */
  54. if(set == 0) {
  55. *ec = U_MEMORY_ALLOCATION_ERROR;
  56. return 0;
  57. }
  58. if (U_FAILURE(*ec)) {
  59. delete set;
  60. set = nullptr;
  61. }
  62. return (USet*) set;
  63. }
  64. U_CAPI int32_t U_EXPORT2
  65. uset_applyPattern(USet *set,
  66. const char16_t *pattern, int32_t patternLength,
  67. uint32_t options,
  68. UErrorCode *status){
  69. // status code needs to be checked since we
  70. // dereference it
  71. if(status == nullptr || U_FAILURE(*status)){
  72. return 0;
  73. }
  74. // check only the set paramenter
  75. // if pattern is nullptr or NUL terminated
  76. // UnicodeString constructor takes care of it
  77. if(set == nullptr){
  78. *status = U_ILLEGAL_ARGUMENT_ERROR;
  79. return 0;
  80. }
  81. UnicodeString pat(pattern, patternLength);
  82. ParsePosition pos;
  83. ((UnicodeSet*) set)->applyPattern(pat, pos, options, nullptr, *status);
  84. return pos.getIndex();
  85. }
  86. U_CAPI void U_EXPORT2
  87. uset_applyIntPropertyValue(USet* set,
  88. UProperty prop, int32_t value, UErrorCode* ec) {
  89. ((UnicodeSet*) set)->applyIntPropertyValue(prop, value, *ec);
  90. }
  91. U_CAPI void U_EXPORT2
  92. uset_applyPropertyAlias(USet* set,
  93. const char16_t *prop, int32_t propLength,
  94. const char16_t *value, int32_t valueLength,
  95. UErrorCode* ec) {
  96. UnicodeString p(prop, propLength);
  97. UnicodeString v(value, valueLength);
  98. ((UnicodeSet*) set)->applyPropertyAlias(p, v, *ec);
  99. }
  100. U_CAPI UBool U_EXPORT2
  101. uset_resemblesPattern(const char16_t *pattern, int32_t patternLength,
  102. int32_t pos) {
  103. UnicodeString pat(pattern, patternLength);
  104. return ((pos+1) < pat.length() &&
  105. pat.charAt(pos) == (char16_t)91/*[*/) ||
  106. UnicodeSet::resemblesPattern(pat, pos);
  107. }
  108. U_CAPI int32_t U_EXPORT2
  109. uset_toPattern(const USet* set,
  110. char16_t* result, int32_t resultCapacity,
  111. UBool escapeUnprintable,
  112. UErrorCode* ec) {
  113. UnicodeString pat;
  114. ((const UnicodeSet*) set)->toPattern(pat, escapeUnprintable);
  115. return pat.extract(result, resultCapacity, *ec);
  116. }
  117. U_CAPI void U_EXPORT2
  118. uset_closeOver(USet* set, int32_t attributes) {
  119. ((UnicodeSet*) set)->UnicodeSet::closeOver(attributes);
  120. }