68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace StgSharp.Script
|
|
{
|
|
public static class CodeLineHelper
|
|
{
|
|
public static string SliceAndTrim(this string line, int startIndex, int length)
|
|
{
|
|
if (line == null) throw new ArgumentNullException(nameof(line));
|
|
if (startIndex < 0 || startIndex >= line.Length)
|
|
throw new ArgumentOutOfRangeException(nameof(startIndex));
|
|
if (length < 0 || startIndex + length > line.Length)
|
|
throw new ArgumentOutOfRangeException(nameof(length));
|
|
|
|
ReadOnlySpan<char> sourceSpan = line.AsSpan();
|
|
ReadOnlySpan<char> subSpan = sourceSpan.Slice(startIndex, length);
|
|
|
|
int start = 0;
|
|
while (start < subSpan.Length && char.IsWhiteSpace(subSpan[start]))
|
|
{
|
|
start++;
|
|
}
|
|
int end = subSpan.Length - 1;
|
|
while (end >= start && char.IsWhiteSpace(subSpan[end]))
|
|
{
|
|
end--;
|
|
}
|
|
if (start > end)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
int finalLength = end - start + 1;
|
|
|
|
return new string(subSpan.Slice(start, finalLength));
|
|
}
|
|
|
|
public static string SliceAndTrim(this string line, int startIndex)
|
|
{
|
|
if (line == null) throw new ArgumentNullException(nameof(line));
|
|
if (startIndex < 0 || startIndex >= line.Length)
|
|
throw new ArgumentOutOfRangeException(nameof(startIndex));
|
|
|
|
ReadOnlySpan<char> sourceSpan = line.AsSpan();
|
|
ReadOnlySpan<char> subSpan = sourceSpan.Slice(startIndex);
|
|
|
|
int start = 0;
|
|
while (start < subSpan.Length && char.IsWhiteSpace(subSpan[start]))
|
|
{
|
|
start++;
|
|
}
|
|
int end = subSpan.Length - 1;
|
|
while (end >= start && char.IsWhiteSpace(subSpan[end]))
|
|
{
|
|
end--;
|
|
}
|
|
if (start > end)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
int finalLength = end - start + 1;
|
|
|
|
return new string(subSpan.Slice(start, finalLength));
|
|
}
|
|
}
|
|
}
|