2021-09-24 00:36:29 -05:00
|
|
|
jdk.imports['java.util.Scanner'].load = async () => {
|
2022-04-12 11:32:32 -05:00
|
|
|
const InputStream = await jdk.import('java.io.InputStream');
|
2021-09-23 01:41:44 -05:00
|
|
|
|
|
|
|
class Scanner {
|
|
|
|
constructor(input) {
|
2022-04-12 11:32:32 -05:00
|
|
|
if (input.getAbsolutePath) {
|
|
|
|
this._loading = true;
|
|
|
|
this._filePath = input.getAbsolutePath();
|
|
|
|
return;
|
2021-09-23 01:41:44 -05:00
|
|
|
}
|
|
|
|
this.in = input;
|
|
|
|
}
|
2022-04-12 11:32:32 -05:00
|
|
|
async _loadFile(filePath) {
|
|
|
|
this.in = new InputStream();
|
|
|
|
this.in.stream = await (await fetch(filePath)).text();
|
|
|
|
this._loading = false;
|
|
|
|
}
|
|
|
|
async hasNext(pattern) {
|
|
|
|
if (this._loading) {
|
|
|
|
await this._loadFile(this._filePath);
|
|
|
|
}
|
2021-09-23 01:41:44 -05:00
|
|
|
if (pattern instanceof RegExp) {
|
|
|
|
return pattern.test(this.in.stream.slice(this.in.mark));
|
|
|
|
}
|
|
|
|
// if pattern is string
|
2022-04-12 11:32:32 -05:00
|
|
|
return this.in.stream.slice(this.in.mark).includes(pattern);
|
2021-09-23 01:41:44 -05:00
|
|
|
}
|
2022-04-12 11:32:32 -05:00
|
|
|
async hasNextLine() {
|
2021-09-23 01:41:44 -05:00
|
|
|
return this.hasNext('\n');
|
|
|
|
}
|
|
|
|
async nextLine() {
|
|
|
|
return await this.next(/.*\n/);
|
|
|
|
}
|
|
|
|
async next(pattern) {
|
2022-04-12 11:32:32 -05:00
|
|
|
while (this._loading || !this.hasNext(pattern)) {
|
2021-09-23 01:41:44 -05:00
|
|
|
await new Promise((done) => setTimeout(() => done(), 100));
|
|
|
|
}
|
|
|
|
let buf = this.in.stream.slice(this.in.mark);
|
|
|
|
let substr = buf.match(pattern)[0];
|
|
|
|
let start = buf.indexOf(substr);
|
|
|
|
let end = buf.indexOf('\n');
|
|
|
|
this.in.read(end - start + 1);
|
|
|
|
return buf.slice(start, end);
|
|
|
|
}
|
|
|
|
async nextShort() {
|
|
|
|
return await this.nextInt();
|
|
|
|
}
|
|
|
|
async nextInt() {
|
|
|
|
return Number(await this.next(/\d+\D/));
|
|
|
|
}
|
|
|
|
async nextLong() {
|
|
|
|
return await this.nextInt();
|
|
|
|
}
|
|
|
|
async nextFloat() {
|
|
|
|
return Number(await this.next(/[0-9\.]+[^0-9\.]/));
|
|
|
|
}
|
|
|
|
async nextDouble() {
|
|
|
|
return await this.nextFloat();
|
|
|
|
}
|
|
|
|
close() {}
|
|
|
|
}
|
2021-09-23 23:26:42 -05:00
|
|
|
jdk.java.util.Scanner = Scanner;
|
2021-09-23 01:41:44 -05:00
|
|
|
};
|