1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2004-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 * Created: March 22 2004
11 **********************************************************************
15 #include "patternprops.h"
19 TokenIterator::TokenIterator(TextFile
* r
) {
21 done
= haveLine
= FALSE
;
25 TokenIterator::~TokenIterator() {
28 UBool
TokenIterator::next(UnicodeString
& token
, UErrorCode
& ec
) {
29 if (done
|| U_FAILURE(ec
)) {
35 if (!reader
->readLineSkippingComments(line
, ec
)) {
43 if (!nextToken(token
, ec
)) {
45 if (U_FAILURE(ec
)) return FALSE
;
52 int32_t TokenIterator::getLineNumber() const {
53 return reader
->getLineNumber();
57 * Read the next token from 'this->line' and append it to 'token'.
58 * Tokens are separated by Pattern_White_Space. Tokens may also be
59 * delimited by double or single quotes. The closing quote must match
60 * the opening quote. If a '#' is encountered, the rest of the line
61 * is ignored, unless it is backslash-escaped or within quotes.
62 * @param token the token is appended to this StringBuffer
63 * @param ec input-output error code
64 * @return TRUE if a valid token is found, or FALSE if the end
65 * of the line is reached or an error occurs
67 UBool
TokenIterator::nextToken(UnicodeString
& token
, UErrorCode
& ec
) {
68 ICU_Utility::skipWhitespace(line
, pos
, TRUE
);
69 if (pos
== line
.length()) {
72 UChar c
= line
.charAt(pos
++);
85 while (pos
< line
.length()) {
86 c
= line
.charAt(pos
); // 16-bit ok
87 if (c
== 92/*'\\'*/) {
88 UChar32 c32
= line
.unescapeAt(pos
);
90 ec
= U_MALFORMED_UNICODE_ESCAPE
;
94 } else if ((quote
!= 0 && c
== quote
) ||
95 (quote
== 0 && PatternProps::isWhiteSpace(c
))) {
98 } else if (quote
== 0 && c
== '#') {
99 return TRUE
; // do NOT increment
106 ec
= U_UNTERMINATED_QUOTE
;