1 /*
2 * Copyright (C) 2008-2009, International Business Machines Corporation and Others.
3 * All rights reserved.
4 */
5
6 #include "unicode/utypes.h"
7 #include "cmemory.h"
8 #include "unicode/bms.h"
9 #include "unicode/unistr.h"
10 #include "unicode/colldata.h"
11 #include "unicode/bmsearch.h"
12
13
14 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
15
16
17 //#define USE_SAFE_CASTS
18 #ifdef USE_SAFE_CASTS
19 #define STATIC_CAST(type,value) static_cast<type>(value)
20 #define CONST_CAST(type,value) const_cast<type>(value)
21 #else
22 #define STATIC_CAST(type,value) (type) (value)
23 #define CONST_CAST(type,value) (type) (value)
24 #endif
25
26 U_NAMESPACE_USE
27
28 U_CAPI UCD * U_EXPORT2
ucd_open(UCollator * coll,UErrorCode * status)29 ucd_open(UCollator *coll, UErrorCode *status)
30 {
31 return STATIC_CAST(UCD *, CollData::open(coll, *status));
32 }
33
34 U_CAPI void U_EXPORT2
ucd_close(UCD * ucd)35 ucd_close(UCD *ucd)
36 {
37 CollData *data = STATIC_CAST(CollData *, ucd);
38
39 CollData::close(data);
40 }
41
42 U_CAPI UCollator * U_EXPORT2
ucd_getCollator(UCD * ucd)43 ucd_getCollator(UCD *ucd)
44 {
45 CollData *data = STATIC_CAST(CollData *, ucd);
46
47 return data->getCollator();
48 }
49
50 U_CAPI void U_EXPORT2
ucd_freeCache()51 ucd_freeCache()
52 {
53 CollData::freeCollDataCache();
54 }
55
56 U_CAPI void U_EXPORT2
ucd_flushCache()57 ucd_flushCache()
58 {
59 CollData::flushCollDataCache();
60 }
61
62 struct BMS
63 {
64 BoyerMooreSearch *bms;
65 const UnicodeString *targetString;
66 };
67
68 U_CAPI BMS * U_EXPORT2
bms_open(UCD * ucd,const UChar * pattern,int32_t patternLength,const UChar * target,int32_t targetLength,UErrorCode * status)69 bms_open(UCD *ucd,
70 const UChar *pattern, int32_t patternLength,
71 const UChar *target, int32_t targetLength,
72 UErrorCode *status)
73 {
74 BMS *bms = STATIC_CAST(BMS *, uprv_malloc(sizeof(BMS)));
75
76 if (bms == NULL) {
77 *status = U_MEMORY_ALLOCATION_ERROR;
78 return NULL;
79 }
80
81 CollData *data = (CollData *) ucd;
82 UnicodeString patternString(pattern, patternLength);
83
84 if (target != NULL) {
85 bms->targetString = new UnicodeString(target, targetLength);
86
87 if (bms->targetString == NULL) {
88 bms->bms = NULL;
89 *status = U_MEMORY_ALLOCATION_ERROR;
90 return bms;
91 }
92 } else {
93 bms->targetString = NULL;
94 }
95
96 bms->bms = new BoyerMooreSearch(data, patternString, bms->targetString, *status);
97
98 if (bms->bms == NULL) {
99 *status = U_MEMORY_ALLOCATION_ERROR;
100 }
101
102 return bms;
103 }
104
105 U_CAPI void U_EXPORT2
bms_close(BMS * bms)106 bms_close(BMS *bms)
107 {
108 delete bms->bms;
109
110 delete bms->targetString;
111
112 uprv_free(bms);
113 }
114
115 U_CAPI UBool U_EXPORT2
bms_empty(BMS * bms)116 bms_empty(BMS *bms)
117 {
118 return bms->bms->empty();
119 }
120
121 U_CAPI UCD * U_EXPORT2
bms_getData(BMS * bms)122 bms_getData(BMS *bms)
123 {
124 return STATIC_CAST(UCD *, bms->bms->getData());
125 }
126
127 U_CAPI UBool U_EXPORT2
bms_search(BMS * bms,int32_t offset,int32_t * start,int32_t * end)128 bms_search(BMS *bms, int32_t offset, int32_t *start, int32_t *end)
129 {
130 return bms->bms->search(offset, *start, *end);
131 }
132
133 U_CAPI void U_EXPORT2
bms_setTargetString(BMS * bms,const UChar * target,int32_t targetLength,UErrorCode * status)134 bms_setTargetString(BMS *bms, const UChar *target, int32_t targetLength, UErrorCode *status)
135 {
136 if (U_FAILURE(*status)) {
137 return;
138 }
139
140 if (bms->targetString != NULL) {
141 delete bms->targetString;
142 }
143
144 if (target != NULL) {
145 bms->targetString = new UnicodeString(target, targetLength);
146 } else {
147 bms->targetString = NULL;
148 }
149
150 bms->bms->setTargetString(bms->targetString, *status);
151 }
152
153 #endif
154