2 **********************************************************************
3 * Copyright (c) 2004-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: March 22 2004
9 **********************************************************************
13 #include "patternprops.h"
17 TokenIterator::TokenIterator(TextFile
* r
) {
19 done
= haveLine
= FALSE
;
23 TokenIterator::~TokenIterator() {
26 UBool
TokenIterator::next(UnicodeString
& token
, UErrorCode
& ec
) {
27 if (done
|| U_FAILURE(ec
)) {
33 if (!reader
->readLineSkippingComments(line
, ec
)) {
41 if (!nextToken(token
, ec
)) {
43 if (U_FAILURE(ec
)) return FALSE
;
50 int32_t TokenIterator::getLineNumber() const {
51 return reader
->getLineNumber();
55 * Read the next token from 'this->line' and append it to 'token'.
56 * Tokens are separated by Pattern_White_Space. Tokens may also be
57 * delimited by double or single quotes. The closing quote must match
58 * the opening quote. If a '#' is encountered, the rest of the line
59 * is ignored, unless it is backslash-escaped or within quotes.
60 * @param token the token is appended to this StringBuffer
61 * @param ec input-output error code
62 * @return TRUE if a valid token is found, or FALSE if the end
63 * of the line is reached or an error occurs
65 UBool
TokenIterator::nextToken(UnicodeString
& token
, UErrorCode
& ec
) {
66 ICU_Utility::skipWhitespace(line
, pos
, TRUE
);
67 if (pos
== line
.length()) {
70 UChar c
= line
.charAt(pos
++);
83 while (pos
< line
.length()) {
84 c
= line
.charAt(pos
); // 16-bit ok
85 if (c
== 92/*'\\'*/) {
86 UChar32 c32
= line
.unescapeAt(pos
);
88 ec
= U_MALFORMED_UNICODE_ESCAPE
;
92 } else if ((quote
!= 0 && c
== quote
) ||
93 (quote
== 0 && PatternProps::isWhiteSpace(c
))) {
96 } else if (quote
== 0 && c
== '#') {
97 return TRUE
; // do NOT increment
104 ec
= U_UNTERMINATED_QUOTE
;