// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text; namespace EpicGames.Core { /// /// View of a character string. Allows comparing/manipulating substrings without unnecessary memory allocatinos. /// public readonly struct StringView : IEquatable { /// /// Memory containing the characters /// public ReadOnlyMemory Memory { get; } /// /// Span for the sequence of characters /// public ReadOnlySpan Span { get { return Memory.Span; } } /// /// Constructor /// /// The memory containing the characters public StringView(ReadOnlyMemory Memory) { this.Memory = Memory; } /// /// Constructor /// /// String to construct from public StringView(string Text) { Memory = Text.AsMemory(); } /// /// Constructs a view onto a substring of the given string /// /// String to construct from /// Offset within the string for this view public StringView(string Text, int Index) { Memory = Text.AsMemory(Index); } /// /// Constructs a view onto a substring of the given string /// /// String to construct from /// Offset within the string for this view /// Number of characters to include public StringView(string Text, int Index, int Count) { Memory = Text.AsMemory(Index, Count); } /// /// Equality comparer /// /// First string to compare /// Second string to compare /// True if the strings are equal public static bool operator ==(StringView X, StringView Y) { return X.Equals(Y); } /// /// Inequality comparer /// /// First string to compare /// Second string to compare /// True if the strings are equal public static bool operator !=(StringView X, StringView Y) { return !X.Equals(Y); } /// public override bool Equals(object? Obj) { return Obj is StringView && Equals((StringView)Obj); } /// public override int GetHashCode() { return String.GetHashCode(Memory.Span); } /// public override string ToString() { return new string(Memory.Span); } /// public bool Equals(StringView Other) { return Equals(Other, StringComparison.CurrentCulture); } /// public bool Equals(StringView? Other, StringComparison comparisonType) { return Other.HasValue && Memory.Span.Equals(Other.Value.Memory.Span, comparisonType); } /// /// Implicit conversion operator from a regular string /// /// The string to construct from public static implicit operator StringView(string Text) => new StringView(Text); } /// /// Comparer for StringView objects /// public class StringViewComparer : IComparer, IEqualityComparer { /// /// Static instance of an ordinal StringView comparer /// public static StringViewComparer Ordinal = new StringViewComparer(StringComparison.Ordinal); /// /// Static instance of an ordinal StringView comparer which ignores case /// public static StringViewComparer OrdinalIgnoreCase = new StringViewComparer(StringComparison.OrdinalIgnoreCase); /// /// The comparison type /// public StringComparison ComparisonType { get; } /// /// Constructor /// /// Type of comparison to perform public StringViewComparer(StringComparison ComparisonType) { this.ComparisonType = ComparisonType; } /// public bool Equals(StringView X, StringView Y) { return X.Span.Equals(Y.Span, ComparisonType); } /// public int GetHashCode(StringView obj) { return String.GetHashCode(obj.Span); } /// public int Compare(StringView X, StringView Y) { return X.Span.CompareTo(Y.Span, ComparisonType); } } }