Skip to content

Commit

Permalink
add #peekString()
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Oct 1, 2024
1 parent 9ff45e3 commit 901a4a4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import revxrsal.commands.exception.InputParseException;

/**
* A class that aids in parsing a stream of characters.
Expand Down Expand Up @@ -118,14 +119,60 @@ public int position() {
return pos;
}

protected @NotNull String readUnquotedString() {
public @NotNull String readUnquotedString() {
int start = pos;
while (hasRemaining() && !Character.isWhitespace(peek())) {
pos += 1;
}
return source.substring(start, pos);
}

public @NotNull String readString() {
if (!hasRemaining())
return "";
char next = read();
if (next == DOUBLE_QUOTE) {
pos += 1;
return readUntil(DOUBLE_QUOTE);
}
return readUnquotedString();
}

public char read() {
return source.charAt(pos++);
}

public @NotNull String readUntil(char delimiter) {
StringBuilder result = new StringBuilder();
boolean escaped = false;
while (hasRemaining()) {
char c = read();
if (escaped) {
if (c == delimiter || c == ESCAPE) {
result.append(c);
escaped = false;
} else {
pos--;
throw new InputParseException(InputParseException.Cause.INVALID_ESCAPE_CHARACTER);
}
} else if (c == ESCAPE) {
escaped = true;
} else if (c == delimiter) {
return result.toString();
} else {
result.append(c);
}
}
throw new InputParseException(InputParseException.Cause.UNCLOSED_QUOTE);
}

@Override public @NotNull String peekString() {
int cursor = pos;
String value = readString();
pos = cursor;
return value;
}

public @NotNull String peekUnquotedString() {
int cursor = pos;
String value = readUnquotedString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ public final class MutableStringStreamImpl extends BaseStringStream implements M
super(source, position);
}

@Override
public char read() {
return source.charAt(pos++);
}

@Override
public String read(int characters) {
if (!canRead(characters))
Expand Down Expand Up @@ -103,21 +98,6 @@ public void extend(@NotNull String str) {
source += str;
}

public @Override @NotNull String readUnquotedString() {
return super.readUnquotedString();
}

public @NotNull String readString() {
if (!hasRemaining())
return "";
char next = peek();
if (next == DOUBLE_QUOTE) {
moveForward();
return readUntil(DOUBLE_QUOTE);
}
return readUnquotedString();
}

public @NotNull String readUntil(char delimiter) {
StringBuilder result = new StringBuilder();
boolean escaped = false;
Expand Down
12 changes: 12 additions & 0 deletions common/src/main/java/revxrsal/commands/stream/StringStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ public interface StringStream {
@NotNull
String peekUnquotedString();

/**
* Peeks the next string. If the string was quoted, it will
* peek the entire string inside the quotes. Otherwise, it will
* peek the next string until a whitespace character is encountered.
* <p>
* This will not move the cursor forward.
*
* @return The next string.
*/
@NotNull
String peekString();

/**
* Peeks the remainder of the string stream
*
Expand Down

0 comments on commit 901a4a4

Please sign in to comment.