• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008-2011, 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     if (ucd != NULL) {
38         CollData *data = STATIC_CAST(CollData *, ucd);
39 
40         CollData::close(data);
41     }
42 }
43 
44 U_CAPI UCollator * U_EXPORT2
ucd_getCollator(UCD * ucd)45 ucd_getCollator(UCD *ucd)
46 {
47     CollData *data = STATIC_CAST(CollData *, ucd);
48 
49     return data->getCollator();
50 }
51 
52 U_CAPI void U_EXPORT2
ucd_freeCache()53 ucd_freeCache()
54 {
55     CollData::freeCollDataCache();
56 }
57 
58 U_CAPI void U_EXPORT2
ucd_flushCache()59 ucd_flushCache()
60 {
61     CollData::flushCollDataCache();
62 }
63 
64 struct BMS
65 {
66     BoyerMooreSearch *bms;
67     const UnicodeString *targetString;
68 };
69 
70 U_CAPI BMS * U_EXPORT2
bms_open(UCD * ucd,const UChar * pattern,int32_t patternLength,const UChar * target,int32_t targetLength,UErrorCode * status)71 bms_open(UCD *ucd,
72          const UChar *pattern, int32_t patternLength,
73          const UChar *target,  int32_t targetLength,
74          UErrorCode  *status)
75 {
76     BMS *bms = STATIC_CAST(BMS *, uprv_malloc(sizeof(BMS)));
77 
78     if (bms == NULL) {
79         *status = U_MEMORY_ALLOCATION_ERROR;
80         return NULL;
81     }
82 
83     CollData *data = (CollData *) ucd;
84     UnicodeString patternString(pattern, patternLength);
85 
86     if (target != NULL) {
87         bms->targetString = new UnicodeString(target, targetLength);
88 
89         if (bms->targetString == NULL) {
90             bms->bms = NULL;
91             *status = U_MEMORY_ALLOCATION_ERROR;
92             return bms;
93         }
94     } else {
95         bms->targetString = NULL;
96     }
97 
98     bms->bms = new BoyerMooreSearch(data, patternString, bms->targetString, *status);
99 
100     if (bms->bms == NULL) {
101         *status = U_MEMORY_ALLOCATION_ERROR;
102     }
103 
104     return bms;
105 }
106 
107 U_CAPI void U_EXPORT2
bms_close(BMS * bms)108 bms_close(BMS *bms)
109 {
110     delete bms->bms;
111 
112     delete bms->targetString;
113 
114     uprv_free(bms);
115 }
116 
117 U_CAPI UBool U_EXPORT2
bms_empty(BMS * bms)118 bms_empty(BMS *bms)
119 {
120     return bms->bms->empty();
121 }
122 
123 U_CAPI UCD * U_EXPORT2
bms_getData(BMS * bms)124 bms_getData(BMS *bms)
125 {
126     return STATIC_CAST(UCD *, bms->bms->getData());
127 }
128 
129 U_CAPI UBool U_EXPORT2
bms_search(BMS * bms,int32_t offset,int32_t * start,int32_t * end)130 bms_search(BMS *bms, int32_t offset, int32_t *start, int32_t *end)
131 {
132     return bms->bms->search(offset, *start, *end);
133 }
134 
135 U_CAPI void U_EXPORT2
bms_setTargetString(BMS * bms,const UChar * target,int32_t targetLength,UErrorCode * status)136 bms_setTargetString(BMS *bms, const UChar *target, int32_t targetLength, UErrorCode *status)
137 {
138     if (U_FAILURE(*status)) {
139         return;
140     }
141 
142     if (bms->targetString != NULL) {
143         delete bms->targetString;
144     }
145 
146     if (target != NULL) {
147         bms->targetString = new UnicodeString(target, targetLength);
148     } else {
149         bms->targetString = NULL;
150     }
151 
152     bms->bms->setTargetString(bms->targetString, *status);
153 }
154 
155 #endif
156