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