-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.js
172 lines (152 loc) · 4.4 KB
/
reader.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
export class Reader{
constructor(txt = ""){
this.i = 0
this.lookahead_index = 0
this.text = txt
this.char = this.text[0]
}
initializeText(txt){
this.text = txt
this.i = 0
this.lookahead_index = 0
this.char = this.text[0]
}
read(){
if(this.i >= this.text.length){
return undefined
}else{
this.char = this.text[this.i]
return this.text[this.i]
}
}
readnext(){
this.i++
this.char = this.text[this.i]
if(this.i >= this.text.length){
return undefined
}else{
return this.text[this.i]
}
}
moveHeadForward(movment = 1){
this.i = this.i + movment
if(this.i > this.text.length){
this.i = this.text.length - 1
}
this.char = this.text[this.i]
}
moveHeadTo(i){
if(i > this.text.length){
i = this.text.length - 1
}else if(i < 0){
i = 0
}
this.i = i
this.char = this.text[this.i]
}
moveHeadBackward(movment = 1){
this.i = this.i - movment
if(this.i < 0){
this.i = 0
}
this.char = this.text[this.i]
}
getSubstring(start, end){
return this.text.substring(start, end+1)
}
getSubstringToChar(char){
let text = ""
for(let i = this.i; i < this.text.length; i++){
text = text + this.text[i]
if(this.text[i] === char){
return text
}
}
return undefined
}
peak(steps_ahead = 1){
return this.text[this.i + steps_ahead]
}
getEncapsulation(open_char, close_char, with_encap = false){
let encap = this.isEncapsulating(open_char, close_char)
if(encap !== false){
let new_array = null
if(with_encap){
new_array = this.text.slice(encap.open, encap.close + 1)
}else{
new_array = this.text.slice(encap.open + 1, encap.close)
}
this.moveHeadTo(encap.close)
return new_array
}else{
return false
}
}
getSuperGreedyEncapsulation(open_char, close_char, with_encap = false){
let encap = this.isSuperGreedyEncapsulation(open_char, close_char)
if(encap !== false){
let new_array = null
if(with_encap){
new_array = this.text.slice(encap.open, encap.close + 1)
}else{
new_array = this.text.slice(encap.open + 1, encap.close)
}
this.moveHeadTo(encap.close)
return new_array
}else{
return false
}
}
isSuperGreedyEncapsulation(open_char, close_char){
let opened_at = null
let did_open = false
loop:
for(let i = this.i; i < this.text.length; i++){
if(did_open && this.text[i] !== close_char && this.text[i] !== open_char){
continue
}else if(this.text[i] === open_char && !did_open){
opened_at = i
did_open = true
}else if(this.text[i] === close_char && did_open){
return {open: opened_at, close: i}
}else{
return false
}
}
return false
}
isEncapsulating(open_char, close_char){
let opened_at = null
let closed_at = null
let unclosed = 0
let didOpen = false
loop:
for(let i = this.i; i < this.text.length; i++){
switch(this.text[i]){
case(open_char):{
unclosed = unclosed + 1
if(!didOpen){
opened_at = i
didOpen = true
}
break;
}
case(close_char):{
if(unclosed > 0){
unclosed = unclosed - 1
closed_at = i
if(unclosed === 0){
break loop
}
}
break
}
}
}
if(didOpen && unclosed === 0){
return {open: opened_at, close: closed_at}
}else{
return false
}
}
}