daily update on 20240203 by nitload youmu, ExpComment[D[D[D[D[D[D[D [C[C[C[C[C[C[D[D[D[D[D[DComment Splitter ok, case code(* comment[D[D[D[D[D[D[D[D[D[D[C[C[[C[Dcomment*)code--comment and comment*)code--comment not added, may use span optimize all programme
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user