-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSourceText.cs
More file actions
51 lines (42 loc) · 1.16 KB
/
Copy pathSourceText.cs
File metadata and controls
51 lines (42 loc) · 1.16 KB
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
using System.Collections;
using System.Collections.Generic;
namespace StringMath
{
internal sealed class SourceText : IEnumerator<char>
{
public string Text { get; }
public int Position { get; set; }
public char Current => Text[Position];
object IEnumerator.Current => Current;
// The string terminator is used by the tokenizer to produce EndOfCode tokens
public SourceText(string source)
=> Text = $"{source}\0";
public bool MoveNext()
{
if (Position + 1 < Text.Length)
{
Position++;
return true;
}
return false;
}
public char Peek(int count = 1)
{
int location = Position + count;
char result = location < Text.Length && location >= 0 ? Text[location] : '\0';
return result;
}
public void Reset()
{
Position = 0;
}
public void Dispose()
{
Reset();
}
public override string ToString()
{
return $"{Current} :{Position}";
}
}
}