Class ReadOnlyStringBuffer
Namespace: OceanApocalypse.RSML.Toolchain.Sources
Assembly: RSML.Toolchain.Sources.dll
A read-only buffer backed by a string. All operations opt for performance primarily via the internal use of ReadOnlySpan over string allocations and also via caching.
public sealed class ReadOnlyStringBuffer : IBuffer, IDisposable, IEquatable<IBuffer>, IEquatable<Char[]?>, IEquatable<String?>, IEquatable<ReadOnlyMemory<Char>>, ISupportsCache
Inheritance
Implements
IBuffer, IDisposable, IEquatable<IBuffer>, IEquatable<Char[]?>, IEquatable<String?>, IEquatable<ReadOnlyMemory<Char>>, ISupportsCache
Inherited Members
Object.Equals(Object?), Object.Equals(Object?, Object?), Object.GetHashCode(), Object.GetType(), Object.ReferenceEquals(Object?, Object?), Object.ToString()
Constructors
ReadOnlyStringBuffer(String)
Initializes a new ReadOnlyStringBuffer with a string.
public ReadOnlyStringBuffer(String content)
Parameters
content String
The string that the buffer will wrap.
ReadOnlyStringBuffer(ReadOnlySpan<Char>)
Initializes a new ReadOnlyStringBuffer by allocating a string from a ReadOnlySpan.
public ReadOnlyStringBuffer(ReadOnlySpan<Char> content)
Parameters
content ReadOnlySpan<Char>
The span pointing to the string's data.
ReadOnlyStringBuffer(Char[])
Initializes a new ReadOnlyStringBuffer with an array of characters.
public ReadOnlyStringBuffer(Char[] content)
Parameters
content Char[]
The array of characters to use for the buffer.
ReadOnlyStringBuffer(Byte[], Encoding?)
Initializes a new ReadOnlyStringBuffer with an array of bytes and the encoding to use when decoding them.
public ReadOnlyStringBuffer(Byte[] content, Encoding? encoding = null)
Parameters
content Byte[]
The array of bytes to use for the buffer.
encoding Encoding?
The encoding to use when decoding content.
Use null for the Default encoding.
ReadOnlyStringBuffer(Byte*, Int32, Encoding?)
Initializes a new ReadOnlyStringBuffer with a pointer referencing an array of bytes and the encoding to use when decoding them.
[CLSCompliant(false)]
public ReadOnlyStringBuffer(Byte* contentPtr, Int32 byteCount, Encoding? encoding = null)
Parameters
contentPtr Byte*
The pointer referecing the array of bytes to use for the buffer.
byteCount Int32
The amount of bytes in the array referenced by contentPtr.
encoding Encoding?
The encoding to use when decoding contentPtr.
Use null for the Default encoding.
Remarks
This method is not CLS-compliant due to the unsafe context and the use of pointers.
Properties
CacheExists
Whether there's cached data.
public Boolean CacheExists { get; }
Property Value
IsEmpty
Whether the source is completely empty.
public Boolean IsEmpty { get; }
Property Value
IsReadOnly
Whether the source can be mutated.
public Boolean IsReadOnly { get; }
Property Value
Remarks
Always returns true, as ReadOnlyStringBuffer only
supports read-only content (hence the name).
Length
The length of the source.
public Int32 Length { get; }
Property Value
LineCount
The total amount of lines in the buffer.
public Int32 LineCount { get; }
Property Value
Remarks
LineCount automatically builds cache if
no cached data exists. No BuildCache calls
are necessary.
this[Int32]
Gets a single item out of the buffer.
public Char this[Int32 index] { get; }
Property Value
this[SourceLocation]
Gets a single item out of the buffer.
public Char this[SourceLocation location] { get; }
Property Value
Methods
BuildCache()
Builds the cache if it doesn't exist yet.
public void BuildCache()
BuildCache(Boolean)
Builds the cache. If forceRebuild is set to true,
the cache will be built even if it already exists.
public void BuildCache(Boolean forceRebuild)
Parameters
forceRebuild Boolean
Whether to force the cache to be built even if it exists.
CountUntilEndOfLine(Int32, out Boolean)
Counts the amount of items until the next line separator in the buffer, relative to a given index.
Only line separators count - regular whitespace do not. CRLF counts as a single line separator, to avoid double counting.
public Int32 CountUntilEndOfLine(Int32 index, out Boolean isCrLf)
Parameters
index Int32
The index at which to start counting.
isCrLf Boolean
Whether the line separator at which the method stopped is the CR in a CRLF sequence. If true, the next item in the buffer is LF.
Returns
The index of the next line separator, relative to an index.
Remarks
This method allows the EOF index as in-range. The convention is as follows:
- If the index is EOF (
Length), then the output is always 0 andisCrLfis alwaysfalse. - If the index is the last (
Length- 1), then the output is always 0.
isCrLf is only true if all the following conditions are true:
- The next line start counting from
indexis preceded by a CRLF sequence. indexdoes not point to the LF in the CRLF sequence.indexdoes not point to EOF.
CountUntilNotWhitespace(Int32)
Counts the amount of items until the next non-whitespace item in the buffer, relative to a given index.
Line separators are included in the whitespace category.
public Int32 CountUntilNotWhitespace(Int32 index)
Parameters
index Int32
The index at which to start counting.
Returns
The index of the next non-whitespace item, relative to a index.
Remarks
This method allows the EOF index as in-range.
If the index is EOF (Length), then the output is always 0.
The return value, when summed with index, becomes the index of the first character that
is not whitespace, counting from index.
The only exception is if the buffer has been consumed (you pass EOF index or there's no more characters that are
not whitespace), meaning the return value, when summed with index is the value of
Length, which is also the EOF index.
CountUntilWhitespace(Int32)
Counts the amount of items until the next whitespace item in the buffer, relative to a given index.
Line separators are included in the whitespace category.
public Int32 CountUntilWhitespace(Int32 index)
Parameters
index Int32
The index at which to start counting.
Returns
The index of the next whitespace item, relative to a index.
Remarks
This method allows the EOF index as in-range.
If the index is EOF (Length), then the output is always 0.
The return value, when summed with index, becomes the index of the first character that
is whitespace, counting from index.
The only exception is if the buffer has been consumed (you pass EOF index or there's no more characters that are
whitespace), meaning the return value, when summed with index is the value of
Length, which is also the EOF index.
CountWhile(Func<Int32, Char, Boolean>, Int32)
Counts the amount of items, starting from a given index,
while a predicate returns true.
public Int32 CountWhile(Func<Int32, Char, Boolean> predicate, Int32 index)
Parameters
predicate Func<Int32, Char, Boolean>
A function that takes the current index (relative to index),
which is incremented every item, and the item associated with it. Execution stops when
the predicate returns false or the index is out of bounds.
index Int32
The index at which to start counting; all indexes will also be given to the
predicate as an offset that when added to the index of the position
equal the actual index.
Returns
The amount of items counted.
Remarks
This method allows the EOF index as in-range.
If the index is EOF (Length), then the output is always 0.
The return value, when summed with index, becomes the index of the first character that
fails to verify the predicate, counting from index.
The only exception is if the buffer has been consumed (you pass EOF index or there's no more characters that fail to verify
the predicate), meaning the return value, when summed with index is the value of
Length, which is also the EOF index.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Equals(Object?)
Determines whether the specified object is equal to the current object.
public override Boolean Equals(Object? obj)
Parameters
obj Object?
The object to compare with the current object.
Returns
true if the specified object is equal to the current object; otherwise, false.
Equals(Char[]?)
Checks if an array of characters is equal to the current instance.
public Boolean Equals(Char[]? other)
Parameters
other Char[]?
The array.
Returns
True if equals.
Equals(IBuffer?)
Checks if another read-only buffer is equal to the current instance.
public Boolean Equals(IBuffer? other)
Parameters
other IBuffer?
The other read-only buffer.
Returns
True if equals.
Equals(ReadOnlyMemory<Char>)
Checks if a read-only contiguous region of memory is equal to the current instance.
public Boolean Equals(ReadOnlyMemory<Char> other)
Parameters
other ReadOnlyMemory<Char>
The region of memory.
Returns
True if equals.
Equals(ReadOnlySpan<Char>)
Checks if a read-only contiguous region of memory is equal to the current instance.
public Boolean Equals(ReadOnlySpan<Char> other)
Parameters
other ReadOnlySpan<Char>
The region of memory.
Returns
True if equals.
Equals(String?)
Checks if a string is equal to the current instance.
public Boolean Equals(String? other)
Parameters
other String?
The string.
Returns
True if equals.
GetHashCode()
Serves as the default hash function.
public override Int32 GetHashCode()
Returns
A hash code for the current object.
GetLengthOfLine(Int32)
Returns the length of a line given its 0-based line number. Line separators do not count towards the length.
public Int32 GetLengthOfLine(Int32 lineNumber)
Parameters
lineNumber Int32
The 0-based line number.
Returns
The length of the line.
Remarks
This method follows EOF conventions.
EOF is considered a 0-character sequence in line N, where N is LineCount.
Keep in mind N does not point to an actual line (it's just a convention), as line numbers are 0-based
(meaning the actual last line is located at N - 1).
GetLengthOfLineFromIndex(Int32)
Returns the length of a line given a 0-based index of one of its items. Line separators do not count towards the length.
public Int32 GetLengthOfLineFromIndex(Int32 index)
Parameters
index Int32
The 0-based index whose line is considered.
Returns
The length of the line.
Remarks
This method follows EOF conventions.
EOF is considered a 0-character sequence in line N, where N is LineCount.
Keep in mind N does not point to an actual line (it's just a convention), as line numbers are 0-based
(meaning the actual last line is located at N - 1).
GetLine(Int32)
Given a 0-based line number, returns the matching line as an array of buffer items.
public ReadOnlySpan<Char> GetLine(Int32 lineNumber)
Parameters
lineNumber Int32
The 0-based line number.
Returns
The line as an array of items.
Remarks
This method follows EOF conventions.
EOF is considered a 0-character sequence in line N, where N is LineCount.
Keep in mind N does not point to an actual line (it's just a convention), as line numbers are 0-based
(meaning the actual last line is located at N - 1).
GetLineFromIndex(Int32)
Tries to read the line that contains the item at index.
No end of line characters are added.
public ReadOnlySpan<Char> GetLineFromIndex(Int32 index)
Parameters
index Int32
The index at which to determine what the current line is.
Returns
The line, as an array of items.
GetLineNumberFromIndex(Int32)
Determines the 0-based line number of the line that contains the item located at index.
public Int32 GetLineNumberFromIndex(Int32 index)
Parameters
index Int32
The index whose parent line's number is to be returned.
Returns
The 0-based number of the line that contains item located at index.
Remarks
This method follows EOF conventions.
GetSourceLocation(Int32)
Converts an index into a location.
public SourceLocation GetSourceLocation(Int32 index)
Parameters
index Int32
The index.
Returns
The location.
Remarks
Unlike with other ReadOnlyStringBuffer methods, this one
does not follow EOF conventions and, because of that, does not accept the
EOF index (index at Length), because it is not
considered a location.
GetSourceSpan(Int32, Int32)
Converts the buffer region into a span.
public SourceSpan GetSourceSpan(Int32 startIndex, Int32 endIndex)
Parameters
startIndex Int32
The starting index.
endIndex Int32
The end index, which is included in the span.
Returns
The span.
Slice(Int32, Int32)
Slices a region of the buffer.
public ReadOnlySpan<Char> Slice(Int32 start, Int32 length)
Parameters
start Int32
The index of the first item in the slice.
length Int32
The amount of items to slice starting at start.
Returns
A slice, as an array of items.
Remarks
Unlike with other ReadOnlyStringBuffer methods, this one
does not follow EOF conventions and, because of that, does not accept the
EOF index (index at Length), because it is not
considered part of any slice.
ToString()
Returns the buffer's content as a String.
public override String ToString()
Returns
The buffer's content.
TryGetChar(Int32, out Char)
Tries to return the item at index.
public Boolean TryGetChar(Int32 index, out Char item)
Parameters
index Int32
The index of the character.
item Char
The item.
Returns
False if the buffer is out of bounds or an exception occured.
Remarks
This method follows the EOF convention where the EOF character
is 0 ('\0') and the return value is false, due to EOF
not being an actual buffer location.
TryGetChar(SourceLocation, out Char)
Tries to return the item at the specified location.
public Boolean TryGetChar(SourceLocation location, out Char item)
Parameters
location SourceLocation
The item's location.
item Char
The item.
Returns
False if the buffer is out of bounds or an exception occured.
Remarks
This method follows the EOF convention where the EOF character
is 0 ('\0') and the return value is false, due to EOF
not being an actual buffer location.
TryGetLine(Int32, Span<Char>)
Given a 0-based line number, assigns the exact line to a result buffer (destination).
No end of line characters are added.
public Boolean TryGetLine(Int32 lineNumber, Span<Char> destination)
Parameters
lineNumber Int32
The 0-based line number.
The destination buffer for the line.
Returns
True if successful.
Remarks
This method follows EOF conventions.
EOF is considered a 0-character sequence in line N, where N is LineCount.
Keep in mind N does not point to an actual line (it's just a convention), as line numbers are 0-based
(meaning the actual last line is located at N - 1).
TryGetLineFromIndex(Int32, Span<Char>)
Tries to read the line that contains the item at index.
No end of line characters are added.
public Boolean TryGetLineFromIndex(Int32 index, Span<Char> destination)
Parameters
index Int32
The index at which to determine what the current line is.
The destination span that will contain the line.
Returns
True if successful.
Remarks
This method follows EOF conventions.
EOF is considered a 0-character sequence in line N, where N is LineCount.
Keep in mind N does not point to an actual line (it's just a convention), as line numbers are 0-based
(meaning the actual last line is located at N - 1). If index is EOF, the
line will also be EOF.
TrySlice(Int32, Span<Char>)
Slices a region of the buffer into a performant span.
public Boolean TrySlice(Int32 start, Span<Char> slice)
Parameters
start Int32
The index of the first item in the slice.
The span serving as the destination for the slice.
Returns
Remarks
Unlike with other ReadOnlyStringBuffer methods, this one
does not follow EOF conventions and, because of that, does not accept the
EOF index (index at Length), because it is not
considered part of any slice.
TrySlice(SourceSpan, Span<Char>)
Slices a region of the buffer into a performant span.
public Boolean TrySlice(SourceSpan sourceSpan, Span<Char> slice)
Parameters
sourceSpan SourceSpan
The span indicating what the slice is.
The span serving as the destination for the slice.
Returns
Remarks
Unlike with other ReadOnlyStringBuffer methods, this one
does not follow EOF conventions and, because of that, does not accept the
EOF index (index at Length), because it is not
considered part of any slice.
Operators
operator ==(ReadOnlyStringBuffer, ReadOnlyStringBuffer)
Checks if two read-only string buffers are equals.
public static Boolean operator ==(ReadOnlyStringBuffer left, ReadOnlyStringBuffer right)
Parameters
left ReadOnlyStringBuffer
right ReadOnlyStringBuffer
Returns
True if equals.
operator !=(ReadOnlyStringBuffer, ReadOnlyStringBuffer)
Checks if two read-only string buffers are different.
public static Boolean operator !=(ReadOnlyStringBuffer left, ReadOnlyStringBuffer right)
Parameters
left ReadOnlyStringBuffer
right ReadOnlyStringBuffer
Returns
True if different.