1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 * Copyright (C) 1997-2005, International Business Machines Corporation and others. All Rights Reserved. 5 ******************************************************************************* 6 * 7 * File PARSEPOS.H 8 * 9 * Modification History: 10 * 11 * Date Name Description 12 * 07/09/97 helena Converted from java. 13 * 07/17/98 stephen Added errorIndex support. 14 * 05/11/99 stephen Cleaned up. 15 ******************************************************************************* 16 */ 17 18 #ifndef PARSEPOS_H 19 #define PARSEPOS_H 20 21 #include "unicode/utypes.h" 22 #include "unicode/uobject.h" 23 24 25 U_NAMESPACE_BEGIN 26 27 /** 28 * \file 29 * \brief C++ API: Canonical Iterator 30 */ 31 /** 32 * <code>ParsePosition</code> is a simple class used by <code>Format</code> 33 * and its subclasses to keep track of the current position during parsing. 34 * The <code>parseObject</code> method in the various <code>Format</code> 35 * classes requires a <code>ParsePosition</code> object as an argument. 36 * 37 * <p> 38 * By design, as you parse through a string with different formats, 39 * you can use the same <code>ParsePosition</code>, since the index parameter 40 * records the current position. 41 * 42 * The ParsePosition class is not suitable for subclassing. 43 * 44 * @version 1.3 10/30/97 45 * @author Mark Davis, Helena Shih 46 * @see java.text.Format 47 */ 48 49 class U_COMMON_API ParsePosition : public UObject { 50 public: 51 /** 52 * Default constructor, the index starts with 0 as default. 53 * @stable ICU 2.0 54 */ 55 ParsePosition() 56 : UObject(), 57 index(0), 58 errorIndex(-1) 59 {} 60 61 /** 62 * Create a new ParsePosition with the given initial index. 63 * @param newIndex the new text offset. 64 * @stable ICU 2.0 65 */ 66 ParsePosition(int32_t newIndex) 67 : UObject(), 68 index(newIndex), 69 errorIndex(-1) 70 {} 71 72 /** 73 * Copy constructor 74 * @param copy the object to be copied from. 75 * @stable ICU 2.0 76 */ 77 ParsePosition(const ParsePosition& copy) 78 : UObject(copy), 79 index(copy.index), 80 errorIndex(copy.errorIndex) 81 {} 82 83 /** 84 * Destructor 85 * @stable ICU 2.0 86 */ 87 virtual ~ParsePosition(); 88 89 /** 90 * Assignment operator 91 * @stable ICU 2.0 92 */ 93 ParsePosition& operator=(const ParsePosition& copy); 94 95 /** 96 * Equality operator. 97 * @return TRUE if the two parse positions are equal, FALSE otherwise. 98 * @stable ICU 2.0 99 */ 100 UBool operator==(const ParsePosition& that) const; 101 102 /** 103 * Equality operator. 104 * @return TRUE if the two parse positions are not equal, FALSE otherwise. 105 * @stable ICU 2.0 106 */ 107 UBool operator!=(const ParsePosition& that) const; 108 109 /** 110 * Clone this object. 111 * Clones can be used concurrently in multiple threads. 112 * If an error occurs, then NULL is returned. 113 * The caller must delete the clone. 114 * 115 * @return a clone of this object 116 * 117 * @see getDynamicClassID 118 * @stable ICU 2.8 119 */ 120 ParsePosition *clone() const; 121 122 /** 123 * Retrieve the current parse position. On input to a parse method, this 124 * is the index of the character at which parsing will begin; on output, it 125 * is the index of the character following the last character parsed. 126 * @return the current index. 127 * @stable ICU 2.0 128 */ 129 int32_t getIndex(void) const; 130 131 /** 132 * Set the current parse position. 133 * @param index the new index. 134 * @stable ICU 2.0 135 */ 136 void setIndex(int32_t index); 137 138 /** 139 * Set the index at which a parse error occurred. Formatters 140 * should set this before returning an error code from their 141 * parseObject method. The default value is -1 if this is not 142 * set. 143 * @stable ICU 2.0 144 */ 145 void setErrorIndex(int32_t ei); 146 147 /** 148 * Retrieve the index at which an error occurred, or -1 if the 149 * error index has not been set. 150 * @stable ICU 2.0 151 */ 152 int32_t getErrorIndex(void) const; 153 154 /** 155 * ICU "poor man's RTTI", returns a UClassID for this class. 156 * 157 * @stable ICU 2.2 158 */ 159 static UClassID U_EXPORT2 getStaticClassID(); 160 161 /** 162 * ICU "poor man's RTTI", returns a UClassID for the actual class. 163 * 164 * @stable ICU 2.2 165 */ 166 virtual UClassID getDynamicClassID() const; 167 168 private: 169 /** 170 * Input: the place you start parsing. 171 * <br>Output: position where the parse stopped. 172 * This is designed to be used serially, 173 * with each call setting index up for the next one. 174 */ 175 int32_t index; 176 177 /** 178 * The index at which a parse error occurred. 179 */ 180 int32_t errorIndex; 181 182 }; 183 184 inline ParsePosition& 185 ParsePosition::operator=(const ParsePosition& copy) 186 { 187 index = copy.index; 188 errorIndex = copy.errorIndex; 189 return *this; 190 } 191 192 inline UBool 193 ParsePosition::operator==(const ParsePosition& copy) const 194 { 195 if(index != copy.index || errorIndex != copy.errorIndex) 196 return FALSE; 197 else 198 return TRUE; 199 } 200 201 inline UBool 202 ParsePosition::operator!=(const ParsePosition& copy) const 203 { 204 return !operator==(copy); 205 } 206 207 inline int32_t 208 ParsePosition::getIndex() const 209 { 210 return index; 211 } 212 213 inline void 214 ParsePosition::setIndex(int32_t offset) 215 { 216 this->index = offset; 217 } 218 219 inline int32_t 220 ParsePosition::getErrorIndex() const 221 { 222 return errorIndex; 223 } 224 225 inline void 226 ParsePosition::setErrorIndex(int32_t ei) 227 { 228 this->errorIndex = ei; 229 } 230 U_NAMESPACE_END 231 232 #endif 233