CharEnumerator: stronger test, new failures, fixed them
[mono.git] / mcs / class / corlib / System / CharEnumerator.cs
1 //
2 // System.CharEnumerator.cs
3 //
4 // Author: Duncan Mak  (duncan@ximian.com)
5 //
6 // (C) Ximian, Inc.
7 //
8
9 using System.Collections;
10
11 namespace System
12 {
13         [Serializable]
14         public sealed class CharEnumerator : IEnumerator, ICloneable
15         {
16                 private string str;
17                 private int index;
18                 private int length;
19                 // Representation invariant:
20                 // length == str.Length
21                 // -1 <= index <= length
22                 
23                 // Constructor
24                 internal CharEnumerator (string s)
25                 {
26                          str = s;
27                          index = -1;
28                          length = s.Length;
29                 }
30                 
31                 // Property
32                 public char Current
33                 {
34                         get {
35                                 if (index == -1 || index == length)
36                                         throw new InvalidOperationException
37                                                 ("The position is not valid.");
38
39                                 return str [index];
40                         }
41                 }
42                 
43                 object IEnumerator.Current
44                 {
45                         get {
46                                 if (index == -1 || index == length)
47                                         throw new InvalidOperationException
48                                                 ("The position is not valid");
49
50                                 return str [index];
51                         }
52                 }
53                 
54                 // Methods
55                 public object Clone ()
56                 {
57                         CharEnumerator x = new CharEnumerator (str);
58                         x.index = index;
59                         return x;
60                 }
61                 
62                 public bool MoveNext ()
63                 {
64                         // Representation invariant holds: -1 <= index <= length
65
66                         index ++;
67
68                         // Now: 0 <= index <= length+1;
69                         //   <=>
70                         // 0 <= index < length (OK) || 
71                         // length <= index <= length+1 (Out of bounds)
72                         
73                         if (index >= length) {
74                                 index = length;
75                                 // Invariant restored:
76                                 // length == index
77                                 //   =>
78                                 // -1 <= index <= length
79                                 return false;   
80                         }
81                         else
82                                 return true;
83                 }
84                 
85                 public void Reset ()
86                 {
87                         index = -1;
88                 }
89         }
90 }