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
30U_NAMESPACE_USE
31
32U_CAPI USet* U_EXPORT2
33uset_openPattern(const char16_t* 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 nullptr */
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 = nullptr;
47 }
48 return (USet*) set;
49}
50
51U_CAPI USet* U_EXPORT2
52uset_openPatternOptions(const char16_t* 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, nullptr, *ec);
58 /* test for nullptr */
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 = nullptr;
67 }
68 return (USet*) set;
69}
70
71
72U_CAPI int32_t U_EXPORT2
73uset_applyPattern(USet *set,
74 const char16_t *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 == nullptr || U_FAILURE(*status)){
81 return 0;
82 }
83
84 // check only the set paramenter
85 // if pattern is nullptr or NUL terminated
86 // UnicodeString constructor takes care of it
87 if(set == nullptr){
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, nullptr, *status);
97
98 return pos.getIndex();
99}
100
101U_CAPI void U_EXPORT2
102uset_applyIntPropertyValue(USet* set,
103 UProperty prop, int32_t value, UErrorCode* ec) {
104 ((UnicodeSet*) set)->applyIntPropertyValue(prop, value, *ec);
105}
106
107U_CAPI void U_EXPORT2
108uset_applyPropertyAlias(USet* set,
109 const char16_t *prop, int32_t propLength,
110 const char16_t *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
119U_CAPI UBool U_EXPORT2
120uset_resemblesPattern(const char16_t *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) == (char16_t)91/*[*/) ||
127 UnicodeSet::resemblesPattern(pat, pos);
128}
129
130U_CAPI int32_t U_EXPORT2
131uset_toPattern(const USet* set,
132 char16_t* 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
140U_CAPI void U_EXPORT2
141uset_closeOver(USet* set, int32_t attributes) {
142 ((UnicodeSet*) set)->UnicodeSet::closeOver(attributes);
143}
144