• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ********************************************************************************
3 *   Copyright (C) 1996-2015, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 ********************************************************************************
6 */
7 
8 #include "unicode/utypes.h"
9 
10 #if !UCONFIG_NO_BREAK_ITERATION
11 
12 #include "unicode/ubrk.h"
13 
14 #include "unicode/brkiter.h"
15 #include "unicode/uloc.h"
16 #include "unicode/ustring.h"
17 #include "unicode/uchriter.h"
18 #include "unicode/rbbi.h"
19 #include "rbbirb.h"
20 #include "uassert.h"
21 
22 U_NAMESPACE_USE
23 
24 //------------------------------------------------------------------------------
25 //
26 //    ubrk_open      Create a canned type of break iterator based on type (word, line, etc.)
27 //                   and locale.
28 //
29 //------------------------------------------------------------------------------
30 U_CAPI UBreakIterator* U_EXPORT2
ubrk_open(UBreakIteratorType type,const char * locale,const UChar * text,int32_t textLength,UErrorCode * status)31 ubrk_open(UBreakIteratorType type,
32       const char *locale,
33       const UChar *text,
34       int32_t textLength,
35       UErrorCode *status)
36 {
37 
38   if(U_FAILURE(*status)) return 0;
39 
40   BreakIterator *result = 0;
41 
42   switch(type) {
43 
44   case UBRK_CHARACTER:
45     result = BreakIterator::createCharacterInstance(Locale(locale), *status);
46     break;
47 
48   case UBRK_WORD:
49     result = BreakIterator::createWordInstance(Locale(locale), *status);
50     break;
51 
52   case UBRK_LINE:
53     result = BreakIterator::createLineInstance(Locale(locale), *status);
54     break;
55 
56   case UBRK_SENTENCE:
57     result = BreakIterator::createSentenceInstance(Locale(locale), *status);
58     break;
59 
60   case UBRK_TITLE:
61     result = BreakIterator::createTitleInstance(Locale(locale), *status);
62     break;
63 
64   default:
65     *status = U_ILLEGAL_ARGUMENT_ERROR;
66   }
67 
68   // check for allocation error
69   if (U_FAILURE(*status)) {
70      return 0;
71   }
72   if(result == 0) {
73     *status = U_MEMORY_ALLOCATION_ERROR;
74     return 0;
75   }
76 
77 
78   UBreakIterator *uBI = (UBreakIterator *)result;
79   if (text != NULL) {
80       ubrk_setText(uBI, text, textLength, status);
81   }
82   return uBI;
83 }
84 
85 
86 
87 //------------------------------------------------------------------------------
88 //
89 //   ubrk_openRules      open a break iterator from a set of break rules.
90 //                       Invokes the rule builder.
91 //
92 //------------------------------------------------------------------------------
93 U_CAPI UBreakIterator* U_EXPORT2
ubrk_openRules(const UChar * rules,int32_t rulesLength,const UChar * text,int32_t textLength,UParseError * parseErr,UErrorCode * status)94 ubrk_openRules(  const UChar        *rules,
95                        int32_t       rulesLength,
96                  const UChar        *text,
97                        int32_t       textLength,
98                        UParseError  *parseErr,
99                        UErrorCode   *status)  {
100 
101     if (status == NULL || U_FAILURE(*status)){
102         return 0;
103     }
104 
105     BreakIterator *result = 0;
106     UnicodeString ruleString(rules, rulesLength);
107     result = RBBIRuleBuilder::createRuleBasedBreakIterator(ruleString, parseErr, *status);
108     if(U_FAILURE(*status)) {
109         return 0;
110     }
111 
112     UBreakIterator *uBI = (UBreakIterator *)result;
113     if (text != NULL) {
114         ubrk_setText(uBI, text, textLength, status);
115     }
116     return uBI;
117 }
118 
119 
120 
121 
122 
123 U_CAPI UBreakIterator * U_EXPORT2
ubrk_safeClone(const UBreakIterator * bi,void *,int32_t * pBufferSize,UErrorCode * status)124 ubrk_safeClone(
125           const UBreakIterator *bi,
126           void * /*stackBuffer*/,
127           int32_t *pBufferSize,
128           UErrorCode *status)
129 {
130     if (status == NULL || U_FAILURE(*status)){
131         return NULL;
132     }
133     if (bi == NULL) {
134        *status = U_ILLEGAL_ARGUMENT_ERROR;
135         return NULL;
136     }
137     if (pBufferSize != NULL) {
138         int32_t inputSize = *pBufferSize;
139         *pBufferSize = 1;
140         if (inputSize == 0) {
141             return NULL;  // preflighting for deprecated functionality
142         }
143     }
144     BreakIterator *newBI = ((BreakIterator *)bi)->clone();
145     if (newBI == NULL) {
146         *status = U_MEMORY_ALLOCATION_ERROR;
147     } else {
148         *status = U_SAFECLONE_ALLOCATED_WARNING;
149     }
150     return (UBreakIterator *)newBI;
151 }
152 
153 
154 
155 U_CAPI void U_EXPORT2
ubrk_close(UBreakIterator * bi)156 ubrk_close(UBreakIterator *bi)
157 {
158     delete (BreakIterator *)bi;
159 }
160 
161 U_CAPI void U_EXPORT2
ubrk_setText(UBreakIterator * bi,const UChar * text,int32_t textLength,UErrorCode * status)162 ubrk_setText(UBreakIterator* bi,
163              const UChar*    text,
164              int32_t         textLength,
165              UErrorCode*     status)
166 {
167     UText  ut = UTEXT_INITIALIZER;
168     utext_openUChars(&ut, text, textLength, status);
169     ((BreakIterator*)bi)->setText(&ut, *status);
170     // A stack allocated UText wrapping a UChar * string
171     //   can be dumped without explicitly closing it.
172 }
173 
174 
175 
176 U_CAPI void U_EXPORT2
ubrk_setUText(UBreakIterator * bi,UText * text,UErrorCode * status)177 ubrk_setUText(UBreakIterator *bi,
178              UText          *text,
179              UErrorCode     *status)
180 {
181   ((BreakIterator*)bi)->setText(text, *status);
182 }
183 
184 
185 
186 
187 
188 U_CAPI int32_t U_EXPORT2
ubrk_current(const UBreakIterator * bi)189 ubrk_current(const UBreakIterator *bi)
190 {
191 
192   return ((BreakIterator*)bi)->current();
193 }
194 
195 U_CAPI int32_t U_EXPORT2
ubrk_next(UBreakIterator * bi)196 ubrk_next(UBreakIterator *bi)
197 {
198 
199   return ((BreakIterator*)bi)->next();
200 }
201 
202 U_CAPI int32_t U_EXPORT2
ubrk_previous(UBreakIterator * bi)203 ubrk_previous(UBreakIterator *bi)
204 {
205 
206   return ((BreakIterator*)bi)->previous();
207 }
208 
209 U_CAPI int32_t U_EXPORT2
ubrk_first(UBreakIterator * bi)210 ubrk_first(UBreakIterator *bi)
211 {
212 
213   return ((BreakIterator*)bi)->first();
214 }
215 
216 U_CAPI int32_t U_EXPORT2
ubrk_last(UBreakIterator * bi)217 ubrk_last(UBreakIterator *bi)
218 {
219 
220   return ((BreakIterator*)bi)->last();
221 }
222 
223 U_CAPI int32_t U_EXPORT2
ubrk_preceding(UBreakIterator * bi,int32_t offset)224 ubrk_preceding(UBreakIterator *bi,
225            int32_t offset)
226 {
227 
228   return ((BreakIterator*)bi)->preceding(offset);
229 }
230 
231 U_CAPI int32_t U_EXPORT2
ubrk_following(UBreakIterator * bi,int32_t offset)232 ubrk_following(UBreakIterator *bi,
233            int32_t offset)
234 {
235 
236   return ((BreakIterator*)bi)->following(offset);
237 }
238 
239 U_CAPI const char* U_EXPORT2
ubrk_getAvailable(int32_t index)240 ubrk_getAvailable(int32_t index)
241 {
242 
243   return uloc_getAvailable(index);
244 }
245 
246 U_CAPI int32_t U_EXPORT2
ubrk_countAvailable()247 ubrk_countAvailable()
248 {
249 
250   return uloc_countAvailable();
251 }
252 
253 
254 U_CAPI  UBool U_EXPORT2
ubrk_isBoundary(UBreakIterator * bi,int32_t offset)255 ubrk_isBoundary(UBreakIterator *bi, int32_t offset)
256 {
257     return ((BreakIterator*)bi)->isBoundary(offset);
258 }
259 
260 
261 U_CAPI  int32_t U_EXPORT2
ubrk_getRuleStatus(UBreakIterator * bi)262 ubrk_getRuleStatus(UBreakIterator *bi)
263 {
264     return ((BreakIterator*)bi)->getRuleStatus();
265 }
266 
267 U_CAPI  int32_t U_EXPORT2
ubrk_getRuleStatusVec(UBreakIterator * bi,int32_t * fillInVec,int32_t capacity,UErrorCode * status)268 ubrk_getRuleStatusVec(UBreakIterator *bi, int32_t *fillInVec, int32_t capacity, UErrorCode *status)
269 {
270     return ((BreakIterator*)bi)->getRuleStatusVec(fillInVec, capacity, *status);
271 }
272 
273 
274 U_CAPI const char* U_EXPORT2
ubrk_getLocaleByType(const UBreakIterator * bi,ULocDataLocaleType type,UErrorCode * status)275 ubrk_getLocaleByType(const UBreakIterator *bi,
276                      ULocDataLocaleType type,
277                      UErrorCode* status)
278 {
279     if (bi == NULL) {
280         if (U_SUCCESS(*status)) {
281             *status = U_ILLEGAL_ARGUMENT_ERROR;
282         }
283         return NULL;
284     }
285     return ((BreakIterator*)bi)->getLocaleID(type, *status);
286 }
287 
288 
ubrk_refreshUText(UBreakIterator * bi,UText * text,UErrorCode * status)289 void ubrk_refreshUText(UBreakIterator *bi,
290                        UText          *text,
291                        UErrorCode     *status)
292 {
293     BreakIterator *bii = reinterpret_cast<BreakIterator *>(bi);
294     bii->refreshInputText(text, *status);
295 }
296 
297 
298 
299 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
300