1 /*
2  * hunt-time: A time library for D programming language.
3  *
4  * Copyright (C) 2015-2018 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.time.text.ParsePosition;
13 
14 import std.conv;
15 /**
16  * <code>ParsePosition</code> is a simple class used by <code>Format</code>
17  * and its subclasses to keep track of the current position during parsing.
18  * The <code>parseObject</code> method in the various <code>Format</code>
19  * classes requires a <code>ParsePosition</code> object as an argument.
20  *
21  * <p>
22  * By design, as you parse through a string with different formats,
23  * you can use the same <code>ParsePosition</code>, since the index parameter
24  * records the current position.
25  *
26  * @author      Mark Davis
27  * @since 1.1
28  * @see         java.text.Format
29  */
30 
31 public class ParsePosition {
32 
33     /**
34      * Input: the place you start parsing.
35      * <br>Output: position where the parse stopped.
36      * This is designed to be used serially,
37      * with each call setting index up for the next one.
38      */
39     int index = 0;
40     int errorIndex = -1;
41 
42     /**
43      * Retrieve the current parse position.  On input to a parse method, this
44      * is the index of the character at which parsing will begin; on output, it
45      * is the index of the character following the last character parsed.
46      *
47      * @return the current parse position
48      */
49     public int getIndex() {
50         return index;
51     }
52 
53     /**
54      * Set the current parse position.
55      *
56      * @param index the current parse position
57      */
58     public void setIndex(int index) {
59         this.index = index;
60     }
61 
62     /**
63      * Create a new ParsePosition with the given initial index.
64      *
65      * @param index initial index
66      */
67     public this(int index) {
68         this.index = index;
69     }
70     /**
71      * Set the index at which a parse error occurred.  Formatters
72      * should set this before returning an error code from their
73      * parseObject method.  The default value is -1 if this is not set.
74      *
75      * @param ei the index at which an error occurred
76      * @since 1.2
77      */
78     public void setErrorIndex(int ei)
79     {
80         errorIndex = ei;
81     }
82 
83     /**
84      * Retrieve the index at which an error occurred, or -1 if the
85      * error index has not been set.
86      *
87      * @return the index at which an error occurred
88      * @since 1.2
89      */
90     public int getErrorIndex()
91     {
92         return errorIndex;
93     }
94 
95     /**
96      * Overrides equals
97      */
98      override
99     public bool opEquals(Object obj)
100     {
101         if (obj is null) return false;
102         if (!(cast(ParsePosition)obj !is null))
103             return false;
104         ParsePosition other = cast(ParsePosition) obj;
105         return (index == other.index && errorIndex == other.errorIndex);
106     }
107 
108     /**
109      * Returns a hash code for this ParsePosition.
110      * @return a hash code value for this object
111      */
112     override
113     public size_t toHash() @trusted nothrow {
114         return (errorIndex << 16) | index;
115     }
116 
117     /**
118      * Return a string representation of this ParsePosition.
119      * @return  a string representation of this object
120      */
121     override
122     public string toString() {
123         return typeof(this).stringof ~
124             "[index=" ~ index.to!string ~
125             ",errorIndex=" ~ errorIndex.to!string ~ ']';
126     }
127 }