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 sourceSpan = line.AsSpan(); ReadOnlySpan 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 sourceSpan = line.AsSpan(); ReadOnlySpan 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)); } } }