Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
- drop IField.decodeValue()
  - use IField.asString() (equivalent to IField.toString())
- drop Model.writeBytes(Bytes)
  - use Model.writeBytes(Bytes.bytes())
- Transductor loops on matchMode
- add .data to .gitignore

Signed-off-by: jrte <jrte.project@gmail.com>
  • Loading branch information
jrte committed Sep 22, 2023
1 parent dd2881e commit 263ba30
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ java_pid*
*.dfa
*.pr
*.last
.data/
.tmp/
.settings/
.vscode/
Expand Down
4 changes: 2 additions & 2 deletions patterns/test/ValuesTest.inr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ValuesTest = (
| ((black - {digit,dash,dot,query}), paste) (black, paste) (space, string paste out clear)
| (query, signal[`!pass`])
)
(pass, out[` ` `!!pass`])?
(pass, out[` ` `!!pass; `])?
)*
(dash, paste)? (digit, paste)*
(
Expand All @@ -17,7 +17,7 @@ ValuesTest = (
| ((black - {digit,dash,dot,query}), paste) (black, paste) (nl|eos, string out in[nl])
| (query, signal[`!pass`])
)
(pass, out[` ` `!!pass`])?
(pass, out[` ` `!!pass; `])?
(nl, out[nl])
):dfamin;

Expand Down
39 changes: 17 additions & 22 deletions src/com/characterforming/jrte/engine/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package com.characterforming.jrte.engine;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.util.Arrays;

import com.characterforming.ribose.IField;
Expand Down Expand Up @@ -81,23 +80,19 @@ public byte[] copyValue() {
return Arrays.copyOf(this.data, this.length);
}

@Override // @see com.characterforming.ribose.IField#decodeValue()
public char[] decodeValue() {
char[] chars = null;
ByteBuffer in = ByteBuffer.wrap(this.data, 0, this.getLength());
CharBuffer out = CharBuffer.allocate(this.getLength());
CoderResult result = this.decoder.reset().decode(in, out, true);
assert !result.isOverflow() && !result.isError();
if (!result.isOverflow() && !result.isError()) {
chars = new char[out.flip().length()];
out.get(chars);
}
return chars;
}

@Override // @see com.characterforming.ribose.IField#asString()
public String asString() {
return new String(this.decodeValue());
try {
return this.decoder.reset().decode(
ByteBuffer.wrap(this.data, 0, this.length)
).flip().toString();
} catch (CharacterCodingException e) {
char[] chars = new char[this.length];
for (int i = 0; i < chars.length; i++) {
chars[i] = (char)(0xff & this.data[i]);
}
return new String(chars);
}
}

@Override // @see com.characterforming.ribose.IField#asInteger()
Expand Down Expand Up @@ -137,6 +132,11 @@ public double asReal() {
return fraction * value;
}

@Override // @see java.lang.Object#toString()
public String toString() {
return this.asString();
}

void clear() {
this.length = 0;
}
Expand Down Expand Up @@ -173,9 +173,4 @@ private void growValue(int size) {
this.data = v;
}
}

@Override // @see java.lang.Object#toString()
public String toString() {
return Bytes.decode(this.decoder, this.data, this.length).toString();
}
}
11 changes: 2 additions & 9 deletions src/com/characterforming/jrte/engine/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,9 @@ protected void writeBytes(final byte[] bytes) throws ModelException {
}
}

protected void writeBytes(final Bytes bytes) throws ModelException {
if (bytes != null) {
this.writeBytes(bytes.bytes());
}
}

protected void writeBytes(final ByteBuffer byteBuffer) throws ModelException {
byte[] bytes = null;
if (byteBuffer != null) {
bytes = new byte[byteBuffer.limit() - byteBuffer.position()];
byte[] bytes = new byte[byteBuffer.limit() - byteBuffer.position()];
byteBuffer.get(bytes, byteBuffer.position(), byteBuffer.limit());
this.writeBytes(bytes);
}
Expand Down Expand Up @@ -657,7 +650,7 @@ protected void writeIntArray(final int[] ints) throws ModelException {
}

protected void writeString(final String s) throws ModelException {
this.writeBytes(Bytes.encode(this.getEncoder(), s));
this.writeBytes(Bytes.encode(this.getEncoder(), s).bytes());
}

protected void writeTransitionMatrix(final int[][][] matrix) throws ModelException {
Expand Down
2 changes: 1 addition & 1 deletion src/com/characterforming/jrte/engine/ModelCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ private boolean save(byte[][][][] compiledParameters) {
this.writeOrdinalMap(transducerOrdinalMap, 0);
for (int index = 0; index < transducerCount; index++) {
if (this.transducerOffsetIndex[index] > 0) {
this.writeBytes(this.transducerNameIndex[index]);
this.writeBytes(this.transducerNameIndex[index].bytes());
this.writeLong(this.transducerOffsetIndex[index]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/characterforming/jrte/engine/Transductor.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public ITransductor run() throws EffectorException, DomainErrorException {
}

// absorb self-referencing (msum,mscan) or sequential (mproduct) transitions with nil effect
if (this.matchMode != MATCH_NONE && token < SIGNUL) {
while (this.matchMode != MATCH_NONE && token < SIGNUL) {
switch (this.matchMode) {
case MATCH_SUM:
token = sumTrap(input, token);
Expand Down
7 changes: 0 additions & 7 deletions src/com/characterforming/ribose/IField.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ public interface IField {
*/
int getLength();

/**
* Decode a UTF-8 encoded value using the default charset.
*
* @return a Unicode char[] array holding the decoded value
*/
char[] decodeValue();

/**
* Get a copy of the value, trimmed to actual length.
*
Expand Down

0 comments on commit 263ba30

Please sign in to comment.