Package convex.core.data
Class AString
- All Implemented Interfaces:
IValidated
,IWriteable
,Comparable<ABlobLike<?>>
- Direct Known Subclasses:
StringShort
,StringSlice
,StringTree
Abstract base Class representing a CVM String.
CVM Strings are UTF-8 byte strings with an immutable, fixed count in bytes.
CVM Strings are NOT enforced to be valid UTF-8, for reasons of performance, simplicity and
consistent behaviour (e.g. in conversions to and from Blobs). It is up to clients to decide
how to represent invalid UTF-8 if necessary.
-
Field Summary
FieldsFields inherited from class convex.core.data.ACell
cachedRef, memorySize
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAppend a CVM String to this CVM String.Convenience method to add a Java String to a CVM String.abstract byte
byteAt
(long i) Gets a byte at the specified index.final int
charAt
(long i) Gets the Unicode character at position i, or -1 if not validlong
count()
Returns the number of elements in this valuefinal StringShort
empty()
Returns the singleton empty Stringint
encode
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.abstract int
encodeRaw
(byte[] bs, int pos) Encode the data of this String.final boolean
Checks for equality with another Cell.abstract boolean
Checks for equality between two strings.get
(long i) Gets the Character at the specified point in the String, or null if there is no valid Character at this position.int
getBytes
(byte[] dest, int destOffset) Copies the bytes from this instance to a given destination arraygetElementRef
(long i) Gets a Ref to the element at the specified element index in this collectionfinal byte
getTag()
Gets the tag byte for this cell.getType()
Gets the most specific known runtime Type for this Cell.final int
hashCode()
Gets a Java hashCode for this CVM String.long
Returns the number of matching hex digits in the given hex range of another Blob.int
intAt
(long index) Gets 32 bytes integer at given position.boolean
print
(BlobBuilder sb, long limit) Prints this Object to a readable String Representation.protected abstract void
printEscaped
(BlobBuilder sb, long start, long end) Prints this string as escaped UTF-8abstract AString
slice
(long start, long end) Gets a slice of this string, or null if not a valid sliceSplits this string by the given character The result will always have at least one String, and as many additional Strings as the split character occurs.abstract ABlob
toBlob()
Converts this String to an equivalent Blob representation.abstract AString
Converts this Cell to a canonical version.toCVMString
(long limit) Returns the CVM String representation of this Cell.toString()
Returns the Java String representation of this Cell.abstract int
writeRawData
(byte[] bs, int pos) Write the raw UTF-8 data of this String to a byte array.Methods inherited from class convex.core.data.ABlobLike
appendHex, byteAtUnchecked, compareTo, equalsBytes, getBytes, getHexDigit, hexEquals, hexLength, hexMatch, isDataValue, longValue, toFlatBlob, toHexString, toHexString
Methods inherited from class convex.core.data.ACountable
isEmpty, size, slice
Methods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createEncoding, createRef, equals, genericEquals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getMemorySize, getRef, getRef, getRefCount, isCanonical, isCVMValue, isEmbedded, updateRefs, validate, validateCell
Methods inherited from class convex.core.data.AObject
attachEncoding, print, print
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface convex.core.data.IWriteable
estimatedEncodingSize
-
Field Details
-
length
protected long length
-
-
Constructor Details
-
AString
protected AString(long length)
-
-
Method Details
-
getType
-
print
Description copied from class:AObject
Prints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time when limit of printing is exceeded otherwise DoS attacks may be possible. -
count
public long count()Description copied from class:ACountable
Returns the number of elements in this value- Specified by:
count
in classACountable<CVMChar>
- Returns:
- Number of elements
-
printEscaped
Prints this string as escaped UTF-8- Parameters:
sb
-
-
empty
Returns the singleton empty String -
charAt
public final int charAt(long i) Gets the Unicode character at position i, or -1 if not valid- Parameters:
i
- Index into String (byte position)- Returns:
- Unicode code point, or -1 if not a valid code point at this position
-
intAt
public int intAt(long index) Gets 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- Parameters:
index
- Index into String (byte position)- Returns:
- Raw integer value
-
byteAt
-
get
Gets the Character at the specified point in the String, or null if there is no valid Character at this position.- Specified by:
get
in classACountable<CVMChar>
- Parameters:
i
- Index of element to get- Returns:
- CVMChar instance, or null for invalid UTF-8 or any character out of the string bounds
-
getElementRef
Description copied from class:ACountable
Gets a Ref to the element at the specified element index in this collection- Overrides:
getElementRef
in classABlobLike<CVMChar>
- Parameters:
i
- Index of element to get- Returns:
- Element at the specified index
-
getBytes
public int getBytes(byte[] dest, int destOffset) Description copied from class:ABlobLike
Copies the bytes from this instance to a given destination array -
toString
-
hexMatch
Description copied from class:ABlobLike
Returns the number of matching hex digits in the given hex range of another Blob. Assumes range is valid for both blobs. Returns length if this Blob is exactly equal to the specified hex range. -
toCVMString
Description copied from class:ACell
Returns the CVM String representation of this Cell. Normally, this is as printed, but may be different for some types. SHOULD return null in O(1) time if the length of the CVM String can be proved to exceed the limit. MUST complete in O(limit) time and space otherwise The String representation is intended to be a easy-to-read textual representation of the Cell's data content.- Overrides:
toCVMString
in classACell
- Parameters:
limit
- Limit of CVM String length in UTF-8 bytes- Returns:
- CVM String, or null if limit exceeded
-
append
-
slice
Gets a slice of this string, or null if not a valid slice- Specified by:
slice
in classACountable<CVMChar>
- Parameters:
start
- Start index (inclusive)end
- End index (Exclusive)- Returns:
- Specified substring
-
split
Splits this string by the given character The result will always have at least one String, and as many additional Strings as the split character occurs.- Parameters:
c
- CMVChar instance with which to split- Returns:
- Vector of Strings, excluding the split character.
-
encode
public int encode(byte[] bs, int pos) Description copied from class:ACell
Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.- Specified by:
encode
in interfaceIWriteable
- Specified by:
encode
in classACell
- Parameters:
bs
- A byte array to which to write the encodingpos
- The offset into the byte array- Returns:
- New position after writing
-
encodeRaw
-
writeRawData
public abstract int writeRawData(byte[] bs, int pos) Write the raw UTF-8 data of this String to a byte array.- Parameters:
bs
- Destination byte arraypos
- Position in target array to write to- Returns:
- End position in array after writing
-
getTag
public final byte getTag()Description copied from class:ACell
Gets the tag byte for this cell. The tag byte is always equal to the first byte of the Cell's canonical Encoding, and is sufficient to distinguish how to read the rest of the encoding. -
hashCode
-
toBlob
-
append
-
equals
Description copied from class:ACell
Checks for equality with another Cell. In general, Cells are considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if they have a more efficient equals implementation. MUST NOT require reads from Store. -
equals
Checks for equality between two strings. Should be optimised- Parameters:
b
- Other string (may be null)- Returns:
- True if strings are exactly equal, false otherwise
-
toCanonical
Description copied from class:ACell
Converts 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:
toCanonical
in classACell
- Returns:
- Canonical version of Cell
-