Coverage Report

Created: 2025-06-13 06:38

/src/icu/icu4c/source/common/locbased.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) 2004-2014, International Business Machines
6
* Corporation and others.  All Rights Reserved.
7
**********************************************************************
8
* Author: Alan Liu
9
* Created: January 16 2004
10
* Since: ICU 2.8
11
**********************************************************************
12
*/
13
#include "locbased.h"
14
#include "cstring.h"
15
#include "charstr.h"
16
17
U_NAMESPACE_BEGIN
18
19
Locale LocaleBased::getLocale(const CharString* valid, const CharString* actual,
20
11.4k
                              ULocDataLocaleType type, UErrorCode& status) {
21
11.4k
    const char* id = getLocaleID(valid, actual, type, status);
22
11.4k
    return Locale(id != nullptr ? id : "");
23
11.4k
}
24
25
const char* LocaleBased::getLocaleID(const CharString* valid, const CharString* actual,
26
11.4k
                                     ULocDataLocaleType type, UErrorCode& status) {
27
11.4k
    if (U_FAILURE(status)) {
28
0
        return nullptr;
29
0
    }
30
31
11.4k
    switch(type) {
32
11.3k
    case ULOC_VALID_LOCALE:
33
11.3k
        return valid == nullptr ? "" : valid->data();
34
174
    case ULOC_ACTUAL_LOCALE:
35
174
        return actual == nullptr ? "" : actual->data();
36
0
    default:
37
0
        status = U_ILLEGAL_ARGUMENT_ERROR;
38
0
        return nullptr;
39
11.4k
    }
40
11.4k
}
41
42
1.90M
void LocaleBased::setLocaleIDs(const CharString* validID, const CharString* actualID, UErrorCode& status) {
43
1.90M
    setValidLocaleID(validID, status);
44
1.90M
    setActualLocaleID(actualID,status);
45
1.90M
}
46
462k
void LocaleBased::setLocaleIDs(const char* validID, const char* actualID, UErrorCode& status) {
47
462k
    setValidLocaleID(validID, status);
48
462k
    setActualLocaleID(actualID,status);
49
462k
}
50
51
946k
void LocaleBased::setLocaleID(const char* id, CharString*& dest, UErrorCode& status) {
52
946k
    if (U_FAILURE(status)) { return; }
53
946k
    if (id == nullptr || *id == 0) {
54
2.78k
        delete dest;
55
2.78k
        dest = nullptr;
56
943k
    } else {
57
943k
        if (dest == nullptr) {
58
942k
            dest = new CharString(id, status);
59
942k
            if (dest == nullptr) {
60
0
                status = U_MEMORY_ALLOCATION_ERROR;
61
0
                return;
62
0
            }
63
942k
        } else {
64
528
            dest->copyFrom(id, status);
65
528
        }
66
943k
    }
67
946k
}
68
69
3.81M
void LocaleBased::setLocaleID(const CharString* id, CharString*& dest, UErrorCode& status) {
70
3.81M
    if (U_FAILURE(status)) { return; }
71
3.81M
    if (id == nullptr || id->isEmpty()) {
72
8.86k
        delete dest;
73
8.86k
        dest = nullptr;
74
3.80M
    } else {
75
3.80M
        if (dest == nullptr) {
76
3.50M
            dest = new CharString(*id, status);
77
3.50M
            if (dest == nullptr) {
78
0
                status = U_MEMORY_ALLOCATION_ERROR;
79
0
                return;
80
0
            }
81
3.50M
        } else {
82
296k
            dest->copyFrom(*id, status);
83
296k
        }
84
3.80M
    }
85
3.81M
}
86
87
0
bool LocaleBased::equalIDs(const CharString* left, const CharString* right) {
88
    // true if both are nullptr
89
0
    if (left == nullptr && right == nullptr) return true;
90
    // false if only one is nullptr
91
0
    if (left == nullptr || right == nullptr) return false;
92
0
    return *left == *right;
93
0
}
94
95
U_NAMESPACE_END