Skip to content

Commit

Permalink
Optimize StringSlice helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Apr 4, 2020
1 parent 1e68149 commit f0830b3
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions src/Markdig/Helpers/StringSlice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public StringSlice(string text, int start, int end)
/// <summary>
/// Gets or sets the start position within <see cref="Text"/>.
/// </summary>
public int Start { get; set; }
public int Start { readonly get; set; }

/// <summary>
/// Gets or sets the end position (inclusive) within <see cref="Text"/>.
/// </summary>
public int End { get; set; }
public int End { readonly get; set; }

/// <summary>
/// Gets the length.
Expand Down Expand Up @@ -275,13 +275,7 @@ public readonly int IndexOf(string text, int offset = 0, bool ignoreCase = false
if (length <= 0)
return -1;

#if NETCORE
var span = Text.AsSpan(offset, length);
int index = ignoreCase ? span.IndexOf(text, StringComparison.OrdinalIgnoreCase) : span.IndexOf(text);
return index == -1 ? index : index + offset;
#else
return Text.IndexOf(text, offset, length, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
#endif
}

/// <summary>
Expand All @@ -296,12 +290,7 @@ public readonly int IndexOf(char c)
if (length <= 0)
return -1;

#if NETCORE
int index = Text.AsSpan(start, length).IndexOf(c);
return index == -1 ? index : index + start;
#else
return Text.IndexOf(c, start, length);
#endif
}

/// <summary>
Expand All @@ -312,16 +301,15 @@ public readonly int IndexOf(char c)
/// </returns>
public bool TrimStart()
{
// Strip leading spaces
for (; Start <= End; Start++)
{
if (Start < Text.Length
&& !Text[Start].IsWhitespace())
{
break;
}
}
return IsEmpty;
string text = Text;
int end = End;
int i = Start;

while (i <= end && (uint)i < (uint)text.Length && text[i].IsWhitespace())
i++;

Start = i;
return i > end;
}

/// <summary>
Expand All @@ -330,16 +318,15 @@ public bool TrimStart()
/// <param name="spaceCount">The number of spaces trimmed.</param>
public void TrimStart(out int spaceCount)
{
spaceCount = 0;
// Strip leading spaces
for (; Start <= End; Start++)
{
if (!Text[Start].IsWhitespace())
{
break;
}
spaceCount++;
}
string text = Text;
int end = End;
int i = Start;

while (i <= end && (uint)i < (uint)text.Length && text[i].IsWhitespace())
i++;

spaceCount = i - Start;
Start = i;
}

/// <summary>
Expand All @@ -348,24 +335,34 @@ public void TrimStart(out int spaceCount)
/// <returns></returns>
public bool TrimEnd()
{
for (; Start <= End; End--)
{
if (End < Text.Length
&& !Text[End].IsWhitespace())
{
break;
}
}
return IsEmpty;
string text = Text;
int start = Start;
int i = End;

while (start <= i && (uint)i < (uint)text.Length && text[i].IsWhitespace())
i--;

End = i;
return start > i;
}

/// <summary>
/// Trims whitespaces from both the start and end of this slice.
/// </summary>
public void Trim()
{
TrimStart();
TrimEnd();
string text = Text;
int start = Start;
int end = End;

while (start <= end && (uint)start < (uint)text.Length && text[start].IsWhitespace())
start++;

while (start <= end && (uint)end < (uint)text.Length && text[end].IsWhitespace())
end--;

Start = start;
End = end;
}

/// <summary>
Expand Down Expand Up @@ -393,9 +390,12 @@ public readonly override string ToString()
/// <returns><c>true</c> if this slice is empty or made only of whitespaces; <c>false</c> otherwise</returns>
public readonly bool IsEmptyOrWhitespace()
{
for (int i = Start; i <= End; i++)
string text = Text;
int end = End;

for (int i = Start; i <= end && (uint)i < (uint)text.Length; i++)
{
if (!Text[i].IsWhitespace())
if (!text[i].IsWhitespace())
{
return false;
}
Expand Down

0 comments on commit f0830b3

Please sign in to comment.