Created
August 31, 2023 08:41
-
-
Save AndrioCelos/1264f6a36ae9f9d96e76f7330bc8bbdc to your computer and use it in GitHub Desktop.
Sample project used to reproduce dotnet/roslyn#69663
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
<LangVersion>preview</LangVersion> | |
</PropertyGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Aiml.Tags; | |
internal class Program { | |
public static string TestMethod(Tuple tuple) => tuple[""]; | |
public static void Main() { | |
Console.WriteLine(TestMethod(new Tuple("", "Hello world"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
namespace Aiml; | |
/// <summary>Represents a set of variable bindings in the process of resolving a <see cref="Tags.Select"/> query.</summary> | |
/// <remarks>This class is represented as a singly-linked list.</remarks> | |
public class Tuple(string key, string value, Tuple? next) : IEnumerable<KeyValuePair<string, string>> { | |
public string Key { get; } = key; | |
public string Value { get; } = value; | |
public Tuple? Next { get; } = next; | |
public Tuple(string key, string value) : this(key, value, null) { } | |
public string? this[string key] { | |
get { | |
var tuple = this; | |
do { | |
if (key.Equals(tuple.Key, StringComparison.OrdinalIgnoreCase)) return tuple.Value; | |
tuple = tuple.Next; | |
} while (tuple != null); | |
return null; | |
} | |
} | |
/// <summary>Returns an encoded string containing the contents of the specified variables. The encoded string shall not contain spaces.</summary> | |
public string Encode(IReadOnlyCollection<string>? visibleVars) { | |
using var ms = new MemoryStream(); | |
using var writer = new BinaryWriter(ms); | |
foreach (var e in this) { | |
if (visibleVars is not null && !visibleVars.Contains(e.Key)) continue; | |
writer.Write(e.Key); | |
writer.Write(e.Value); | |
} | |
return Convert.ToBase64String(ms.GetBuffer(), 0, (int) ms.Position); | |
} | |
/// <summary>Returns the value of the specified variable from an encoded string, or <see langword="null"/> if the variable is not bound in the encoded string.</summary> | |
public static string? GetFromEncoded(string encoded, string key) { | |
var array = Convert.FromBase64String(encoded); | |
using var ms = new MemoryStream(array); | |
using var reader = new BinaryReader(ms); | |
while (ms.Position < ms.Length) { | |
var key2 = reader.ReadString(); | |
var value = reader.ReadString(); | |
if (key.Equals(key2, StringComparison.OrdinalIgnoreCase)) return value; | |
} | |
return null; | |
} | |
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() { | |
var tuple = this; | |
do { | |
yield return new(tuple.Key, tuple.Value); | |
tuple = tuple.Next; | |
} while (tuple != null); | |
} | |
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment