Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 55bb222

Browse files
authored
Adds StringBuilder.Equals(ReadOnlySpan<char>) Api (#15759)
Adds StringBuilder.Equals(ReadOnlySpan<char>) Api
1 parent 2bb0fed commit 55bb222

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/mscorlib/shared/System/Text/StringBuilder.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,35 @@ public bool Equals(StringBuilder sb)
16451645
}
16461646
}
16471647

1648+
/// <summary>
1649+
/// Determines if the contents of this builder are equal to the contents of ReadOnlySpan<char>.
1650+
/// </summary>
1651+
/// <param name="value">The ReadOnlySpan{char}.</param>
1652+
public bool Equals(ReadOnlySpan<char> value)
1653+
{
1654+
if (value.Length != Length)
1655+
return false;
1656+
1657+
StringBuilder sbChunk = this;
1658+
int offset = 0;
1659+
1660+
do
1661+
{
1662+
int chunk_length = sbChunk.m_ChunkLength;
1663+
offset += chunk_length;
1664+
1665+
ReadOnlySpan<char> chunk = new ReadOnlySpan<char>(sbChunk.m_ChunkChars, 0, chunk_length);
1666+
1667+
if (!chunk.Equals(value.Slice(value.Length - offset, chunk_length)))
1668+
return false;
1669+
1670+
sbChunk = sbChunk.m_ChunkPrevious;
1671+
} while (sbChunk != null);
1672+
1673+
Debug.Assert(offset == Length);
1674+
return true;
1675+
}
1676+
16481677
/// <summary>
16491678
/// Replaces all instances of one string with another in part of this builder.
16501679
/// </summary>

0 commit comments

Comments
 (0)