Coverage Report

Created: 2026-05-06 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/i18n/collationroot.cpp
Line
Count
Source
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
*******************************************************************************
5
* Copyright (C) 2012-2014, International Business Machines
6
* Corporation and others.  All Rights Reserved.
7
*******************************************************************************
8
* collationroot.cpp
9
*
10
* created on: 2012dec17
11
* created by: Markus W. Scherer
12
*/
13
14
#include "unicode/utypes.h"
15
16
#if !UCONFIG_NO_COLLATION
17
18
#include "unicode/coll.h"
19
#include "unicode/udata.h"
20
#include "collation.h"
21
#include "collationdata.h"
22
#include "collationdatareader.h"
23
#include "collationroot.h"
24
#include "collationsettings.h"
25
#include "collationtailoring.h"
26
#include "normalizer2impl.h"
27
#include "ucln_in.h"
28
#include "udatamem.h"
29
#include "umutex.h"
30
#include "umapfile.h"
31
32
U_NAMESPACE_BEGIN
33
34
namespace {
35
36
const CollationCacheEntry *rootSingleton = nullptr;
37
UInitOnce initOnce{};
38
39
}  // namespace
40
41
U_CDECL_BEGIN
42
43
0
static UBool U_CALLCONV uprv_collation_root_cleanup() {
44
0
    SharedObject::clearPtr(rootSingleton);
45
0
    initOnce.reset();
46
0
    return true;
47
0
}
48
49
U_CDECL_END
50
51
UDataMemory*
52
0
CollationRoot::loadFromFile(const char* ucadataPath, UErrorCode &errorCode) {
53
0
    UDataMemory dataMemory;
54
0
    UDataMemory  *rDataMem = nullptr;
55
0
    if (U_FAILURE(errorCode)) {
56
0
        return nullptr;
57
0
    }
58
0
    if (uprv_mapFile(&dataMemory, ucadataPath, &errorCode)) {
59
0
        if (dataMemory.pHeader->dataHeader.magic1 == 0xda &&
60
0
            dataMemory.pHeader->dataHeader.magic2 == 0x27 &&
61
0
            CollationDataReader::isAcceptable(nullptr, "icu", "ucadata", &dataMemory.pHeader->info)) {
62
0
            rDataMem = UDataMemory_createNewInstance(&errorCode);
63
0
            if (U_FAILURE(errorCode)) {
64
0
                return nullptr;
65
0
            }
66
0
            rDataMem->pHeader = dataMemory.pHeader;
67
0
            rDataMem->mapAddr = dataMemory.mapAddr;
68
0
            rDataMem->map = dataMemory.map;
69
0
            return rDataMem;
70
0
        }
71
0
        errorCode = U_INVALID_FORMAT_ERROR;
72
0
        return nullptr;
73
0
    }
74
0
    errorCode = U_MISSING_RESOURCE_ERROR;
75
0
    return nullptr;
76
0
}
77
78
void U_CALLCONV
79
2
CollationRoot::load(const char* ucadataPath, UErrorCode &errorCode) {
80
2
    if(U_FAILURE(errorCode)) { return; }
81
2
    LocalPointer<CollationTailoring> t(new CollationTailoring(nullptr));
82
2
    if(t.isNull() || t->isBogus()) {
83
0
        errorCode = U_MEMORY_ALLOCATION_ERROR;
84
0
        return;
85
0
    }
86
2
    t->memory = ucadataPath ? CollationRoot::loadFromFile(ucadataPath, errorCode) :
87
2
                              udata_openChoice(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "coll",
88
2
                                               "icu", "ucadata",
89
2
                                               CollationDataReader::isAcceptable,
90
2
                                               t->version, &errorCode);
91
2
    if(U_FAILURE(errorCode)) { return; }
92
2
    const uint8_t *inBytes = static_cast<const uint8_t *>(udata_getMemory(t->memory));
93
2
    CollationDataReader::read(nullptr, inBytes, udata_getLength(t->memory), *t, errorCode);
94
2
    if(U_FAILURE(errorCode)) { return; }
95
2
    ucln_i18n_registerCleanup(UCLN_I18N_COLLATION_ROOT, uprv_collation_root_cleanup);
96
2
    CollationCacheEntry *entry = new CollationCacheEntry(Locale::getRoot(), t.getAlias());
97
2
    if(entry != nullptr) {
98
2
        t.orphan();  // The rootSingleton took ownership of the tailoring.
99
2
        entry->addRef();
100
2
        rootSingleton = entry;
101
2
    }
102
2
}
103
104
const CollationCacheEntry *
105
14.0k
CollationRoot::getRootCacheEntry(UErrorCode &errorCode) {
106
14.0k
    umtx_initOnce(initOnce, CollationRoot::load, static_cast<const char*>(nullptr), errorCode);
107
14.0k
    if(U_FAILURE(errorCode)) { return nullptr; }
108
14.0k
    return rootSingleton;
109
14.0k
}
110
111
const CollationTailoring *
112
7.22k
CollationRoot::getRoot(UErrorCode &errorCode) {
113
7.22k
    umtx_initOnce(initOnce, CollationRoot::load, static_cast<const char*>(nullptr), errorCode);
114
7.22k
    if(U_FAILURE(errorCode)) { return nullptr; }
115
7.22k
    return rootSingleton->tailoring;
116
7.22k
}
117
118
const CollationData *
119
0
CollationRoot::getData(UErrorCode &errorCode) {
120
0
    const CollationTailoring *root = getRoot(errorCode);
121
0
    if(U_FAILURE(errorCode)) { return nullptr; }
122
0
    return root->data;
123
0
}
124
125
const CollationSettings *
126
0
CollationRoot::getSettings(UErrorCode &errorCode) {
127
0
    const CollationTailoring *root = getRoot(errorCode);
128
0
    if(U_FAILURE(errorCode)) { return nullptr; }
129
0
    return root->settings;
130
0
}
131
132
void
133
0
CollationRoot::forceLoadFromFile(const char* ucadataPath, UErrorCode &errorCode) {
134
0
    umtx_initOnce(initOnce, CollationRoot::load, ucadataPath, errorCode);
135
0
}
136
137
138
U_NAMESPACE_END
139
140
#endif  // !UCONFIG_NO_COLLATION