diff --git a/convex-core/src/main/java/convex/core/data/Format.java b/convex-core/src/main/java/convex/core/data/Format.java index 352746aea..b91d505c6 100644 --- a/convex-core/src/main/java/convex/core/data/Format.java +++ b/convex-core/src/main/java/convex/core/data/Format.java @@ -466,7 +466,7 @@ public static ByteBuffer writeLength(ByteBuffer bb, int i) { * @param <T> Type of referenced value * @param b Blob containing a ref to read * @param pos Position to read Ref from (should point to tag) - * @return Ref as read from ByteBuffer + * @return Ref as read from Blob at the specified position * @throws BadFormatException If the data is badly formatted, or a non-embedded * value is found where it should be a Ref. */ @@ -476,10 +476,10 @@ public static <T extends ACell> Ref<T> readRef(Blob b,int pos) throws BadFormatE if (tag==Tag.NULL) return Ref.nil(); - // Looks like an embedded cell, so try to read this + // We now expect a non-null embedded cell T cell= Format.read(tag,b,pos); - if (!Format.isEmbedded(cell)) throw new BadFormatException("Non-embedded Cell found instead of ref: type = " +RT.getType(cell)); - return Ref.get(cell); + if (!cell.isEmbedded()) throw new BadFormatException("Non-embedded cell found instead of ref: type = " +RT.getType(cell)); + return cell.getRef(); } @SuppressWarnings("unchecked") @@ -529,7 +529,7 @@ private static ACell readCode(byte tag, Blob b, int pos) throws BadFormatExcepti public static <T extends ACell> T read(Blob blob) throws BadFormatException { long n=blob.count(); if (n<1) throw new BadFormatException("Attempt to decode from empty Blob"); - byte tag = blob.byteAt(0); + byte tag = blob.byteAtUnchecked(0); T result= read(tag,blob,0); if (result==null) { if (n!=1) throw new BadFormatException("Decode of nil value but blob size = "+n); diff --git a/convex-core/src/main/java/convex/core/data/Index.java b/convex-core/src/main/java/convex/core/data/Index.java index 39ed181c8..ebae4e1c6 100644 --- a/convex-core/src/main/java/convex/core/data/Index.java +++ b/convex-core/src/main/java/convex/core/data/Index.java @@ -593,7 +593,7 @@ public static <K extends ABlobLike<?>, V extends ACell> Index<K, V> read(Blob b, epos+=kr.getEncodingLength(); Ref<V> vr=Format.readRef(b,epos); epos+=vr.getEncodingLength(); - me=MapEntry.createRef(kr, vr); + me=MapEntry.fromRefs(kr, vr); if (count == 1) { // single entry map, doesn't need separate depth encoding diff --git a/convex-core/src/main/java/convex/core/data/MapEntry.java b/convex-core/src/main/java/convex/core/data/MapEntry.java index 38338a816..92d6d2e72 100644 --- a/convex-core/src/main/java/convex/core/data/MapEntry.java +++ b/convex-core/src/main/java/convex/core/data/MapEntry.java @@ -43,7 +43,7 @@ public AType getType() { } @SuppressWarnings("unchecked") - public static <K extends ACell, V extends ACell> MapEntry<K, V> createRef(Ref<? extends K> keyRef, Ref<? extends V> valueRef) { + public static <K extends ACell, V extends ACell> MapEntry<K, V> fromRefs(Ref<? extends K> keyRef, Ref<? extends V> valueRef) { // ensure we have a hash at least return new MapEntry<K, V>((Ref<K>) keyRef, (Ref<V>) valueRef); } @@ -57,7 +57,7 @@ public static <K extends ACell, V extends ACell> MapEntry<K, V> createRef(Ref<? * @return New MapEntry instance */ public static <K extends ACell, V extends ACell> MapEntry<K, V> create(K key, V value) { - return createRef(Ref.get(key), Ref.get(value)); + return fromRefs(Ref.get(key), Ref.get(value)); } /** @@ -75,7 +75,7 @@ public static <K extends ACell, V extends ACell> MapEntry<K, V> of(Object key, O @SuppressWarnings({ "rawtypes", "unchecked" }) public static MapEntry convertOrNull(AVector v) { if (v.count()!=2) return null; - return createRef(v.getElementRef(0),v.getElementRef(1)); + return fromRefs(v.getElementRef(0),v.getElementRef(1)); } @Override diff --git a/convex-core/src/main/java/convex/core/data/MapLeaf.java b/convex-core/src/main/java/convex/core/data/MapLeaf.java index 79bbaf8f7..c4f0bebe7 100644 --- a/convex-core/src/main/java/convex/core/data/MapLeaf.java +++ b/convex-core/src/main/java/convex/core/data/MapLeaf.java @@ -349,7 +349,7 @@ public static <K extends ACell, V extends ACell> MapLeaf<K, V> read(Blob b, int epos+=kr.getEncodingLength(); Ref<V> vr=Format.readRef(b,epos); epos+=vr.getEncodingLength(); - items[i] = MapEntry.createRef(kr, vr); + items[i] = MapEntry.fromRefs(kr, vr); } if (!isValidOrder(items)) { diff --git a/convex-core/src/main/java/convex/core/data/MapTree.java b/convex-core/src/main/java/convex/core/data/MapTree.java index 14aa71ee5..310326c87 100644 --- a/convex-core/src/main/java/convex/core/data/MapTree.java +++ b/convex-core/src/main/java/convex/core/data/MapTree.java @@ -333,7 +333,7 @@ protected MapTree<K, V> assocRef(Ref<K> keyRef, V value, int shift) { int i = Bits.indexForDigit(digit, mask); if (i < 0) { // location not present, need to insert new child - AHashMap<K, V> newChild = MapLeaf.create(MapEntry.createRef(keyRef, Ref.get(value))); + AHashMap<K, V> newChild = MapLeaf.create(MapEntry.fromRefs(keyRef, Ref.get(value))); return insertChild(digit, newChild.getRef()); } else { // child exists, so assoc in new ref at lower shift level diff --git a/convex-core/src/main/java/convex/core/lang/RT.java b/convex-core/src/main/java/convex/core/lang/RT.java index 9ae303ad7..a771f0118 100644 --- a/convex-core/src/main/java/convex/core/lang/RT.java +++ b/convex-core/src/main/java/convex/core/lang/RT.java @@ -1373,7 +1373,7 @@ public static <K extends ACell, V extends ACell> MapEntry<K, V> ensureMapEntry(A AVector<?> v = (AVector<?>) x; if (v.count() != 2) return null; - me = MapEntry.createRef(v.getRef(0), v.getRef(1)); + me = MapEntry.fromRefs(v.getRef(0), v.getRef(1)); } else { return null; } diff --git a/convex-core/src/main/java/convex/core/util/Utils.java b/convex-core/src/main/java/convex/core/util/Utils.java index f644ba4e5..42152ae64 100644 --- a/convex-core/src/main/java/convex/core/util/Utils.java +++ b/convex-core/src/main/java/convex/core/util/Utils.java @@ -90,10 +90,10 @@ public static String toHexString(short val) { * @return Lowercase hex string */ public static String toHexString(byte value) { - StringBuilder sb = new StringBuilder(2); - sb.append(toHexChar((((int) value) & 0xF0) >>> 4)); - sb.append(toHexChar(((int) value) & 0xF)); - return sb.toString(); + char[] chars=new char[2]; + chars[0]=(toHexChar((((int) value) & 0xF0) >>> 4)); + chars[1]=(toHexChar(((int) value) & 0xF)); + return new String(chars); } @@ -104,26 +104,11 @@ public static String toHexString(byte value) { * @return Hex string for the given long */ public static String toHexString(long x) { - StringBuffer sb = new StringBuffer(16); + char[] chars=new char[16]; for (int i = 15; i >= 0; i--) { - sb.append(toHexChar(((int) (x >> (4 * i))) & 0xF)); + chars[i]=(toHexChar(((int) (x >> (4 * i))) & 0xF)); } - return sb.toString(); - } - - /** - * Converts a hex string to a friendly version ( first x chars). - * SECURITY; do not use this output for any comparison. - * - * @param hexString String to show in friendly format. - * @param size Number of hex chars to output. - * @return Hex String - */ - public static String toFriendlyHexString(String hexString, int size) { - String cleanHexString = hexString.replaceAll("^0[Xx]", ""); - String result = cleanHexString.substring(0, size); - // + ".." + cleanHexString.substring(cleanHexString.length() - size); - return result; + return new String(chars); } /** @@ -144,7 +129,7 @@ public static int readInt(byte[] data, int offset) { /** * Reads an int from a specified location in a byte array. Assumes 4-byte - * big-endian representation. Assumes zeros beyong end of array + * big-endian representation. Assumes zeros beyond end of array * * @param data Byte array from which to read the 4-byte int representation * @param offset Offset into byte array to read