parsepos.h 5.5 KB

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