Skip to content

Commit

Permalink
[#163] Added ByteString.iterator(max), with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
susanw1 committed Sep 12, 2024
1 parent bca99d4 commit 56ebf58
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 1 addition & 2 deletions util/misc/src/main/java/net/zscript/ascii/TextBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.PrimitiveIterator;

public class TextBox implements AsciiFrame {

Expand Down Expand Up @@ -133,7 +132,7 @@ public boolean setWidth(int width) {
@Override
public Iterator<TextRow> iterator() {
emptyBuilder();
return new Iterator<TextRow>() {
return new Iterator<>() {
private final Iterator<TextLine> lineIter = lines.iterator();

private int indent = 0;
Expand Down
26 changes: 23 additions & 3 deletions util/misc/src/main/java/net/zscript/util/ByteString.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,23 @@ public int size() {
}

/**
* Returns a block Iterator over the bytes in the string.
* Returns a block Iterator over the bytes in the string, with the default maximum block size set to the size of this ByteString.
*
* @return iterator a block iterator
*/
@Override
public BlockIterator iterator() {
return new ByteBlockIterator();
return new ByteBlockIterator(bytes.length);
}

/**
* Returns a block Iterator over the bytes in the string, with the specified default maximum block size.
*
* @param defaultMaxLength the max number of bytes to return in a call to {@link BlockIterator#nextContiguous()}
* @return iterator a block iterator
*/
public BlockIterator iterator(int defaultMaxLength) {
return new ByteBlockIterator(defaultMaxLength);
}

/**
Expand Down Expand Up @@ -612,8 +622,18 @@ default ByteString toByteString(T object) {
* Simple array iterator
*/
private class ByteBlockIterator implements BlockIterator {
final int defaultMaxLength;
int index = 0;

/**
* Creates a new iterator.
*
* @param defaultMaxLength the max number of bytes to return in a call to {@link #nextContiguous()}
*/
ByteBlockIterator(int defaultMaxLength) {
this.defaultMaxLength = defaultMaxLength;
}

@Override
public boolean hasNext() {
return index < bytes.length;
Expand All @@ -629,7 +649,7 @@ public Byte next() {

@Override
public byte[] nextContiguous() {
return nextContiguous(bytes.length);
return nextContiguous(defaultMaxLength);
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions util/misc/src/test/java/net/zscript/util/ByteStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ public void shouldIterateBytes() {
@Test
public void shouldIterateBlockBytes() {
var str = ByteString.builder().appendUtf8("hello, London calling, £5 is a fiver").build();
var iter = str.iterator();
var iter = str.iterator(6);

assertThat(iter.hasNext()).isTrue();
assertThat(iter.next()).isEqualTo((byte) 'h');
assertThat(iter.nextContiguous(8)).isEqualTo("ello, Lo".getBytes(ISO_8859_1));
assertThat(iter.nextContiguous(11)).isEqualTo("ndon callin".getBytes(ISO_8859_1));
assertThat(iter.nextContiguous(6)).containsExactly('g', ',', ' ', 0xc2, 0xa3, '5');
assertThat(iter.nextContiguous()).containsExactly('g', ',', ' ', 0xc2, 0xa3, '5');
assertThat(iter.hasNext()).isTrue();
assertThat(iter.nextContiguous()).isEqualTo(" is a fiver".getBytes(ISO_8859_1));
assertThat(iter.nextContiguous()).isEqualTo(" is a ".getBytes(ISO_8859_1));
assertThat(iter.nextContiguous()).isEqualTo("fiver".getBytes(ISO_8859_1));
assertThat(iter.hasNext()).isFalse();
}

Expand Down

0 comments on commit 56ebf58

Please sign in to comment.