summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-4.2.1/i18n/bms.cpp
blob: d86a98401f6bb22685a564339a9c4a673e906e2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2008-2009, International Business Machines Corporation and Others.
 * All rights reserved.
 */

#include "unicode/utypes.h"
#include "cmemory.h"
#include "unicode/bms.h"
#include "unicode/unistr.h"
#include "unicode/colldata.h"
#include "unicode/bmsearch.h"


#if !UCONFIG_NO_COLLATION


//#define USE_SAFE_CASTS
#ifdef USE_SAFE_CASTS
#define STATIC_CAST(type,value) static_cast<type>(value)
#define CONST_CAST(type,value) const_cast<type>(value)
#else
#define STATIC_CAST(type,value) (type) (value)
#define CONST_CAST(type,value) (type) (value)
#endif

U_CAPI UCD * U_EXPORT2
ucd_open(UCollator *coll, UErrorCode *status)
{
    return STATIC_CAST(UCD *, CollData::open(coll, *status));
}

U_CAPI void U_EXPORT2
ucd_close(UCD *ucd)
{
    CollData *data = STATIC_CAST(CollData *, ucd);

    CollData::close(data);
}

U_CAPI UCollator * U_EXPORT2
ucd_getCollator(UCD *ucd)
{
    CollData *data = STATIC_CAST(CollData *, ucd);

    return data->getCollator();
}

U_CAPI void U_EXPORT2
ucd_freeCache()
{
    CollData::freeCollDataCache();
}

U_CAPI void U_EXPORT2
ucd_flushCache()
{
    CollData::flushCollDataCache();
}

struct BMS
{
    BoyerMooreSearch *bms;
    const UnicodeString *targetString;
};

U_CAPI BMS * U_EXPORT2
bms_open(UCD *ucd,
         const UChar *pattern, int32_t patternLength,
         const UChar *target,  int32_t targetLength,
         UErrorCode  *status)
{
    BMS *bms = STATIC_CAST(BMS *, uprv_malloc(sizeof(BMS)));

    if (bms == NULL) {
        *status = U_MEMORY_ALLOCATION_ERROR;
        return NULL;
    }

    CollData *data = (CollData *) ucd;
    UnicodeString patternString(pattern, patternLength);

    if (target != NULL) {
        bms->targetString = new UnicodeString(target, targetLength);
        
        if (bms->targetString == NULL) {
            bms->bms = NULL;
            *status = U_MEMORY_ALLOCATION_ERROR;
            return bms;
        }
    } else {
        bms->targetString = NULL;
    }

    bms->bms = new BoyerMooreSearch(data, patternString, bms->targetString, *status);

    if (bms->bms == NULL) {
        *status = U_MEMORY_ALLOCATION_ERROR;
    }

    return bms;
}

U_CAPI void U_EXPORT2
bms_close(BMS *bms)
{
    delete bms->bms;

    delete bms->targetString;

    uprv_free(bms);
}

U_CAPI UBool U_EXPORT2
bms_empty(BMS *bms)
{
    return bms->bms->empty();
}

U_CAPI UCD * U_EXPORT2
bms_getData(BMS *bms)
{
    return STATIC_CAST(UCD *, bms->bms->getData());
}

U_CAPI UBool U_EXPORT2
bms_search(BMS *bms, int32_t offset, int32_t *start, int32_t *end)
{
    return bms->bms->search(offset, *start, *end);
}

U_CAPI void U_EXPORT2
bms_setTargetString(BMS *bms, const UChar *target, int32_t targetLength, UErrorCode *status)
{
    if (U_FAILURE(*status)) {
        return;
    }

    if (bms->targetString != NULL) {
        delete bms->targetString;
    }

    if (target != NULL) {
        bms->targetString = new UnicodeString(target, targetLength);
    } else {
        bms->targetString = NULL;
    }

    bms->bms->setTargetString(bms->targetString, *status);
}

#endif