mirror of
https://github.com/8damon/Blackbird-Platform
synced 2026-06-21 13:41:12 +00:00
53 lines
1.2 KiB
C#
53 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BlackbirdInterface
|
|
{
|
|
internal sealed class BoundedStringPool
|
|
{
|
|
private readonly Dictionary<string, string> _values = new(StringComparer.Ordinal);
|
|
private readonly int _maxEntries;
|
|
|
|
public BoundedStringPool(int maxEntries)
|
|
{
|
|
_maxEntries = maxEntries < 64 ? 64 : maxEntries;
|
|
}
|
|
|
|
public int Count => _values.Count;
|
|
|
|
public string Intern(string? value, int maxLength)
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
if (value.Length > maxLength)
|
|
{
|
|
return value;
|
|
}
|
|
|
|
if (_values.TryGetValue(value, out string? existing))
|
|
{
|
|
return existing;
|
|
}
|
|
|
|
if (_values.Count >= _maxEntries)
|
|
{
|
|
// Roll over: discard all interned strings and start fresh rather than
|
|
// silently bypassing the pool for the rest of the session.
|
|
_values.Clear();
|
|
}
|
|
|
|
_values[value] = value;
|
|
return value;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_values.Clear();
|
|
}
|
|
}
|
|
}
|
|
|