Coverage Report

Created: 2026-02-05 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/i18n/string_segment.cpp
Line
Count
Source
1
// © 2018 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
#include "unicode/utypes.h"
5
6
#if !UCONFIG_NO_FORMATTING
7
8
// Allow implicit conversion from char16_t* to UnicodeString for this file:
9
// Helpful in toString methods and elsewhere.
10
#define UNISTR_FROM_STRING_EXPLICIT
11
12
#include "numparse_types.h"
13
#include "string_segment.h"
14
#include "putilimp.h"
15
#include "unicode/utf16.h"
16
#include "unicode/uniset.h"
17
18
U_NAMESPACE_BEGIN
19
20
21
StringSegment::StringSegment(const UnicodeString& str, bool ignoreCase)
22
3.68M
        : fStr(str), fStart(0), fEnd(str.length()),
23
3.68M
          fFoldCase(ignoreCase) {}
24
25
104M
int32_t StringSegment::getOffset() const {
26
104M
    return fStart;
27
104M
}
28
29
2.47M
void StringSegment::setOffset(int32_t start) {
30
2.47M
    fStart = start;
31
2.47M
}
32
33
114M
void StringSegment::adjustOffset(int32_t delta) {
34
114M
    fStart += delta;
35
114M
}
36
37
20.8M
void StringSegment::adjustOffsetByCodePoint() {
38
20.8M
    fStart += U16_LENGTH(getCodePoint());
39
20.8M
}
40
41
7.88k
void StringSegment::setLength(int32_t length) {
42
7.88k
    fEnd = fStart + length;
43
7.88k
}
44
45
7.88k
void StringSegment::resetLength() {
46
7.88k
    fEnd = fStr.length();
47
7.88k
}
48
49
240M
int32_t StringSegment::length() const {
50
240M
    return fEnd - fStart;
51
240M
}
52
53
17.9M
char16_t StringSegment::charAt(int32_t index) const {
54
17.9M
    return fStr.charAt(index + fStart);
55
17.9M
}
56
57
22.7M
UChar32 StringSegment::codePointAt(int32_t index) const {
58
22.7M
    return fStr.char32At(index + fStart);
59
22.7M
}
60
61
0
UnicodeString StringSegment::toUnicodeString() const {
62
0
    return UnicodeString(fStr.getBuffer() + fStart, fEnd - fStart);
63
0
}
64
65
1.74k
UnicodeString StringSegment::toTempUnicodeString() const {
66
    // Use the readonly-aliasing constructor for efficiency.
67
1.74k
    return UnicodeString(false, fStr.getBuffer() + fStart, fEnd - fStart);
68
1.74k
}
69
70
200M
UChar32 StringSegment::getCodePoint() const {
71
200M
    char16_t lead = fStr.charAt(fStart);
72
200M
    if (U16_IS_LEAD(lead) && fStart + 1 < fEnd) {
73
1.83M
        return fStr.char32At(fStart);
74
198M
    } else if (U16_IS_SURROGATE(lead)) {
75
264k
        return -1;
76
198M
    } else {
77
198M
        return lead;
78
198M
    }
79
200M
}
80
81
26.2M
bool StringSegment::startsWith(UChar32 otherCp) const {
82
26.2M
    return codePointsEqual(getCodePoint(), otherCp, fFoldCase);
83
26.2M
}
84
85
29.5M
bool StringSegment::startsWith(const UnicodeSet& uniset) const {
86
    // TODO: Move UnicodeSet case-folding logic here.
87
    // TODO: Handle string matches here instead of separately.
88
29.5M
    UChar32 cp = getCodePoint();
89
29.5M
    if (cp == -1) {
90
144k
        return false;
91
144k
    }
92
29.3M
    return uniset.contains(cp);
93
29.5M
}
94
95
27.0M
bool StringSegment::startsWith(const UnicodeString& other) const {
96
27.0M
    if (other.isBogus() || other.length() == 0 || length() == 0) {
97
16.0M
        return false;
98
16.0M
    }
99
11.0M
    int cp1 = getCodePoint();
100
11.0M
    int cp2 = other.char32At(0);
101
11.0M
    return codePointsEqual(cp1, cp2, fFoldCase);
102
27.0M
}
103
104
14.3M
int32_t StringSegment::getCommonPrefixLength(const UnicodeString& other) {
105
14.3M
    return getPrefixLengthInternal(other, fFoldCase);
106
14.3M
}
107
108
1.27M
int32_t StringSegment::getCaseSensitivePrefixLength(const UnicodeString& other) {
109
1.27M
    return getPrefixLengthInternal(other, false);
110
1.27M
}
111
112
15.6M
int32_t StringSegment::getPrefixLengthInternal(const UnicodeString& other, bool foldCase) {
113
15.6M
    U_ASSERT(other.length() > 0);
114
15.6M
    int32_t offset = 0;
115
16.0M
    for (; offset < uprv_min(length(), other.length());) {
116
        // TODO: case-fold code points, not chars
117
15.7M
        char16_t c1 = charAt(offset);
118
15.7M
        char16_t c2 = other.charAt(offset);
119
15.7M
        if (!codePointsEqual(c1, c2, foldCase)) {
120
15.2M
            break;
121
15.2M
        }
122
434k
        offset++;
123
434k
    }
124
15.6M
    return offset;
125
15.6M
}
126
127
52.9M
bool StringSegment::codePointsEqual(UChar32 cp1, UChar32 cp2, bool foldCase) {
128
52.9M
    if (cp1 == cp2) {
129
22.4M
        return true;
130
22.4M
    }
131
30.4M
    if (!foldCase) {
132
1.27M
        return false;
133
1.27M
    }
134
29.1M
    cp1 = u_foldCase(cp1, true);
135
29.1M
    cp2 = u_foldCase(cp2, true);
136
29.1M
    return cp1 == cp2;
137
30.4M
}
138
139
76
bool StringSegment::operator==(const UnicodeString& other) const {
140
76
    return toTempUnicodeString() == other;
141
76
}
142
143
144
U_NAMESPACE_END
145
#endif /* #if !UCONFIG_NO_FORMATTING */