Coverage Report

Created: 2022-11-20 06:14

/src/icu/icu4c/source/common/ruleiter.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) 2003-2011, International Business Machines
6
* Corporation and others.  All Rights Reserved.
7
**********************************************************************
8
* Author: Alan Liu
9
* Created: September 24 2003
10
* Since: ICU 2.8
11
**********************************************************************
12
*/
13
#include "ruleiter.h"
14
#include "unicode/parsepos.h"
15
#include "unicode/symtable.h"
16
#include "unicode/unistr.h"
17
#include "unicode/utf16.h"
18
#include "patternprops.h"
19
20
/* \U87654321 or \ud800\udc00 */
21
7
#define MAX_U_NOTATION_LEN 12
22
23
U_NAMESPACE_BEGIN
24
25
RuleCharacterIterator::RuleCharacterIterator(const UnicodeString& theText, const SymbolTable* theSym,
26
                      ParsePosition& thePos) :
27
    text(theText),
28
    pos(thePos),
29
    sym(theSym),
30
    buf(0),
31
    bufPos(0)
32
13
{}
33
34
76
UBool RuleCharacterIterator::atEnd() const {
35
76
    return buf == 0 && pos.getIndex() == text.length();
36
76
}
37
38
182
UChar32 RuleCharacterIterator::next(int32_t options, UBool& isEscaped, UErrorCode& ec) {
39
182
    if (U_FAILURE(ec)) return DONE;
40
41
182
    UChar32 c = DONE;
42
182
    isEscaped = false;
43
44
182
    for (;;) {
45
182
        c = _current();
46
182
        _advance(U16_LENGTH(c));
47
48
182
        if (c == SymbolTable::SYMBOL_REF && buf == 0 &&
49
182
            (options & PARSE_VARIABLES) != 0 && sym != 0) {
50
0
            UnicodeString name = sym->parseReference(text, pos, text.length());
51
            // If name is empty there was an isolated SYMBOL_REF;
52
            // return it.  Caller must be prepared for this.
53
0
            if (name.length() == 0) {
54
0
                break;
55
0
            }
56
0
            bufPos = 0;
57
0
            buf = sym->lookup(name);
58
0
            if (buf == 0) {
59
0
                ec = U_UNDEFINED_VARIABLE;
60
0
                return DONE;
61
0
            }
62
            // Handle empty variable value
63
0
            if (buf->length() == 0) {
64
0
                buf = 0;
65
0
            }
66
0
            continue;
67
0
        }
68
69
182
        if ((options & SKIP_WHITESPACE) != 0 && PatternProps::isWhiteSpace(c)) {
70
0
            continue;
71
0
        }
72
73
182
        if (c == 0x5C /*'\\'*/ && (options & PARSE_ESCAPES) != 0) {
74
7
            UnicodeString tempEscape;
75
7
            int32_t offset = 0;
76
7
            c = lookahead(tempEscape, MAX_U_NOTATION_LEN).unescapeAt(offset);
77
7
            jumpahead(offset);
78
7
            isEscaped = true;
79
7
            if (c < 0) {
80
0
                ec = U_MALFORMED_UNICODE_ESCAPE;
81
0
                return DONE;
82
0
            }
83
7
        }
84
85
182
        break;
86
182
    }
87
88
182
    return c;
89
182
}
90
91
131
void RuleCharacterIterator::getPos(RuleCharacterIterator::Pos& p) const {
92
131
    p.buf = buf;
93
131
    p.pos = pos.getIndex();
94
131
    p.bufPos = bufPos;
95
131
}
96
97
88
void RuleCharacterIterator::setPos(const RuleCharacterIterator::Pos& p) {
98
88
    buf = p.buf;
99
88
    pos.setIndex(p.pos);
100
88
    bufPos = p.bufPos;
101
88
}
102
103
46
void RuleCharacterIterator::skipIgnored(int32_t options) {
104
46
    if ((options & SKIP_WHITESPACE) != 0) {
105
46
        for (;;) {
106
46
            UChar32 a = _current();
107
46
            if (!PatternProps::isWhiteSpace(a)) break;
108
0
            _advance(U16_LENGTH(a));
109
0
        }
110
46
    }
111
46
}
112
113
40
UnicodeString& RuleCharacterIterator::lookahead(UnicodeString& result, int32_t maxLookAhead) const {
114
40
    if (maxLookAhead < 0) {
115
33
        maxLookAhead = 0x7FFFFFFF;
116
33
    }
117
40
    if (buf != 0) {
118
0
        buf->extract(bufPos, maxLookAhead, result);
119
40
    } else {
120
40
        text.extract(pos.getIndex(), maxLookAhead, result);
121
40
    }
122
40
    return result;
123
40
}
124
125
40
void RuleCharacterIterator::jumpahead(int32_t count) {
126
40
    _advance(count);
127
40
}
128
129
/*
130
UnicodeString& RuleCharacterIterator::toString(UnicodeString& result) const {
131
    int32_t b = pos.getIndex();
132
    text.extract(0, b, result);
133
    return result.append((UChar) 0x7C).append(text, b, 0x7FFFFFFF); // Insert '|' at index
134
}
135
*/
136
137
228
UChar32 RuleCharacterIterator::_current() const {
138
228
    if (buf != 0) {
139
0
        return buf->char32At(bufPos);
140
228
    } else {
141
228
        int i = pos.getIndex();
142
228
        return (i < text.length()) ? text.char32At(i) : (UChar32)DONE;
143
228
    }
144
228
}
145
146
222
void RuleCharacterIterator::_advance(int32_t count) {
147
222
    if (buf != 0) {
148
0
        bufPos += count;
149
0
        if (bufPos == buf->length()) {
150
0
            buf = 0;
151
0
        }
152
222
    } else {
153
222
        pos.setIndex(pos.getIndex() + count);
154
222
        if (pos.getIndex() > text.length()) {
155
0
            pos.setIndex(text.length());
156
0
        }
157
222
    }
158
222
}
159
160
U_NAMESPACE_END
161
162
//eof