]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/ruleiter.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2003-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 * Created: September 24 2003
11 **********************************************************************
16 #include "unicode/uobject.h"
25 * An iterator that returns 32-bit code points. This class is deliberately
26 * <em>not</em> related to any of the ICU character iterator classes
27 * in order to minimize complexity.
31 class RuleCharacterIterator
: public UMemory
{
33 // TODO: Ideas for later. (Do not implement if not needed, lest the
34 // code coverage numbers go down due to unused methods.)
35 // 1. Add a copy constructor, operator==() method.
36 // 2. Rather than return DONE, throw an exception if the end
37 // is reached -- this is an alternate usage model, probably not useful.
41 * Text being iterated.
43 const UnicodeString
& text
;
46 * Position of iterator.
51 * Symbol table used to parse and dereference variables. May be 0.
53 const SymbolTable
* sym
;
56 * Current variable expansion, or 0 if none.
58 const UnicodeString
* buf
;
61 * Position within buf. Meaningless if buf == 0.
67 * Value returned when there are no more characters to iterate.
72 * Bitmask option to enable parsing of variable names. If (options &
73 * PARSE_VARIABLES) != 0, then an embedded variable will be expanded to
74 * its value. Variables are parsed using the SymbolTable API.
76 enum { PARSE_VARIABLES
= 1 };
79 * Bitmask option to enable parsing of escape sequences. If (options &
80 * PARSE_ESCAPES) != 0, then an embedded escape sequence will be expanded
81 * to its value. Escapes are parsed using Utility.unescapeAt().
83 enum { PARSE_ESCAPES
= 2 };
86 * Bitmask option to enable skipping of whitespace. If (options &
87 * SKIP_WHITESPACE) != 0, then Pattern_White_Space characters will be silently
88 * skipped, as if they were not present in the input.
90 enum { SKIP_WHITESPACE
= 4 };
93 * Constructs an iterator over the given text, starting at the given
95 * @param text the text to be iterated
96 * @param sym the symbol table, or null if there is none. If sym is null,
97 * then variables will not be deferenced, even if the PARSE_VARIABLES
99 * @param pos upon input, the index of the next character to return. If a
100 * variable has been dereferenced, then pos will <em>not</em> increment as
101 * characters of the variable value are iterated.
103 RuleCharacterIterator(const UnicodeString
& text
, const SymbolTable
* sym
,
107 * Returns true if this iterator has no more characters to return.
112 * Returns the next character using the given options, or DONE if there
113 * are no more characters, and advance the position to the next
115 * @param options one or more of the following options, bitwise-OR-ed
116 * together: PARSE_VARIABLES, PARSE_ESCAPES, SKIP_WHITESPACE.
117 * @param isEscaped output parameter set to TRUE if the character
119 * @param ec input-output error code. An error will only be set by
120 * this routing if options includes PARSE_VARIABLES and an unknown
121 * variable name is seen, or if options includes PARSE_ESCAPES and
122 * an invalid escape sequence is seen.
123 * @return the current 32-bit code point, or DONE
125 UChar32
next(int32_t options
, UBool
& isEscaped
, UErrorCode
& ec
);
128 * Returns true if this iterator is currently within a variable expansion.
130 inline UBool
inVariable() const;
133 * An opaque object representing the position of a RuleCharacterIterator.
135 struct Pos
: public UMemory
{
137 const UnicodeString
* buf
;
140 friend class RuleCharacterIterator
;
144 * Sets an object which, when later passed to setPos(), will
145 * restore this iterator's position. Usage idiom:
147 * RuleCharacterIterator iterator = ...;
148 * RuleCharacterIterator::Pos pos;
149 * iterator.getPos(pos);
151 * iterator.getPos(pos);
152 * int c = iterator.next(...);
155 * iterator.setPos(pos);
157 * @param p a position object to be set to this iterator's
160 void getPos(Pos
& p
) const;
163 * Restores this iterator to the position it had when getPos()
164 * set the given object.
165 * @param p a position object previously set by getPos()
167 void setPos(const Pos
& p
);
170 * Skips ahead past any ignored characters, as indicated by the given
171 * options. This is useful in conjunction with the lookahead() method.
173 * Currently, this only has an effect for SKIP_WHITESPACE.
174 * @param options one or more of the following options, bitwise-OR-ed
175 * together: PARSE_VARIABLES, PARSE_ESCAPES, SKIP_WHITESPACE.
177 void skipIgnored(int32_t options
);
180 * Returns a string containing the remainder of the characters to be
181 * returned by this iterator, without any option processing. If the
182 * iterator is currently within a variable expansion, this will only
183 * extend to the end of the variable expansion. This method is provided
184 * so that iterators may interoperate with string-based APIs. The typical
185 * sequence of calls is to call skipIgnored(), then call lookahead(), then
186 * parse the string returned by lookahead(), then call jumpahead() to
187 * resynchronize the iterator.
188 * @param result a string to receive the characters to be returned
189 * by future calls to next()
190 * @param maxLookAhead The maximum to copy into the result.
191 * @return a reference to result
193 UnicodeString
& lookahead(UnicodeString
& result
, int32_t maxLookAhead
= -1) const;
196 * Advances the position by the given number of 16-bit code units.
197 * This is useful in conjunction with the lookahead() method.
198 * @param count the number of 16-bit code units to jump over
200 void jumpahead(int32_t count
);
203 * Returns a string representation of this object, consisting of the
204 * characters being iterated, with a '|' marking the current position.
205 * Position within an expanded variable is <em>not</em> indicated.
206 * @param result output parameter to receive a string
207 * representation of this object
209 // UnicodeString& toString(UnicodeString& result) const;
213 * Returns the current 32-bit code point without parsing escapes, parsing
214 * variables, or skipping whitespace.
215 * @return the current 32-bit code point
217 UChar32
_current() const;
220 * Advances the position by the given amount.
221 * @param count the number of 16-bit code units to advance past
223 void _advance(int32_t count
);
226 inline UBool
RuleCharacterIterator::inVariable() const {
232 #endif // _RULEITER_H_