Skip to content

Commit

Permalink
Implemented first version of the asReader interface on IString
Browse files Browse the repository at this point in the history
  • Loading branch information
DavyLandman committed Sep 19, 2024
1 parent 38556b4 commit 16073c5
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/io/usethesource/vallang/IString.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package io.usethesource.vallang;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.PrimitiveIterator.OfInt;

Expand Down Expand Up @@ -99,6 +100,13 @@ default int getMatchFingerprint() {
*/
public void write(Writer w) throws IOException;

/**
* Generates a reader that can be used to stream the contents of the string
* Note, this will generate java characters, users are responsible for dealing with surrogate-pairs.
* See {@link #iterator()} for a more unicode compatible approach to iterate over the characters of an IString.
*/
public Reader asReader();

/**
* Build an iterator which generates the Unicode UTF-32 codepoints of the IString one-by-one.
* @see Character for more information on Unicode UTF-32 codepoints.
Expand Down
124 changes: 124 additions & 0 deletions src/main/java/io/usethesource/vallang/impl/primitive/StringValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package io.usethesource.vallang.impl.primitive;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.management.ManagementFactory;
Expand Down Expand Up @@ -279,6 +281,11 @@ public boolean isNewlineTerminated() {
public IString concat(IString other) {
return other;
}

@Override
public Reader asReader() {
return Reader.nullReader();
}
}

private static class FullUnicodeString extends AbstractString {
Expand Down Expand Up @@ -501,6 +508,11 @@ public void write(Writer w) throws IOException {
w.write(value);
}

@Override
public Reader asReader() {
return new StringReader(value);
}

@Override
public void indentedWrite(Writer w, Deque<IString> whitespace, boolean indentFirstLine) throws IOException {
if (value.isEmpty()) {
Expand Down Expand Up @@ -633,6 +645,11 @@ public int nextInt() {
}
};
}

@Override
public Reader asReader() {
return new StringReader(value);
}
}

/**
Expand Down Expand Up @@ -1146,6 +1163,47 @@ public void write(Writer w) throws IOException {
right.write(w);
}

@Override
public Reader asReader() {
return new Reader() {
private Reader currentReader = left.asReader();
private boolean readingRight = false;

private void continueRight() throws IOException {
assert !readingRight;
currentReader.close();
currentReader = right.asReader();
readingRight = true;
}


@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int result = currentReader.read(cbuf, off, len);
if (result == -1 && !readingRight) {
continueRight();
return read(cbuf, off, len);
}
return result;
}

@Override
public int read() throws IOException {
int result = currentReader.read();
if (result == -1 && !readingRight) {
continueRight();
return read();
}
return result;
}

@Override
public void close() throws IOException {
currentReader.close();
}
};
}

@Override
public void indentedWrite(Writer w, Deque<IString> whitespace, boolean indentFirstLine) throws IOException {
left.indentedWrite(w, whitespace, indentFirstLine);
Expand Down Expand Up @@ -1380,6 +1438,72 @@ public void write(Writer w) throws IOException {
assert indents.isEmpty();
}

@Override
public Reader asReader() {
return new Reader() {
private final OfInt chars = iterator();
private char endedWithHalfSurrogate = 0;

@Override
public int read(char[] cbuf, int off, int len) throws IOException {
if (off < 0 || len < 0 || len > (cbuf.length - off)) {
throw new IndexOutOfBoundsException();
}
if (len == 0) {
return 0;
}

int pos = off;
if (endedWithHalfSurrogate != 0) {
cbuf[pos++] = endedWithHalfSurrogate;
endedWithHalfSurrogate = 0;
}
int endPos = off + len;
while (pos <= endPos) {
if (!chars.hasNext()) {
break;
}
int nextChar = chars.nextInt();
if (Character.isBmpCodePoint(nextChar)) {
cbuf[pos++] = (char)nextChar;
} else {
cbuf[pos++] = Character.highSurrogate(nextChar);
char lowSide = Character.lowSurrogate(nextChar);
if (pos <= endPos) {
cbuf[pos++] = lowSide;
} else {
endedWithHalfSurrogate = lowSide;
}
}
}
int written = pos - off;
return written == 0 ? /* EOF */ -1 : written;
}

@Override
public int read() throws IOException {
if (endedWithHalfSurrogate != 0) {
int result = endedWithHalfSurrogate;
endedWithHalfSurrogate = 0;
return result;
}
if (chars.hasNext()) {
int nextChar = chars.nextInt();
if (Character.isBmpCodePoint(nextChar)) {
return nextChar;
} else {
endedWithHalfSurrogate = Character.lowSurrogate(nextChar);
return Character.highSurrogate(nextChar);
}
}
return -1;
}
@Override
public void close() throws IOException {
}
};
}

@Override
public void indentedWrite(Writer w, Deque<IString> whitespace, boolean indentFirstLine) throws IOException {
if (flattened != null) {
Expand Down

0 comments on commit 16073c5

Please sign in to comment.