Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现MiraiCode #47

Draft
wants to merge 1 commit into
base: preview/1.0.2.2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Mirai-CSharp/Mirai-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.1;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Mirai_CSharp</RootNamespace>
<OutputType>Library</OutputType>
<NoWin32Manifest>true</NoWin32Manifest>
Expand Down
102 changes: 102 additions & 0 deletions Mirai-CSharp/Utility/MiraiCodeReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Mirai_CSharp.Utility
{
//see https://github.com/mamoe/mirai/blob/dev/docs/Messages.md#mirai-%E7%A0%81
public ref struct MiraiCodeReader
{
public delegate void NameHandler(ReadOnlySpan<char> name);

public delegate void ArgumentHandler(ReadOnlySpan<char> argument);

private readonly ReadOnlySpan<char> _Code;

private int _Index;

private ReadOnlySpan<char> Current => GetFromIndex(_Code, _Index);

public MiraiCodeReader(ReadOnlySpan<char> code)
{
_Code = code;
_Index = 0;
}

public void Parse(NameHandler nameHandler, ArgumentHandler argumentHandler)
{
//[mirai:
ReadOnlySpan<char> code = Current;
if (code.Length < 9)
{
throw new FormatException();
}
if (!GetFromLength(code, 7).SequenceEqual("[mirai:".AsSpan()))
{
throw new FormatException();
}
if (!FindBlock(GetFromIndex(code, 7), ']', out code))
{
throw new FormatException("Missing end of char ]");
}
//[mirai:......]
_Index += code.Length + 7;
if (!FindBlock(code, ':', out ReadOnlySpan<char> result))
{
if (code.Length - 1 == 0)
{
throw new FormatException("Missing name");
}
nameHandler(GetFromLength(code, code.Length - 1));
return;
}
nameHandler(GetFromLength(result, result.Length - 1));
while (FindBlock(code = GetFromIndex(code, result.Length), ':', out result))
{
argumentHandler(GetFromLength(result, result.Length - 1));
}
argumentHandler(GetFromLength(code, code.Length - 1)); // do not check empty argument
}

private static unsafe bool FindBlock(ReadOnlySpan<char> input, char seperator, out ReadOnlySpan<char> span)
{
int end = 0;
ReadOnlySpan<char> code;
do
{
code = CreateReadOnlySpan(ref Unsafe.Add(ref MemoryMarshal.GetReference(input), end), input.Length - end);
int result = code.IndexOf(seperator);
if (result == -1)
{
span = default;
return false;
}
end += result;
}
while (Unsafe.Add(ref MemoryMarshal.GetReference(code), end++ - 1) == '\\');
span = CreateReadOnlySpan(ref MemoryMarshal.GetReference(input), end);
return true;
}

private static unsafe ReadOnlySpan<char> GetFromIndex(ReadOnlySpan<char> span, int index)
{
return CreateReadOnlySpan(ref Unsafe.Add(ref MemoryMarshal.GetReference(span), index), span.Length - index);
}

private static unsafe ReadOnlySpan<char> GetFromLength(ReadOnlySpan<char> span, int length)
{
return CreateReadOnlySpan(ref MemoryMarshal.GetReference(span), length);
}
#if !NETSTANDARD2_0
private static unsafe ReadOnlySpan<char> CreateReadOnlySpan(ref char c, int length)
{
return MemoryMarshal.CreateReadOnlySpan(ref c, length);
}
#else
private static unsafe ReadOnlySpan<char> CreateReadOnlySpan(ref char c, int length)
{
return new ReadOnlySpan<char>(Unsafe.AsPointer(ref c), length);
}
#endif
}
}