blob: 7145ce8f7b333f49e799cd040554d6ca7db43c82 (
plain) (
blame)
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
91
|
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace JSCompiler.Core
{
public class BaseInputStream : IInputStream
{
private char[][] buffs;
private int index;
private int activeBuffer;
private StreamReader reader;
public BaseInputStream(Stream stream, int buffLength = 4096)
{
this.Line = 0;
this.index = 0;
this.activeBuffer = 0;
this.buffs = new char[2][];
this.buffs[0] = new char[buffLength];
this.buffs[1] = new char[buffLength];
this.reader = new StreamReader(stream);
ReadStream(buffs[0], buffLength);
}
public BaseInputStream(string path, int buffLength = 4096)
: this(new FileStream(path, FileMode.Open), buffLength) { }
public int Line { get; private set; }
public void Close()
{
reader.Close();
}
public bool IsEndOfStream()
{
return Peek() == 0;
}
public char Next()
{
if (IsEndOfStream()) throw new EndOfStreamException();
char cur = Peek();
if (cur == '\n')
{
Line++;
}
index++;
if (index >= buffs[activeBuffer].Length)
{
SwitchBuffers();
}
return cur;
}
public char Peek()
{
return buffs[activeBuffer][index];
}
private void SwitchBuffers()
{
index = 0;
activeBuffer = activeBuffer == 0 ? 1 : 0;
ReadStream(buffs[activeBuffer], buffs[activeBuffer].Length);
}
private int ReadStream(char[] buffer, int length)
{
int bytesRead = reader.Read(buffer, 0, length);
if (bytesRead < length)
{
buffer[bytesRead] = (char) 0;
}
return bytesRead;
}
}
}
|