mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
1 changed files with 784842 additions and 0 deletions
42
Brutal.ImGui/ImString/ImString.ToString.cs
Normal file
42
Brutal.ImGui/ImString/ImString.ToString.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
namespace Brutal.ImGuiApi;
|
||||
|
||||
public ref partial struct ImString
|
||||
{
|
||||
/*
|
||||
* This partial provides helpers to interface with APIs that take regular C# strings.
|
||||
* Ideally should be removed in a stricter implementation, to avoid accidental conversions.
|
||||
* Also, the StringPool dictionary is naive, leaks etc.
|
||||
*/
|
||||
|
||||
// TODO: I would like to make the string conversion operator explicit,
|
||||
// unfortunately it is necessary to support ImString inside DefaultInterpolatedStringHandler...
|
||||
public static implicit operator string(in ImString s) => s.ToString();
|
||||
|
||||
private class StringPool
|
||||
{
|
||||
private readonly Dictionary<int, string> _utf8ToString = new();
|
||||
|
||||
public string ToString(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
var hash = new HashCode();
|
||||
hash.AddBytes(bytes);
|
||||
var key = hash.ToHashCode();
|
||||
if (!_utf8ToString.TryGetValue(key, out var toString))
|
||||
{
|
||||
toString = Utf8.GetString(bytes);
|
||||
_utf8ToString.Add(key, toString);
|
||||
}
|
||||
return toString;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly StringPool SharedStringPool = new();
|
||||
public static bool CacheToString = true;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (!CacheToString)
|
||||
return Utf8.GetString(_bytes);
|
||||
return SharedStringPool.ToString(_bytes);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue