parsepos.h 5.6 KB

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