Struct StringSegment
- Namespace
- Acuit.Pinpoint.Primitives
- Assembly
- Acuit.Pinpoint.Configuration.dll
An optimized representation of a substring.
public readonly struct StringSegment : IEquatable<StringSegment>, IEquatable<string>
- Implements
- Inherited Members
Constructors
StringSegment(string)
Initializes an instance of the StringSegment struct.
public StringSegment(string buffer)
Parameters
buffer
stringThe original string. The StringSegment includes the whole string.
StringSegment(string, int, int)
Initializes an instance of the StringSegment struct.
public StringSegment(string buffer, int offset, int length)
Parameters
buffer
stringThe original string used as buffer.
offset
intThe offset of the segment within the
buffer
.length
intThe length of the segment.
Fields
Empty
A StringSegment for Empty.
public static readonly StringSegment Empty
Field Value
Properties
Buffer
Gets the string buffer for this StringSegment.
public string Buffer { get; }
Property Value
HasValue
Gets whether or not this StringSegment contains a valid value.
public bool HasValue { get; }
Property Value
this[int]
Gets the char at a specified position in the current StringSegment.
public char this[int index] { get; }
Parameters
index
intThe offset into the StringSegment
Property Value
Length
Gets the length of this StringSegment.
public int Length { get; }
Property Value
Offset
Gets the offset within the buffer for this StringSegment.
public int Offset { get; }
Property Value
Value
Gets the value of this segment as a string.
public string Value { get; }
Property Value
Methods
EndsWith(string, StringComparison)
Checks if the end of this StringSegment matches the specified string when compared using the specified comparisonType
.
public bool EndsWith(string text, StringComparison comparisonType)
Parameters
text
stringThe stringto compare.
comparisonType
StringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
Returns
- bool
true if
text
matches the end of this StringSegment; otherwise, false.
Equals(StringSegment)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(StringSegment other)
Parameters
other
StringSegmentAn object to compare with this object.
Returns
Equals(StringSegment, StringSegment, StringComparison)
Determines whether two specified StringSegment objects have the same value. A parameter specifies the culture, case, and sort rules used in the comparison.
public static bool Equals(StringSegment a, StringSegment b, StringComparison comparisonType)
Parameters
a
StringSegmentThe first StringSegment to compare.
b
StringSegmentThe second StringSegment to compare.
comparisonType
StringComparisonOne of the enumeration values that specifies the rules for the comparison.
Returns
Equals(StringSegment, StringComparison)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(StringSegment other, StringComparison comparisonType)
Parameters
other
StringSegmentAn object to compare with this object.
comparisonType
StringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
Returns
Equals(object)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object obj)
Parameters
obj
objectThe object to compare with the current instance.
Returns
- bool
true if
obj
and this instance are the same type and represent the same value; otherwise, false.
Equals(string)
Checks if the specified string is equal to the current StringSegment.
public bool Equals(string text)
Parameters
text
stringThe string to compare with the current StringSegment.
Returns
- bool
true if the specified string is equal to the current StringSegment; otherwise, false.
Equals(string, StringComparison)
Checks if the specified string is equal to the current StringSegment.
public bool Equals(string text, StringComparison comparisonType)
Parameters
text
stringThe string to compare with the current StringSegment.
comparisonType
StringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
Returns
- bool
true if the specified string is equal to the current StringSegment; otherwise, false.
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Remarks
This GetHashCode is expensive since it allocates on every call. However this is required to ensure we retain any behavior (such as hash code randomization) that string.GetHashCode has.
IndexOf(char)
Gets the zero-based index of the first occurrence of the character c
in this StringSegment.
public int IndexOf(char c)
Parameters
c
charThe Unicode character to seek.
Returns
- int
The zero-based index position of
c
from the beginning of the StringSegment if that character is found, or -1 if it is not.
IndexOf(char, int)
Gets the zero-based index of the first occurrence of the character c
in this StringSegment.
The search starts at start
.
public int IndexOf(char c, int start)
Parameters
c
charThe Unicode character to seek.
start
intThe zero-based index position at which the search starts.
Returns
- int
The zero-based index position of
c
from the beginning of the StringSegment if that character is found, or -1 if it is not.
IndexOf(char, int, int)
Gets the zero-based index of the first occurrence of the character c
in this StringSegment.
The search starts at start
and examines a specified number of count
character positions.
public int IndexOf(char c, int start, int count)
Parameters
c
charThe Unicode character to seek.
start
intThe zero-based index position at which the search starts.
count
intThe number of characters to examine.
Returns
- int
The zero-based index position of
c
from the beginning of the StringSegment if that character is found, or -1 if it is not.
IndexOfAny(char[])
Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.
public int IndexOfAny(char[] anyOf)
Parameters
anyOf
char[]A Unicode character array containing one or more characters to seek.
Returns
- int
The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.
IndexOfAny(char[], int)
Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. The search starts at a specified character position.
public int IndexOfAny(char[] anyOf, int startIndex)
Parameters
anyOf
char[]A Unicode character array containing one or more characters to seek.
startIndex
intThe search starting position.
Returns
- int
The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.
IndexOfAny(char[], int, int)
Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. The search starts at a specified character position and examines a specified number of character positions.
public int IndexOfAny(char[] anyOf, int startIndex, int count)
Parameters
anyOf
char[]A Unicode character array containing one or more characters to seek.
startIndex
intThe search starting position.
count
intThe number of character positions to examine.
Returns
- int
The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.
IsNullOrEmpty(StringSegment)
Indicates whether the specified StringSegment is null or an Empty string.
public static bool IsNullOrEmpty(StringSegment value)
Parameters
value
StringSegmentThe StringSegment to test.
Returns
LastIndexOf(char)
Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance.
public int LastIndexOf(char value)
Parameters
value
charThe Unicode character to seek.
Returns
- int
The zero-based index position of value if that character is found, or -1 if it is not.
Split(char[])
Splits a string into StringSegments that are based on the characters in an array.
public StringTokenizer Split(char[] chars)
Parameters
chars
char[]A character array that delimits the substrings in this string, an empty array that contains no delimiters, or null.
Returns
- StringTokenizer
An StringTokenizer whose elements contain the StringSegmeents from this instance that are delimited by one or more characters in separator.
StartsWith(string, StringComparison)
Checks if the beginning of this StringSegment matches the specified string when compared using the specified comparisonType
.
public bool StartsWith(string text, StringComparison comparisonType)
Parameters
text
stringThe stringto compare.
comparisonType
StringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
Returns
- bool
true if
text
matches the beginning of this StringSegment; otherwise, false.
Subsegment(int)
Retrieves a StringSegment that represents a substring from this StringSegment.
The StringSegment starts at the position specified by offset
.
public StringSegment Subsegment(int offset)
Parameters
offset
intThe zero-based starting character position of a substring in this StringSegment.
Returns
- StringSegment
A StringSegment that begins at
offset
in this StringSegment whose length is the remainder.
Subsegment(int, int)
Retrieves a StringSegment that represents a substring from this StringSegment.
The StringSegment starts at the position specified by offset
and has the specified length
.
public StringSegment Subsegment(int offset, int length)
Parameters
offset
intThe zero-based starting character position of a substring in this StringSegment.
length
intThe number of characters in the substring.
Returns
- StringSegment
A StringSegment that is equivalent to the substring of length
length
that begins atoffset
in this StringSegment
Substring(int)
Retrieves a substring from this StringSegment.
The substring starts at the position specified by offset
and has the remaining length.
public string Substring(int offset)
Parameters
offset
intThe zero-based starting character position of a substring in this StringSegment.
Returns
- string
A string that is equivalent to the substring of remaining length that begins at
offset
in this StringSegment
Substring(int, int)
Retrieves a substring from this StringSegment.
The substring starts at the position specified by offset
and has the specified length
.
public string Substring(int offset, int length)
Parameters
offset
intThe zero-based starting character position of a substring in this StringSegment.
length
intThe number of characters in the substring.
Returns
- string
A string that is equivalent to the substring of length
length
that begins atoffset
in this StringSegment
ToString()
Returns the string represented by this StringSegment or String.Empty
if the StringSegment does not contain a value.
public override string ToString()
Returns
- string
The string represented by this StringSegment or
String.Empty
if the StringSegment does not contain a value.
Trim()
Removes all leading and trailing whitespaces.
public StringSegment Trim()
Returns
- StringSegment
The trimmed StringSegment.
TrimEnd()
Removes all trailing whitespaces.
public StringSegment TrimEnd()
Returns
- StringSegment
The trimmed StringSegment.
TrimStart()
Removes all leading whitespaces.
public StringSegment TrimStart()
Returns
- StringSegment
The trimmed StringSegment.
Operators
operator ==(StringSegment, StringSegment)
Checks if two specified StringSegment have the same value.
public static bool operator ==(StringSegment left, StringSegment right)
Parameters
left
StringSegmentThe first StringSegment to compare, or null.
right
StringSegmentThe second StringSegment to compare, or null.
Returns
implicit operator StringSegment(string)
Creates a new StringSegment from the given string.
public static implicit operator StringSegment(string value)
Parameters
value
stringThe string to convert to a StringSegment
Returns
operator !=(StringSegment, StringSegment)
Checks if two specified StringSegment have different values.
public static bool operator !=(StringSegment left, StringSegment right)
Parameters
left
StringSegmentThe first StringSegment to compare, or null.
right
StringSegmentThe second StringSegment to compare, or null.