Package convex.core.data
Class StringShort
java.lang.Object
- All Implemented Interfaces:
IValidated,IWriteable,Comparable<ABlobLike<?>>
Class representing a short CVM string, backed by a flat Blob
Used for most small strings, and acts as the leaf chunk for StringTrees
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringShortThe canonical empty Stringstatic final intLength of longest StringShort value that is embedded Basically max embedded length minus tag byte and 2-byte lengthstatic final intstatic final intLength of longest StringShort value in bytes.Fields inherited from class convex.core.data.ACell
cachedRef, memorySize -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedStringShort(byte[] data) protectedStringShort(byte[] data, int offset, int length) protectedStringShort(Blob data) -
Method Summary
Modifier and TypeMethodDescriptionbytebyteAt(long index) Gets a byte at the specified index.intstatic StringShortcreate(AArrayBlob b) Creates a StringShort instance from a Blob of UTF-8 data.static StringShortCreates a StringShort instance from a regular Java StringintencodeRaw(byte[] bs, int pos) Encode the data of this String.booleanChecks for equality between two strings.final booleanbooleanequalsBytes(ABlob b) Compare the byte content of this BlobLike to a Blob valueintEstimate the encoded data size for this Cell.getRef(int i) Gets a numbered child Ref from within this Cell.intGets the number of Refs contained within this Cell.intintAt(long index) Gets 32 bytes integer at given position.booleanReturns true if this Cell is in a canonical representation.final booleanReturns true if this cell is a first class CVM Value allowable in the CVM state Sub-structural cells that are not themselves first class values should return false Records and types that are not permissible on the CVM should return false.booleanDetermines if this Cell Represents an embedded object.longConverts this BlobLike to the corresponding long value.protected voidprintEscaped(BlobBuilder sb, long start, long end) Prints this string as escaped UTF-8static StringShortRead a StringShort from an encoding.slice(long start, long end) Gets a slice of this string, or null if not a valid slicetoBlob()Converts this String to an equivalent Blob representation.Converts this Cell to a canonical version.toString()Returns the Java String representation of this Cell.updateRefs(IRefFunction func) Updates all Refs in this object using the given function.voidValidates the local structure and invariants of this cell.intwriteRawData(byte[] bs, int pos) Write the raw UTF-8 data of this String to a byte array.Methods inherited from class convex.core.data.AString
append, append, charAt, count, empty, encode, equals, get, getBytes, getElementRef, getTag, getType, hashCode, hexMatch, print, split, toCVMStringMethods inherited from class convex.core.data.ABlobLike
appendHex, byteAtUnchecked, getBytes, getHexDigit, hexEquals, hexLength, hexMatch, isDataValue, toFlatBlob, toHexString, toHexStringMethods inherited from class convex.core.data.ACountable
isEmpty, size, sliceMethods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createEncoding, createRef, equals, genericEquals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getMemorySize, getRef, validateMethods inherited from class convex.core.data.AObject
attachEncoding, print, print
-
Field Details
-
MAX_EMBEDDED_STRING_LENGTH
public static final int MAX_EMBEDDED_STRING_LENGTHLength of longest StringShort value that is embedded Basically max embedded length minus tag byte and 2-byte length- See Also:
-
MAX_LENGTH
public static final int MAX_LENGTHLength of longest StringShort value in bytes. Use Blob as base.- See Also:
-
MAX_ENCODING_LENGTH
public static final int MAX_ENCODING_LENGTH -
EMPTY
The canonical empty String
-
-
Constructor Details
-
StringShort
-
StringShort
protected StringShort(byte[] data) -
StringShort
protected StringShort(byte[] data, int offset, int length)
-
-
Method Details
-
create
Creates a StringShort instance from a regular Java String- Parameters:
string- String to wrap as StringShort- Returns:
- StringShort instance, or null if String is of invalid size
-
create
Creates a StringShort instance from a Blob of UTF-8 data. Shares underlying array.- Parameters:
b- Array Blob to convert to StringShort- Returns:
- StringShort instance
-
byteAt
-
intAt
public int intAt(long index) Description copied from class:AStringGets 32 bytes integer at given position. Extends with 255 (invalid UTF-8) if needed. The main purpose of this function is to enable fast peeking at UTF-8 characters -
validateCell
Description copied from class:ACellValidates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.- Specified by:
validateCellin classACell- Throws:
InvalidDataException- If the Cell is invalid
-
encodeRaw
public int encodeRaw(byte[] bs, int pos) Description copied from class:AStringEncode the data of this String. Assumes tag already written -
writeRawData
public int writeRawData(byte[] bs, int pos) Description copied from class:AStringWrite the raw UTF-8 data of this String to a byte array.- Specified by:
writeRawDatain classAString- Parameters:
bs- Destination byte arraypos- Position in target array to write to- Returns:
- End position in array after writing
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteableEstimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures- Returns:
- The estimated size for the binary representation of this object.
-
isCanonical
public boolean isCanonical()Description copied from class:ACellReturns true if this Cell is in a canonical representation. Non-canonical objects may be used on a temporary internal basis, they should be converted to canonical representations for general purpose use.- Specified by:
isCanonicalin classACell- Returns:
- true if the object is in canonical format, false otherwise
-
isCVMValue
public final boolean isCVMValue()Description copied from class:ACellReturns true if this cell is a first class CVM Value allowable in the CVM state Sub-structural cells that are not themselves first class values should return false Records and types that are not permissible on the CVM should return false. Pretty much everything else should return true. Note: CVM values might not be in a canonical format, e.g. temporary data structures- Specified by:
isCVMValuein classACell- Returns:
- true if the object is a CVM Value, false otherwise
-
isEmbedded
public boolean isEmbedded()Description copied from class:ACellDetermines if this Cell Represents an embedded object. Embedded objects are encoded directly into the encoding of the containing Cell (avoiding the need for a hashed reference). Subclasses should override this if they have a cheap O(1) way to determine if they are embedded or otherwise.- Overrides:
isEmbeddedin classACell- Returns:
- true if Cell is embedded, false otherwise
-
getRef
Description copied from class:ACellGets a numbered child Ref from within this Cell. WARNING: May need to convert to a canonical instance -
updateRefs
Description copied from class:ACellUpdates all Refs in this object using the given function. The function *must not* change the hash value of Refs, in order to ensure structural integrity of modified data structures. The implementation *should* re-attach any original encoding in order to prevent re-encoding or surplus hashing This is a building block for a very sneaky trick that enables use to do a lot of efficient operations on large trees of smart references. Must return the same object if no Refs are altered.- Overrides:
updateRefsin classACell- Parameters:
func- Ref update function- Returns:
- Cell with updated Refs
-
getRefCount
public int getRefCount()Description copied from class:ACellGets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. Contained Refs may be either external or embedded.- Overrides:
getRefCountin classACell- Returns:
- The number of Refs in this Cell
-
read
Read a StringShort from an encoding. Assumes tag and length already validated.- Parameters:
length- Length of string in UTF-8 bytesblob- Source of encodingpos- Position of encoding start (i.e. tag)- Returns:
- String instance
-
toBlob
-
equals
-
equals
-
compareTo
-
toCanonical
Description copied from class:ACellConverts this Cell to a canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise. Callers should usually use getCanonical(), which caches canonical instances once created- Specified by:
toCanonicalin classAString- Returns:
- Canonical version of Cell
-
slice
-
printEscaped
Description copied from class:AStringPrints this string as escaped UTF-8- Specified by:
printEscapedin classAString- Parameters:
sb-
-
equalsBytes
Description copied from class:ABlobLikeCompare the byte content of this BlobLike to a Blob value- Specified by:
equalsBytesin classABlobLike<CVMChar>- Parameters:
b- Blob value to compare with- Returns:
- `true` if byte contents are exactly equal, `false` otherwise
-
longValue
public long longValue()Description copied from class:ABlobLikeConverts this BlobLike to the corresponding long value. Assumes big-endian format, as if the entire blob is interpreted as an unsigned big integer. Higher bytes outside the Long range will be ignored, i.e. the lowest 64 bits are taken -
toString
-