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/nfrlist.h
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) 1997-2012, International Business Machines
6
*   Corporation and others.  All Rights Reserved.
7
******************************************************************************
8
*   file name:  nfrlist.h
9
*   encoding:   UTF-8
10
*   tab size:   8 (not used)
11
*   indentation:4
12
*
13
* Modification history
14
* Date        Name      Comments
15
* 10/11/2001  Doug      Ported from ICU4J
16
*/
17
18
#ifndef NFRLIST_H
19
#define NFRLIST_H
20
21
#include "unicode/rbnf.h"
22
23
#if U_HAVE_RBNF
24
25
#include "unicode/uobject.h"
26
#include "nfrule.h"
27
28
#include "cmemory.h"
29
30
U_NAMESPACE_BEGIN
31
32
// unsafe class for internal use only.  assume memory allocations succeed, indexes are valid.
33
// should be a template, but we can't use them
34
35
class NFRuleList : public UMemory {
36
protected:
37
    NFRule** fStuff;
38
    uint32_t fCount;
39
    uint32_t fCapacity;
40
public:
41
    NFRuleList(uint32_t capacity = 10) 
42
256k
        : fStuff(capacity ? static_cast<NFRule**>(uprv_malloc(capacity * sizeof(NFRule*))) : nullptr)
43
256k
        , fCount(0)
44
256k
        , fCapacity(capacity) {}
45
256k
    ~NFRuleList() {
46
256k
        if (fStuff) {
47
2.82M
            for(uint32_t i = 0; i < fCount; ++i) {
48
2.57M
                delete fStuff[i];
49
2.57M
            }
50
251k
            uprv_free(fStuff);
51
251k
        }
52
256k
    }
53
500M
    NFRule* operator[](uint32_t index) const { return fStuff != nullptr ? fStuff[index] : nullptr; }
54
0
    NFRule* remove(uint32_t index) {
55
0
      if (fStuff == nullptr) {
56
0
        return nullptr;
57
0
      }
58
0
        NFRule* result = fStuff[index];
59
0
        fCount -= 1;
60
0
        for (uint32_t i = index; i < fCount; ++i) { // assumes small arrays
61
0
            fStuff[i] = fStuff[i+1];
62
0
        }
63
0
        return result;
64
0
    }
65
2.57M
    void add(NFRule* thing) {
66
2.57M
        if (fCount == fCapacity) {
67
343k
            fCapacity += 10;
68
343k
            fStuff = static_cast<NFRule**>(uprv_realloc(fStuff, fCapacity * sizeof(NFRule*))); // assume success
69
343k
        }
70
2.57M
        if (fStuff != nullptr) {
71
2.57M
          fStuff[fCount++] = thing;
72
2.57M
        } else {
73
0
          fCapacity = 0;
74
0
          fCount = 0;
75
0
        }
76
2.57M
    }
77
8.52M
    uint32_t size() const { return fCount; }
78
1.96M
    NFRule* last() const { return (fCount > 0 && fStuff != nullptr) ? fStuff[fCount-1] : nullptr; }
79
0
    NFRule** release() {
80
0
        add(nullptr); // ensure null termination
81
0
        NFRule** result = fStuff;
82
0
        fStuff = nullptr;
83
0
        fCount = 0;
84
0
        fCapacity = 0;
85
0
        return result;
86
0
    }
87
124k
    void deleteAll() {
88
124k
        NFRule** tmp = nullptr;
89
124k
        int32_t size = fCount;
90
124k
        if (size > 0) {
91
0
            tmp = release();
92
0
            for (int32_t i = 0; i < size; i++) {
93
0
                delete tmp[i];
94
0
            }
95
0
            if (tmp) {
96
0
                uprv_free(tmp);
97
0
            }
98
0
        }
99
124k
    }
100
101
private:
102
    NFRuleList(const NFRuleList &other); // forbid copying of this class
103
    NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class
104
};
105
106
U_NAMESPACE_END
107
108
/* U_HAVE_RBNF */
109
#endif
110
111
// NFRLIST_H
112
#endif