-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from Cysharp/hadashiA/update-msgpack
Add messagepack configurations
- Loading branch information
Showing
10 changed files
with
925 additions
and
667 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Text; | ||
using MessagePack; | ||
|
||
namespace ZLogger.MessagePack; | ||
|
||
public readonly struct MessagePackEncodedText : IEquatable<MessagePackEncodedText> | ||
{ | ||
static readonly Encoding StringEncoding = new UTF8Encoding(false); | ||
|
||
public string Value { get; private init; } | ||
public ReadOnlySpan<byte> Utf8EncodedValue => utf8EncodedValue; | ||
|
||
readonly byte[] utf8EncodedValue; | ||
|
||
MessagePackEncodedText(string value, byte[] utf8EncodedValue) | ||
{ | ||
Value = value; | ||
this.utf8EncodedValue = utf8EncodedValue; | ||
} | ||
|
||
public static MessagePackEncodedText Encode(string stringValue, MessagePackSerializerOptions? options = null) | ||
{ | ||
var oldSpec = options?.OldSpec ?? false; | ||
var characterLength = stringValue.Length; | ||
var bufferSize = StringEncoding.GetMaxByteCount(characterLength) + 5; | ||
|
||
var useOffset = characterLength switch | ||
{ | ||
<= MessagePackRange.MaxFixStringLength => 1, | ||
<= byte.MaxValue when !oldSpec => 2, | ||
<= ushort.MaxValue => 3, | ||
_ => 5 | ||
}; | ||
|
||
Span<byte> buffer = stackalloc byte[bufferSize]; | ||
var byteCount = StringEncoding.GetBytes(stringValue, buffer[useOffset..]); | ||
// move body and write prefix | ||
if (byteCount <= MessagePackRange.MaxFixStringLength) | ||
{ | ||
buffer[0] = (byte)(MessagePackCode.MinFixStr | byteCount); | ||
} | ||
else if (byteCount <= byte.MaxValue && !oldSpec) | ||
{ | ||
buffer[0] = MessagePackCode.Str8; | ||
buffer[1] = unchecked((byte)byteCount); | ||
} | ||
else if (byteCount <= ushort.MaxValue) | ||
{ | ||
buffer[0] = MessagePackCode.Str16; | ||
// Write big-endian | ||
buffer[1] = (byte)(byteCount >> 8); | ||
buffer[2] = (byte)byteCount; | ||
} | ||
else | ||
{ | ||
buffer[0] = MessagePackCode.Str32; | ||
buffer[1] = (byte)(byteCount >> 24); | ||
buffer[2] = (byte)(byteCount >> 16); | ||
buffer[3] = (byte)(byteCount >> 8); | ||
buffer[4] = (byte)byteCount; | ||
} | ||
|
||
var encodedValue = buffer[..(byteCount + useOffset)].ToArray(); | ||
return new MessagePackEncodedText(stringValue, encodedValue); | ||
} | ||
|
||
public bool Equals(MessagePackEncodedText other) => Value == other.Value; | ||
public override bool Equals(object? obj) => obj is MessagePackEncodedText other && Equals(other); | ||
public override int GetHashCode() => Value.GetHashCode(); | ||
} |
Oops, something went wrong.