Skip to content

Commit

Permalink
Remove some branches
Browse files Browse the repository at this point in the history
  • Loading branch information
gfoidl committed May 29, 2021
1 parent 1a76e22 commit da1027e
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions src/libraries/Microsoft.Extensions.Primitives/src/StringSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,12 @@ public StringSegment(string buffer, int offset, int length)
/// <summary>
/// Gets the value of this segment as a <see cref="string"/>.
/// </summary>
public string Value
{
get
{
if (HasValue)
{
return Buffer.Substring(Offset, Length);
}
else
{
return null;
}
}
}
public string Value => HasValue ? Buffer.Substring(Offset, Length) : null;

/// <summary>
/// Gets whether this <see cref="StringSegment"/> contains a valid value.
/// </summary>
public bool HasValue
{
get { return Buffer != null; }
}
public bool HasValue => Buffer != null;

/// <summary>
/// Gets the <see cref="char"/> at a specified position in the current <see cref="StringSegment"/>.
Expand Down Expand Up @@ -152,7 +136,7 @@ public char this[int index]
/// </exception>
public ReadOnlySpan<char> AsSpan(int start, int length)
{
if (!HasValue || start < 0 || length < 0 || (uint)(start + length) > (uint)Length)
if (!HasValue || (start | length) < 0 || (uint)(start + length) > (uint)Length)
{
ThrowInvalidArguments(start, length, offsetIsStart: true);
}
Expand Down Expand Up @@ -404,7 +388,7 @@ public bool EndsWith(string text, StringComparison comparisonType)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Substring(int offset, int length)
{
if (!HasValue || offset < 0 || length < 0 || (uint)(offset + length) > (uint)Length)
if (!HasValue || (offset | length) < 0 || (uint)(offset + length) > (uint)Length)
{
ThrowInvalidArguments(offset, length);
}
Expand Down Expand Up @@ -437,7 +421,7 @@ public string Substring(int offset, int length)
/// </exception>
public StringSegment Subsegment(int offset, int length)
{
if (!HasValue || offset < 0 || length < 0 || (uint)(offset + length) > (uint)Length)
if (!HasValue || (offset | length) < 0 || (uint)(offset + length) > (uint)Length)
{
ThrowInvalidArguments(offset, length);
}
Expand Down

0 comments on commit da1027e

Please sign in to comment.