2002-03-05 Duncan Mak <duncan@ximian.com>
[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                 
20                 // Constructor
21                 internal CharEnumerator (string s)
22                 {
23                          str = s;
24                          index = -1;
25                          length = s.Length;
26                 }
27                 
28                 // Property
29                 public char Current
30                 {
31                         get {
32                                 if (index == -1)
33                                         throw new InvalidOperationException
34                                                 ("The position is not valid.");
35
36                                 return str [index];
37                         }
38                 }
39                 
40                 object IEnumerator.Current
41                 {
42                         get {
43                                 if (index == -1)
44                                         throw new InvalidOperationException
45                                                 ("The position is not valid");
46
47                                 return str [index];
48                         }
49                 }
50                 
51                 // Methods
52                 public object Clone ()
53                 {
54                         CharEnumerator x = new CharEnumerator (str);
55                         x.index = index;
56                         return x;
57                 }
58                 
59                 public bool MoveNext ()
60                 {
61                         if (length == 0)
62                                 return false;                   
63
64                         index ++;
65                         
66                         if (index == length)                            
67                                 return false;
68                         else
69                                 return true;
70                 }
71                 
72                 public void Reset ()
73                 {
74                         index = -1;
75                 }
76         }
77 }