Skip to content

Commit

Permalink
Some improvements for ACell.toString() implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 20, 2024
1 parent 2fe2fb6 commit 121e333
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 12 deletions.
5 changes: 0 additions & 5 deletions convex-core/src/main/java/convex/core/data/ABlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,6 @@ public int compareTo(ABlobLike<?> b) {
*/
public abstract Blob getChunk(long i);

/**
* Prints this Blob in a readable Hex representation, typically in the format "0x01abcd...."
*
* Subclasses may override this if they require a different representation.
*/
@Override
public boolean print(BlobBuilder bb, long limit) {
bb.append(Strings.HEX_PREFIX);
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/AString.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public int getBytes(byte[] dest, int destOffset) {
}

@Override
public final String toString() {
public String toString() {
int n=Utils.checkedInt(count());
ByteBuffer bb=toBlob().toByteBuffer();

Expand Down
7 changes: 6 additions & 1 deletion convex-core/src/main/java/convex/core/data/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ public boolean print(BlobBuilder sb, long limit) {
@Override
public AString toCVMString(long limit) {
if (limit<2) return null;
return Strings.create("#"+value);
return Strings.create(toString());
}

@Override
public String toString() {
return "#"+value;
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions convex-core/src/main/java/convex/core/data/StringShort.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,12 @@ public boolean equalsBytes(ABlob b) {
public long longValue() {
return data.longValue();
}

@Override
public String toString() {
byte [] bytes=data.getInternalArray();
if (bytes.length!=data.count()) bytes=data.getBytes(); // need a copy if not fully packed
return new String(bytes,StandardCharsets.UTF_8);
}

}
5 changes: 3 additions & 2 deletions convex-core/src/main/java/convex/core/data/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.charset.StandardCharsets;

import convex.core.Constants;
import convex.core.data.prim.CVMBool;
import convex.core.data.prim.CVMChar;
import convex.core.data.util.BlobBuilder;
import convex.core.exceptions.BadFormatException;
Expand All @@ -20,8 +21,8 @@ public class Strings {
public static final Ref<StringShort> EMPTY_REF = EMPTY.getRef();

public static final StringShort NIL = StringShort.create("nil");
public static final StringShort TRUE = StringShort.create("true");
public static final StringShort FALSE = StringShort.create("false");
public static final StringShort TRUE = StringShort.create(CVMBool.TRUE_STRING);
public static final StringShort FALSE = StringShort.create(CVMBool.FALSE_STRING);

public static final StringShort BAD_SIGNATURE = StringShort.create("Bad Signature!");
public static final StringShort BAD_FORMAT = StringShort.create("Bad Message Format!");
Expand Down
11 changes: 10 additions & 1 deletion convex-core/src/main/java/convex/core/data/prim/CVMBool.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public final class CVMBool extends APrimitive {
// Salted values for hashcodes
private static final int TRUE_HASHCODE = (int)Bits.SALT;
private static final int FALSE_HASHCODE = (int)(Bits.SALT>>32);


// Java String values
public static final String TRUE_STRING = "true";
public static final String FALSE_STRING = "false";

private CVMBool(boolean value) {
this.value=value;
}
Expand Down Expand Up @@ -127,5 +131,10 @@ public Blob toBlob() {
public ACell not() {
return value?FALSE:TRUE;
}

@Override
public String toString() {
return value?TRUE_STRING:FALSE_STRING;
}

}
3 changes: 1 addition & 2 deletions convex-core/src/main/java/convex/core/lang/RT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,7 @@ public static String toString(ACell a) {
* @return Java String representation. May be "nil". May include message if print limit exceeded
*/
public static String toString(ACell a, long limit) {
if (a==null) return "nil";
return a.print(limit).toString();
return RT.print(a,limit).toString();
}

/**
Expand Down

0 comments on commit 121e333

Please sign in to comment.