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 
22 #include "unicode/utypes.h"
23 #include "unicode/uobject.h"
24 #include "unicode/uset.h"
25 #include "unicode/uniset.h"
26 #include "cmemory.h"
27 #include "unicode/ustring.h"
28 #include "unicode/parsepos.h"
29 
30 U_NAMESPACE_USE
31 
32 U_CAPI USet* U_EXPORT2
uset_openPattern(const UChar * pattern,int32_t patternLength,UErrorCode * ec)33 uset_openPattern(const UChar* pattern, int32_t patternLength,
34                  UErrorCode* ec)
35 {
36     UnicodeString pat(patternLength==-1, pattern, patternLength);
37     UnicodeSet* set = new UnicodeSet(pat, *ec);
38     /* test for NULL */
39     if(set == 0) {
40         *ec = U_MEMORY_ALLOCATION_ERROR;
41         return 0;
42     }
43 
44     if (U_FAILURE(*ec)) {
45         delete set;
46         set = NULL;
47     }
48     return (USet*) set;
49 }
50 
51 U_CAPI USet* U_EXPORT2
uset_openPatternOptions(const UChar * pattern,int32_t patternLength,uint32_t options,UErrorCode * ec)52 uset_openPatternOptions(const UChar* pattern, int32_t patternLength,
53                  uint32_t options,
54                  UErrorCode* ec)
55 {
56     UnicodeString pat(patternLength==-1, pattern, patternLength);
57     UnicodeSet* set = new UnicodeSet(pat, options, NULL, *ec);
58     /* test for NULL */
59     if(set == 0) {
60         *ec = U_MEMORY_ALLOCATION_ERROR;
61         return 0;
62     }
63 
64     if (U_FAILURE(*ec)) {
65         delete set;
66         set = NULL;
67     }
68     return (USet*) set;
69 }
70 
71 
72 U_CAPI int32_t U_EXPORT2
uset_applyPattern(USet * set,const UChar * pattern,int32_t patternLength,uint32_t options,UErrorCode * status)73 uset_applyPattern(USet *set,
74                   const UChar *pattern, int32_t patternLength,
75                   uint32_t options,
76                   UErrorCode *status){
77 
78     // status code needs to be checked since we
79     // dereference it
80     if(status == NULL || U_FAILURE(*status)){
81         return 0;
82     }
83 
84     // check only the set paramenter
85     // if pattern is NULL or null terminate
86     // UnicodeString constructor takes care of it
87     if(set == NULL){
88         *status = U_ILLEGAL_ARGUMENT_ERROR;
89         return 0;
90     }
91 
92     UnicodeString pat(pattern, patternLength);
93 
94     ParsePosition pos;
95 
96     ((UnicodeSet*) set)->applyPattern(pat, pos, options, NULL, *status);
97 
98     return pos.getIndex();
99 }
100 
101 U_CAPI void U_EXPORT2
uset_applyIntPropertyValue(USet * set,UProperty prop,int32_t value,UErrorCode * ec)102 uset_applyIntPropertyValue(USet* set,
103                UProperty prop, int32_t value, UErrorCode* ec) {
104     ((UnicodeSet*) set)->applyIntPropertyValue(prop, value, *ec);
105 }
106 
107 U_CAPI void U_EXPORT2
uset_applyPropertyAlias(USet * set,const UChar * prop,int32_t propLength,const UChar * value,int32_t valueLength,UErrorCode * ec)108 uset_applyPropertyAlias(USet* set,
109                         const UChar *prop, int32_t propLength,
110                         const UChar *value, int32_t valueLength,
111             UErrorCode* ec) {
112 
113     UnicodeString p(prop, propLength);
114     UnicodeString v(value, valueLength);
115 
116     ((UnicodeSet*) set)->applyPropertyAlias(p, v, *ec);
117 }
118 
119 U_CAPI UBool U_EXPORT2
uset_resemblesPattern(const UChar * pattern,int32_t patternLength,int32_t pos)120 uset_resemblesPattern(const UChar *pattern, int32_t patternLength,
121                       int32_t pos) {
122 
123     UnicodeString pat(pattern, patternLength);
124 
125     return ((pos+1) < pat.length() &&
126             pat.charAt(pos) == (UChar)91/*[*/) ||
127             UnicodeSet::resemblesPattern(pat, pos);
128 }
129 
130 U_CAPI int32_t U_EXPORT2
uset_toPattern(const USet * set,UChar * result,int32_t resultCapacity,UBool escapeUnprintable,UErrorCode * ec)131 uset_toPattern(const USet* set,
132                UChar* result, int32_t resultCapacity,
133                UBool escapeUnprintable,
134                UErrorCode* ec) {
135     UnicodeString pat;
136     ((const UnicodeSet*) set)->toPattern(pat, escapeUnprintable);
137     return pat.extract(result, resultCapacity, *ec);
138 }
139 
140 U_CAPI void U_EXPORT2
uset_closeOver(USet * set,int32_t attributes)141 uset_closeOver(USet* set, int32_t attributes) {
142     ((UnicodeSet*) set)->UnicodeSet::closeOver(attributes);
143 }
144