Skip to content

Commit

Permalink
Toys
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbilas committed Nov 2, 2023
1 parent b862a8f commit 6b21a75
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Core/StringSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

namespace OkTools.Core;

// TODO: throw in some AggressiveInlining like they do in .net span

// note: this class isn't exactly like a Span<char> because it is always pointing into a string,
// and can move freely within the bounds of that. real Spans have no "outer context" to do this.
// so some of the functions may seem a little different (like WithStart goes from start of Text
// not from current Start - for that use Slice).
[PublicAPI]
[DebuggerDisplay("{ToDebugString()}")]
public readonly struct StringSpan : IEquatable<StringSpan>
Expand Down Expand Up @@ -47,6 +53,8 @@ public char this[int index]
}
}

public StringSpan Slice(int start, int end) => new(Text, Start + start, Start + end);

public StringSpan WithStart(int start) => new(Text, start, End);
public StringSpan WithOffsetStart(int offset) => new(Text, Start + offset, End);
public StringSpan WithEnd(int end) => new(Text, Start, end);
Expand Down Expand Up @@ -99,6 +107,18 @@ public string ToDebugString()

public override string ToString() => Text.Substring(Start, Length);

public int IndexOf(char value) => Span.IndexOf(value);
public int IndexOf(char value, int startIndex) => Span[startIndex..].IndexOf(value) + startIndex;
public int IndexOf(char value, int startIndex, int count) => Span[startIndex..count].IndexOf(value) + startIndex;

public bool TextEquals(StringSpan other)
{
if (Length != other.Length)
return false;

return string.Compare(Text, Start, other.Text, other.Start, Length, StringComparison.Ordinal) == 0;
}

public bool Equals(StringSpan other)
{
return Text == other.Text && Start == other.Start && End == other.End;
Expand Down

0 comments on commit 6b21a75

Please sign in to comment.