Migrate existing code
This commit is contained in:
parent
384ff4a6f3
commit
9b49f880a2
30 changed files with 1789 additions and 5 deletions
63
Integrations/Kea/KeaDhcpLease.cs
Normal file
63
Integrations/Kea/KeaDhcpLease.cs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
using System.Buffers;
|
||||
using System.Globalization;
|
||||
|
||||
using DotNext.Buffers;
|
||||
|
||||
using CommunityToolkit.HighPerformance.Buffers;
|
||||
|
||||
namespace DotNetDDI.Integrations.Kea;
|
||||
|
||||
public static class KeaDhcpLease
|
||||
{
|
||||
private static ReadOnlySpan<char> EscapeTag => ['&', '#', 'x'];
|
||||
|
||||
// ref: https://github.com/isc-projects/kea/blob/Kea-2.5.3/src/lib/util/csv_file.cc#L479
|
||||
public static string Unescape(in ReadOnlySpan<char> text)
|
||||
{
|
||||
return text.IndexOf(EscapeTag) switch
|
||||
{
|
||||
-1 => text.ToString(),
|
||||
int i => SlowPath(i, text)
|
||||
};
|
||||
|
||||
static string SlowPath(int esc_pos, in ReadOnlySpan<char> text)
|
||||
{
|
||||
SpanReader<char> reader = new(text);
|
||||
using ArrayPoolBufferWriter<char> writer = new(text.Length);
|
||||
while (reader.RemainingCount > 0)
|
||||
{
|
||||
writer.Write(reader.Read(esc_pos));
|
||||
reader.Advance(EscapeTag.Length);
|
||||
|
||||
bool converted = false;
|
||||
char escapedChar = default;
|
||||
if (reader.RemainingCount >= 2)
|
||||
{
|
||||
if (byte.TryParse(reader.RemainingSpan[..2], NumberStyles.AllowHexSpecifier, null, out var b))
|
||||
{
|
||||
converted = true;
|
||||
escapedChar = (char)b;
|
||||
reader.Advance(2);
|
||||
}
|
||||
}
|
||||
|
||||
if (converted)
|
||||
{
|
||||
writer.Write([escapedChar]);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.Write(EscapeTag);
|
||||
}
|
||||
|
||||
esc_pos = reader.RemainingSpan.IndexOf(EscapeTag);
|
||||
if (esc_pos == -1)
|
||||
{
|
||||
writer.Write(reader.ReadToEnd());
|
||||
}
|
||||
}
|
||||
|
||||
return writer.WrittenSpan.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue