Skip to content

Commit

Permalink
[#163] Incorporate Zchars constants more thoroughly
Browse files Browse the repository at this point in the history
  • Loading branch information
susanw1 committed Sep 8, 2024
1 parent 8828f8b commit e7e1935
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Zchars {
return false{{#zchars}}{{#separator}} || b == '{{{chEscapedAsC}}}'{{/separator}}{{/zchars}};
}

public static boolean isNonNumerical(byte b) {
public static boolean isNonNumericalKey(byte b) {
return false{{#zchars}}{{#nonNumeric}} || b == '{{{chEscapedAsC}}}'{{/nonNumeric}}{{/zchars}};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private int appendHexTrim(byte b, byte[] buffer, int startIndex) {
* Convenience method that write the bytes stored in the supplied string.
*/
private void writeEscaped(byte b) {
writeBytes(new byte[] { '=', toHexChar(b >>> 4), toHexChar(b & 0xf) }, 3, false);
writeBytes(new byte[] { Zchars.Z_STRING_ESCAPE, toHexChar(b >>> 4), toHexChar(b & 0xf) }, 3, false);
}

private void writeStringByte(byte b) {
Expand All @@ -73,7 +73,7 @@ private void writeStringByte(byte b) {

@Override
public void endSequence() {
writeCharAsByte('\n');
writeCharAsByte(Zchars.Z_NEWLINE);
}

@Override
Expand All @@ -100,14 +100,14 @@ public void writeField(byte field, int value) {
public void writeField(ZscriptTokenField field) {
if (field.isBigField()) {
if (field.getKey() == Zchars.Z_BIGFIELD_QUOTED) {
writeCharAsByte('"');
writeCharAsByte(Zchars.Z_BIGFIELD_QUOTED);
// note, could use BlockIterator, but need to split and extract escaped chars
for (byte b : field) {
writeStringByte(b);
}
writeCharAsByte('"');
writeCharAsByte(Zchars.Z_BIGFIELD_QUOTED);
} else {
writeCharAsByte('+');
writeCharAsByte(Zchars.Z_BIGFIELD_HEX);
for (BlockIterator iterator = field.iterator(); iterator.hasNext(); ) {
byte[] bytes = iterator.nextContiguous();
writeBytes(bytes, bytes.length, true);
Expand All @@ -125,36 +125,36 @@ public void writeQuotedString(String string) {

@Override
public void writeQuotedString(byte[] data) {
writeCharAsByte('"');
writeCharAsByte(Zchars.Z_BIGFIELD_QUOTED);
for (byte b : data) {
writeStringByte(b);
}
writeCharAsByte('"');
writeCharAsByte(Zchars.Z_BIGFIELD_QUOTED);
}

@Override
public void writeBig(byte[] data) {
writeCharAsByte('+');
writeCharAsByte(Zchars.Z_BIGFIELD_HEX);
writeBytes(data, data.length, true);
}

@Override
public void writeOrElse() {
writeCharAsByte('|');
writeCharAsByte(Zchars.Z_ORELSE);
}

@Override
public void writeAndThen() {
writeCharAsByte('&');
writeCharAsByte(Zchars.Z_ANDTHEN);
}

@Override
public void writeOpenParen() {
writeCharAsByte('(');
writeCharAsByte(Zchars.Z_OPENPAREN);
}

@Override
public void writeCloseParen() {
writeCharAsByte(')');
writeCharAsByte(Zchars.Z_CLOSEPAREN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public interface CommandOutStream {
*/
void writeField(byte field, int value);

/**
* Convenience method to minimize char->byte casting.
*
* @param field the field char
* @param value the 0-0xffff value to write
*/
default void writeField(char field, int value) {
writeField((byte) field, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public boolean status(byte status) {
if (!out.isOpen()) {
out.open();
}
commandOut.writeField('!', 0);
commandOut.writeField(Zchars.Z_RESPONSE_MARK, 0);
commandOut.writeField(Zchars.Z_STATUS, status);
out.endSequence();
out.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.zscript.javareceiver.core.Zscript;
import net.zscript.javareceiver.execution.NotificationContext;
import net.zscript.javareceiver.execution.ZscriptAction;
import net.zscript.model.components.Zchars;

public class ZscriptNotificationAction implements ZscriptAction {
enum NotificationActionType {
Expand Down Expand Up @@ -59,10 +60,10 @@ private void startResponse(SequenceOutStream out) {
}
CommandOutStream commandStream = out.asCommandOutStream();
if (source.isAddressing()) {
commandStream.writeField('!', 0);
commandStream.writeField(Zchars.Z_RESPONSE_MARK, 0);
// commandStream.writeField(Zchars.Z_ADDRESSING, (source.getID() >> 4));
} else {
commandStream.writeField('!', (source.getID() >> 4));
commandStream.writeField(Zchars.Z_RESPONSE_MARK, (source.getID() >> 4));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void startResponse(SequenceOutStream out, byte respType) {
out.open();
}
CommandOutStream commandOutput = out.asCommandOutStream();
commandOutput.writeField('!', respType);
commandOutput.writeField(Zchars.Z_RESPONSE_MARK, respType);
if (parseState.hasEcho()) {
commandOutput.writeField('_', parseState.getEcho());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private void startNewToken(final byte b) {
return;
}

numeric = !Zchars.isNonNumerical(b);
numeric = !Zchars.isNonNumericalKey(b);
isText = false;
if (b == Zchars.Z_COMMENT) {
skipToNL = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ void shouldWriteBufferOverflowOnTokenData() {
assertThat(buffer.getInternalData()).startsWith('+', 3, 0xa0, 0xa1, 0xa2, ERROR_BUFFER_OVERRUN, 2, 0x32, 0x33);
}

@Test
public void shouldPreventOversizeBuffer() {
assertThatThrownBy(() -> {
TokenRingBuffer.createBufferWithCapacity(65537);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("size too big [sz=65537, max=65536]");
}

private void insertNumericToken(char key, byte... data) {
writer.startToken((byte) key, true);
for (byte b : data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void notification(Entity entity, NotificationContext ctx) {
I2cResponse addrRespTmp = (I2cResponse) entity.getConnection(I2cProtocolCategory.class, i).sendMessage(entity, new I2cReceivePacket(SmBusAlertConnection.ALERT_ADDRESS, 2));
if (!addrRespTmp.worked()) {
if (isAddressed) {
commandOut.writeField('S', ZscriptStatus.ADDRESS_NOT_FOUND);
commandOut.writeField(Zchars.Z_STATUS, ZscriptStatus.ADDRESS_NOT_FOUND);
} else {
commandOut.writeField('P', i);
}
Expand Down Expand Up @@ -136,8 +136,8 @@ public void notification(Entity entity, NotificationContext ctx) {
new I2cReceivePacket(new I2cAddress(addr), CHUNK_LENGTH));
if (!respTmp.worked()) {
out.endSequence();
commandOut.writeField('!', 0x50);
commandOut.writeField('S', ZscriptStatus.ADDRESS_NOT_FOUND);
commandOut.writeField(Zchars.Z_RESPONSE_MARK, 0x50);
commandOut.writeField(Zchars.Z_STATUS, ZscriptStatus.ADDRESS_NOT_FOUND);
break;
}
data = ((I2cReceiveResponse) respTmp).getData();
Expand Down

0 comments on commit e7e1935

Please sign in to comment.