Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CharEnumerator.cs « System « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7689a5a3b4ce1e2386295460c20c910dee636d9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// System.CharEnumerator.cs
//
// Author: Duncan Mak  (duncan@ximian.com)
//
// (C) Ximian, Inc.
//

using System.Collections;

namespace System
{
	[Serializable]
	public sealed class CharEnumerator : IEnumerator, ICloneable
	{
		private string str;
		private int index;
		private int length;
		// Representation invariant:
		// length == str.Length
		// -1 <= index <= length
		
		// Constructor
		internal CharEnumerator (string s)
		{
			 str = s;
			 index = -1;
			 length = s.Length;
		}
		
		// Property
		public char Current
		{
			get {
				if (index == -1 || index == length)
					throw new InvalidOperationException
						("The position is not valid.");

				return str [index];
			}
		}
		
		object IEnumerator.Current
		{
			get {
				if (index == -1 || index == length)
					throw new InvalidOperationException
						("The position is not valid");

				return str [index];
			}
		}
		
		// Methods
		public object Clone ()
		{
			CharEnumerator x = new CharEnumerator (str);
			x.index = index;
			return x;
		}
		
		public bool MoveNext ()
		{
			// Representation invariant holds: -1 <= index <= length

			index ++;

			// Now: 0 <= index <= length+1;
			//   <=>
			// 0 <= index < length (OK) || 
			// length <= index <= length+1 (Out of bounds)
			
			if (index >= length) {
				index = length;
				// Invariant restored:
				// length == index
				//   =>
				// -1 <= index <= length
				return false;	
			}
			else
				return true;
		}
		
		public void Reset ()
		{
			index = -1;
		}
	}
}