Coverage Report

Created: 2018-09-25 14:53

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