Table of Contents

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 string

The 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 string

The original string used as buffer.

offset int

The offset of the segment within the buffer.

length int

The length of the segment.

Fields

Empty

public static readonly StringSegment Empty

Field Value

StringSegment

Properties

Buffer

Gets the string buffer for this StringSegment.

public string Buffer { get; }

Property Value

string

HasValue

Gets whether or not this StringSegment contains a valid value.

public bool HasValue { get; }

Property Value

bool

this[int]

Gets the char at a specified position in the current StringSegment.

public char this[int index] { get; }

Parameters

index int

The offset into the StringSegment

Property Value

char

The char at a specified position.

Length

Gets the length of this StringSegment.

public int Length { get; }

Property Value

int

Offset

Gets the offset within the buffer for this StringSegment.

public int Offset { get; }

Property Value

int

Value

Gets the value of this segment as a string.

public string Value { get; }

Property Value

string

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 string

The stringto compare.

comparisonType StringComparison

One 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 StringSegment

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

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 StringSegment

The first StringSegment to compare.

b StringSegment

The second StringSegment to compare.

comparisonType StringComparison

One of the enumeration values that specifies the rules for the comparison.

Returns

bool

true if the objects are equal; otherwise, false.

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 StringSegment

An object to compare with this object.

comparisonType StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object obj)

Parameters

obj object

The 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 string

The 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 string

The string to compare with the current StringSegment.

comparisonType StringComparison

One 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 char

The 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 char

The Unicode character to seek.

start int

The 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 char

The Unicode character to seek.

start int

The zero-based index position at which the search starts.

count int

The 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 int

The 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 int

The search starting position.

count int

The 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 StringSegment

The StringSegment to test.

Returns

bool

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 char

The 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 string

The stringto compare.

comparisonType StringComparison

One 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 int

The 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 int

The zero-based starting character position of a substring in this StringSegment.

length int

The number of characters in the substring.

Returns

StringSegment

A StringSegment that is equivalent to the substring of length length that begins at offset 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 int

The 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 int

The zero-based starting character position of a substring in this StringSegment.

length int

The number of characters in the substring.

Returns

string

A string that is equivalent to the substring of length length that begins at offset 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 StringSegment

The first StringSegment to compare, or null.

right StringSegment

The second StringSegment to compare, or null.

Returns

bool

true if the value of left is the same as the value of right; otherwise, false.

implicit operator StringSegment(string)

Creates a new StringSegment from the given string.

public static implicit operator StringSegment(string value)

Parameters

value string

The string to convert to a StringSegment

Returns

StringSegment

operator !=(StringSegment, StringSegment)

Checks if two specified StringSegment have different values.

public static bool operator !=(StringSegment left, StringSegment right)

Parameters

left StringSegment

The first StringSegment to compare, or null.

right StringSegment

The second StringSegment to compare, or null.

Returns

bool

true if the value of left is different from the value of right; otherwise, false.