Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/numparse_stringsegment.cpp
Line
Count
Source (jump to first uncovered line)
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 "numparse_stringsegment.h"
14
#include "putilimp.h"
15
#include "unicode/utf16.h"
16
#include "unicode/uniset.h"
17
18
using namespace icu;
19
using namespace icu::numparse;
20
using namespace icu::numparse::impl;
21
22
23
StringSegment::StringSegment(const UnicodeString& str, bool ignoreCase)
24
        : fStr(str), fStart(0), fEnd(str.length()),
25
0
          fFoldCase(ignoreCase) {}
26
27
0
int32_t StringSegment::getOffset() const {
28
0
    return fStart;
29
0
}
30
31
0
void StringSegment::setOffset(int32_t start) {
32
0
    fStart = start;
33
0
}
34
35
0
void StringSegment::adjustOffset(int32_t delta) {
36
0
    fStart += delta;
37
0
}
38
39
0
void StringSegment::adjustOffsetByCodePoint() {
40
0
    fStart += U16_LENGTH(getCodePoint());
41
0
}
42
43
0
void StringSegment::setLength(int32_t length) {
44
0
    fEnd = fStart + length;
45
0
}
46
47
0
void StringSegment::resetLength() {
48
0
    fEnd = fStr.length();
49
0
}
50
51
0
int32_t StringSegment::length() const {
52
0
    return fEnd - fStart;
53
0
}
54
55
0
char16_t StringSegment::charAt(int32_t index) const {
56
0
    return fStr.charAt(index + fStart);
57
0
}
58
59
0
UChar32 StringSegment::codePointAt(int32_t index) const {
60
0
    return fStr.char32At(index + fStart);
61
0
}
62
63
0
UnicodeString StringSegment::toUnicodeString() const {
64
0
    return UnicodeString(fStr.getBuffer() + fStart, fEnd - fStart);
65
0
}
66
67
0
const UnicodeString StringSegment::toTempUnicodeString() const {
68
0
    // Use the readonly-aliasing constructor for efficiency.
69
0
    return UnicodeString(FALSE, fStr.getBuffer() + fStart, fEnd - fStart);
70
0
}
71
72
0
UChar32 StringSegment::getCodePoint() const {
73
0
    char16_t lead = fStr.charAt(fStart);
74
0
    if (U16_IS_LEAD(lead) && fStart + 1 < fEnd) {
75
0
        return fStr.char32At(fStart);
76
0
    } else if (U16_IS_SURROGATE(lead)) {
77
0
        return -1;
78
0
    } else {
79
0
        return lead;
80
0
    }
81
0
}
82
83
0
bool StringSegment::startsWith(UChar32 otherCp) const {
84
0
    return codePointsEqual(getCodePoint(), otherCp, fFoldCase);
85
0
}
86
87
0
bool StringSegment::startsWith(const UnicodeSet& uniset) const {
88
0
    // TODO: Move UnicodeSet case-folding logic here.
89
0
    // TODO: Handle string matches here instead of separately.
90
0
    UChar32 cp = getCodePoint();
91
0
    if (cp == -1) {
92
0
        return false;
93
0
    }
94
0
    return uniset.contains(cp);
95
0
}
96
97
0
bool StringSegment::startsWith(const UnicodeString& other) const {
98
0
    if (other.isBogus() || other.length() == 0 || length() == 0) {
99
0
        return false;
100
0
    }
101
0
    int cp1 = getCodePoint();
102
0
    int cp2 = other.char32At(0);
103
0
    return codePointsEqual(cp1, cp2, fFoldCase);
104
0
}
105
106
0
int32_t StringSegment::getCommonPrefixLength(const UnicodeString& other) {
107
0
    return getPrefixLengthInternal(other, fFoldCase);
108
0
}
109
110
0
int32_t StringSegment::getCaseSensitivePrefixLength(const UnicodeString& other) {
111
0
    return getPrefixLengthInternal(other, false);
112
0
}
113
114
0
int32_t StringSegment::getPrefixLengthInternal(const UnicodeString& other, bool foldCase) {
115
0
    U_ASSERT(other.length() > 0);
116
0
    int32_t offset = 0;
117
0
    for (; offset < uprv_min(length(), other.length());) {
118
0
        // TODO: case-fold code points, not chars
119
0
        char16_t c1 = charAt(offset);
120
0
        char16_t c2 = other.charAt(offset);
121
0
        if (!codePointsEqual(c1, c2, foldCase)) {
122
0
            break;
123
0
        }
124
0
        offset++;
125
0
    }
126
0
    return offset;
127
0
}
128
129
0
bool StringSegment::codePointsEqual(UChar32 cp1, UChar32 cp2, bool foldCase) {
130
0
    if (cp1 == cp2) {
131
0
        return true;
132
0
    }
133
0
    if (!foldCase) {
134
0
        return false;
135
0
    }
136
0
    cp1 = u_foldCase(cp1, TRUE);
137
0
    cp2 = u_foldCase(cp2, TRUE);
138
0
    return cp1 == cp2;
139
0
}
140
141
0
bool StringSegment::operator==(const UnicodeString& other) const {
142
0
    return toTempUnicodeString() == other;
143
0
}
144
145
146
#endif /* #if !UCONFIG_NO_FORMATTING */