blob: d02808cff558ff7d530e09a0769c273ca36134e1 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2014, 2021 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ecommons.text;
import org.eclipse.jface.text.IDocument;
import org.eclipse.statet.ecommons.text.core.rules.BufferedDocumentScanner;
public class CharacterScannerReader {
private final BufferedDocumentScanner scanner;
private int offset;
public CharacterScannerReader(final BufferedDocumentScanner scanner) {
this.scanner= scanner;
}
public final BufferedDocumentScanner getScanner() {
return this.scanner;
}
public final int read() {
final int c= this.scanner.read();
if (c >= 0) {
this.offset++;
}
return c;
}
public final boolean read(final char c1) {
final int c= this.scanner.read();
if (c == c1) {
this.offset++;
return true;
}
if (c >= 0) {
this.scanner.unread();
}
return false;
}
public final boolean read(final char c1, final char c2) {
int c= this.scanner.read();
if (c == c1) {
c= this.scanner.read();
if (c == c2) {
this.offset+= 2;
return true;
}
this.scanner.unread();
}
if (c >= 0) {
this.scanner.unread();
}
return false;
}
public final boolean read(final char c1, final char c2, final char c3) {
int c= this.scanner.read();
if (c == c1) {
c= this.scanner.read();
if (c == c2) {
c= this.scanner.read();
if (c == c3) {
this.offset+= 3;
return true;
}
this.scanner.unread();
}
this.scanner.unread();
}
if (c >= 0) {
this.scanner.unread();
}
return false;
}
public final boolean read2(final char[] cSeq) {
for (int i= 1; i < cSeq.length; i++) {
final int c= this.scanner.read();
if (c != cSeq[i]) {
unreadRaw((c >= 0) ? i : (i - 1));
return false;
}
}
this.offset+= cSeq.length - 1;
return true;
}
public final boolean readConsuming(final char c1, final char c2) {
int c= this.scanner.read();
if (c == c1) {
c= this.scanner.read();
if (c == c2) {
this.offset+= 2;
return true;
}
}
if (c >= 0) {
this.scanner.unread();
}
return false;
}
public final boolean readConsuming(final char[] seq) {
final int n= seq.length;
for (int i= 0; i < n; i++) {
final int c= this.scanner.read();
if (c != seq[i]) {
this.offset+= i;
if (c >= 0) {
this.scanner.unread();
}
return false;
}
}
this.offset+= n;
return true;
}
public final boolean readConsuming2(final char[] cSeq) {
final int n= cSeq.length;
for (int i= 1; i < n; i++) {
final int c= this.scanner.read();
if (c != cSeq[i]) {
this.offset+= i - 1;
if (c >= 0) {
this.scanner.unread();
}
return false;
}
}
this.offset+= n-1;
return true;
}
public final int readConsumingWhitespace() {
int readed= 0;
while (true) {
final int c= this.scanner.read();
if (c != ' ' && c != '\t') {
if (c >= 0) {
this.scanner.unread();
}
this.offset+= readed;
return readed;
}
readed++;
}
}
public final boolean readTemp(final char c1) {
final int c= this.scanner.read();
if (c >= 0) {
this.scanner.unread();
}
return (c == c1);
}
public final boolean readTemp(final char c1, final char c2) {
int c= this.scanner.read();
if (c == c1) {
c= this.scanner.read();
if (c == c2) {
return true;
}
this.scanner.unread();
}
if (c >= 0) {
this.scanner.unread();
}
return false;
}
public final void unread() {
this.offset--;
this.scanner.unread();
}
public final void unread(int count) {
this.offset-= count;
while (count-- > 0) {
this.scanner.unread();
}
}
public final int readRaw() {
return this.scanner.read();
}
public final void unreadRaw(int count) {
while (count-- > 0) {
this.scanner.unread();
}
}
public int getOffset() {
return this.offset;
}
public void setRange(final IDocument document, final int startOffset, final int length) {
this.scanner.setRange(document, startOffset, length);
this.offset= startOffset;
}
}