diff --git a/util/misc/src/main/java/net/zscript/ascii/TextBox.java b/util/misc/src/main/java/net/zscript/ascii/TextBox.java index c6ec51630..e8d104015 100644 --- a/util/misc/src/main/java/net/zscript/ascii/TextBox.java +++ b/util/misc/src/main/java/net/zscript/ascii/TextBox.java @@ -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 { @@ -133,7 +132,7 @@ public boolean setWidth(int width) { @Override public Iterator iterator() { emptyBuilder(); - return new Iterator() { + return new Iterator<>() { private final Iterator lineIter = lines.iterator(); private int indent = 0; diff --git a/util/misc/src/main/java/net/zscript/util/ByteString.java b/util/misc/src/main/java/net/zscript/util/ByteString.java index 605081adb..bfb3c8be6 100644 --- a/util/misc/src/main/java/net/zscript/util/ByteString.java +++ b/util/misc/src/main/java/net/zscript/util/ByteString.java @@ -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); } /** @@ -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; @@ -629,7 +649,7 @@ public Byte next() { @Override public byte[] nextContiguous() { - return nextContiguous(bytes.length); + return nextContiguous(defaultMaxLength); } @Override diff --git a/util/misc/src/test/java/net/zscript/util/ByteStringTest.java b/util/misc/src/test/java/net/zscript/util/ByteStringTest.java index 32cf4e046..3972784b0 100644 --- a/util/misc/src/test/java/net/zscript/util/ByteStringTest.java +++ b/util/misc/src/test/java/net/zscript/util/ByteStringTest.java @@ -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(); }