mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-04 05:44:50 +00:00
Optimize StringSlice helpers
This commit is contained in:
@@ -51,12 +51,12 @@ namespace Markdig.Helpers
|
||||
/// <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.
|
||||
@@ -275,13 +275,7 @@ namespace Markdig.Helpers
|
||||
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>
|
||||
@@ -296,12 +290,7 @@ namespace Markdig.Helpers
|
||||
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>
|
||||
@@ -312,16 +301,15 @@ namespace Markdig.Helpers
|
||||
/// </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>
|
||||
@@ -330,16 +318,15 @@ namespace Markdig.Helpers
|
||||
/// <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>
|
||||
@@ -348,15 +335,15 @@ namespace Markdig.Helpers
|
||||
/// <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>
|
||||
@@ -364,8 +351,18 @@ namespace Markdig.Helpers
|
||||
/// </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>
|
||||
@@ -393,9 +390,12 @@ namespace Markdig.Helpers
|
||||
/// <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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user