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

Commit 1572a8a

Browse files
committed
Merge pull request #1632 from bbowyersmyth/StringStartsWith
String.StartsWith ordinal optimization
2 parents bdc5bce + 2005a13 commit 1572a8a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/mscorlib/src/System/String.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,10 +2556,12 @@ public Boolean StartsWith(String value, StringComparison comparisonType) {
25562556
return CultureInfo.InvariantCulture.CompareInfo.IsPrefix(this, value, CompareOptions.IgnoreCase);
25572557

25582558
case StringComparison.Ordinal:
2559-
if( this.Length < value.Length) {
2559+
if( this.Length < value.Length || m_firstChar != value.m_firstChar) {
25602560
return false;
25612561
}
2562-
return (nativeCompareOrdinalEx(this, 0, value, 0, value.Length) == 0);
2562+
return (value.Length == 1) ?
2563+
true : // First char is the same and thats all there is to compare
2564+
(nativeCompareOrdinalEx(this, 0, value, 0, value.Length) == 0);
25632565

25642566
case StringComparison.OrdinalIgnoreCase:
25652567
if( this.Length < value.Length) {

0 commit comments

Comments
 (0)