50 lines
1.2 KiB
C#
50 lines
1.2 KiB
C#
using Qrakhen.Qamp.Core.Collections;
|
|
using System.Text;
|
|
|
|
namespace Qrakhen.Qamp.Core.Values.Objects;
|
|
|
|
public class String(string? value) : Obj(ValueType.String)
|
|
{
|
|
private static readonly Register<uint, String> _strings = new();
|
|
|
|
public string? Value = value;
|
|
|
|
public override string ToString() => Value ?? "null";
|
|
|
|
public uint GetHash()
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
return 0;
|
|
|
|
return Value.GetHash();
|
|
}
|
|
|
|
public static Value Make(string? value)
|
|
{
|
|
String? str;
|
|
var hash = value?.GetHash() ?? 0;
|
|
if (!_strings.TryGet(hash, out str)) {
|
|
str = new String(value);
|
|
_strings.Add(hash, str);
|
|
}
|
|
|
|
return Create(str);
|
|
}
|
|
|
|
public static byte[] SerializeStrings()
|
|
{
|
|
List<byte> result = new();
|
|
foreach (var pair in _strings) {
|
|
String str = pair.Value;
|
|
if (str == null || str.__GC_Marked)
|
|
continue;
|
|
byte[] bytes;
|
|
bytes = Encoding.ASCII.GetBytes(str.Value!);
|
|
result.AddRange(pair.Key.GetBytes());
|
|
result.AddRange(bytes.Length.GetBytes());
|
|
result.AddRange(bytes);
|
|
result.Add(0);
|
|
}
|
|
return result.ToArray();
|
|
}
|
|
} |