Skip to content

Commit

Permalink
Remove Utf8Span (#312)
Browse files Browse the repository at this point in the history
in favor of simply using ReadOnlySpan<byte>.
  • Loading branch information
tmds authored Nov 10, 2024
1 parent 2a41d14 commit ad5a21a
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 53 deletions.
10 changes: 5 additions & 5 deletions src/Tmds.DBus.Protocol/MessageWriter.Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ref partial struct MessageWriter

public void WriteDouble(double value) => WritePrimitiveCore<double>(value, DBusType.Double);

public void WriteString(Utf8Span value) => WriteStringCore(value);
public void WriteString(ReadOnlySpan<byte> value) => WriteStringCore(value);

public void WriteString(string value) => WriteStringCore(value);

Expand All @@ -48,7 +48,7 @@ public void WriteSignature(string s)
WriteByte(0);
}

public void WriteObjectPath(Utf8Span value) => WriteStringCore(value);
public void WriteObjectPath(ReadOnlySpan<byte> value) => WriteStringCore(value);

public void WriteObjectPath(string value) => WriteStringCore(value);

Expand Down Expand Up @@ -108,19 +108,19 @@ public void WriteVariantDouble(double value)
WriteDouble(value);
}

public void WriteVariantString(Utf8Span value)
public void WriteVariantString(ReadOnlySpan<byte> value)
{
WriteSignature(ProtocolConstants.StringSignature);
WriteString(value);
}

public void WriteVariantSignature(Utf8Span value)
public void WriteVariantSignature(ReadOnlySpan<byte> value)
{
WriteSignature(ProtocolConstants.SignatureSignature);
WriteSignature(value);
}

public void WriteVariantObjectPath(Utf8Span value)
public void WriteVariantObjectPath(ReadOnlySpan<byte> value)
{
WriteSignature(ProtocolConstants.ObjectPathSignature);
WriteObjectPath(value);
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/MessageWriter.Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void WriteMethodCallHeader(

public void WriteMethodReturnHeader(
uint replySerial,
Utf8Span destination = default,
ReadOnlySpan<byte> destination = default,
string? signature = null)
{
ArrayStart start = WriteHeaderStart(MessageType.MethodReturn, MessageFlags.None);
Expand Down
14 changes: 5 additions & 9 deletions src/Tmds.DBus.Protocol/Reader.Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,15 @@ public unsafe double ReadDouble()
return value;
}

[Obsolete($"Use {nameof(ReadSignatureAsSpan)}.")]
public Utf8Span ReadSignature()
=> ReadSignatureAsSpan();

public Utf8Span ReadSignatureAsSpan()
public ReadOnlySpan<byte> ReadSignatureAsSpan()
{
int length = ReadByte();
return ReadSpan(length);
}

public void ReadSignature(string expected)
{
ReadOnlySpan<byte> signature = ReadSignatureAsSpan().Span;
ReadOnlySpan<byte> signature = ReadSignatureAsSpan();
if (signature.Length != expected.Length)
{
ThrowHelper.ThrowUnexpectedSignature(signature, expected);
Expand All @@ -91,13 +87,13 @@ public void ReadSignature(string expected)
}
}

public Utf8Span ReadObjectPathAsSpan() => ReadSpan();
public ReadOnlySpan<byte> ReadObjectPathAsSpan() => ReadSpan();

public ObjectPath ReadObjectPath() => new ObjectPath(ReadString());

public ObjectPath ReadObjectPathAsString() => ReadString();
public string ReadObjectPathAsString() => ReadString();

public Utf8Span ReadStringAsSpan() => ReadSpan();
public ReadOnlySpan<byte> ReadStringAsSpan() => ReadSpan();

public string ReadString() => Encoding.UTF8.GetString(ReadSpan());

Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.DBus.Protocol/Reader.Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public VariantValue ReadVariantValue()

private VariantValue ReadVariantValue(byte nesting)
{
Utf8Span signature = ReadSignatureAsSpan();
ReadOnlySpan<byte> signature = ReadSignatureAsSpan();
SignatureReader sigReader = new(signature);
if (!sigReader.TryRead(out DBusType type, out ReadOnlySpan<byte> innerSignature))
{
ThrowInvalidSignature($"Invalid variant signature: {signature.ToString()}");
ThrowInvalidSignature($"Invalid variant signature: {Encoding.UTF8.GetString(signature)}");
}
return ReadTypeAsVariantValue(type, innerSignature, nesting);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.DBus.Protocol/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ private static void EnsureSupportedVariantType(Type type)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Utf8Span GetSignature<T>(scoped Span<byte> buffer)
public static ReadOnlySpan<byte> GetSignature<T>(scoped Span<byte> buffer)
{
Debug.Assert(buffer.Length >= ProtocolConstants.MaxSignatureLength);

int bytesWritten = AppendTypeSignature(typeof(T), buffer);
return new Utf8Span(buffer.Slice(0, bytesWritten).ToArray());
return buffer.Slice(0, bytesWritten).ToArray();
}

private static int AppendTypeSignature(Type type, Span<byte> signature)
Expand Down
20 changes: 0 additions & 20 deletions src/Tmds.DBus.Protocol/Utf8Span.cs

This file was deleted.

18 changes: 9 additions & 9 deletions src/Tmds.DBus.Protocol/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,18 @@ public static Variant FromStruct<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(Struct
}

// Dictionary, Struct, Array.
private unsafe Variant(Utf8Span signature, IDBusWritable value)
private unsafe Variant(ReadOnlySpan<byte> signature, IDBusWritable value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
// Store the signature in the long if it is large enough.
if (signature.Span.Length <= 8)
if (signature.Length <= 8)
{
long l = 0;
Span<byte> span = new Span<byte>(&l, 8);
signature.Span.CopyTo(span);
signature.CopyTo(span);
if (BitConverter.IsLittleEndian)
{
l = BinaryPrimitives.ReverseEndianness(l);
Expand All @@ -274,8 +274,8 @@ private unsafe Variant(Utf8Span signature, IDBusWritable value)
}
else
{
_l = (long)signature.Span[0] << TypeShift;
_o = new ValueTuple<byte[], IDBusWritable>(signature.Span.ToArray(), value);
_l = (long)signature[0] << TypeShift;
_o = new ValueTuple<byte[], IDBusWritable>(signature.ToArray(), value);
}
}

Expand Down Expand Up @@ -407,21 +407,21 @@ internal unsafe void WriteTo(ref MessageWriter writer)
writer.WriteVariantObjectPath(GetObjectPath());
break;
case DBusType.Signature:
writer.WriteVariantSignature(new Utf8Span(GetSignature()));
writer.WriteVariantSignature(GetSignature());
break;
case DBusType.UnixFd:
writer.WriteVariantHandle(GetUnixFd());
break;

case DBusType.Array:
case DBusType.Struct:
Utf8Span signature;
ReadOnlySpan<byte> signature;
IDBusWritable writable;
if ((_l << 8) == 0)
{
// The signature is stored in the object.
var o = (ValueTuple<byte[], IDBusWritable>)_o!;
signature = new Utf8Span(o.Item1);
signature = o.Item1;
writable = o.Item2;
}
else
Expand All @@ -438,7 +438,7 @@ internal unsafe void WriteTo(ref MessageWriter writer)
{
length = 8;
}
signature = new Utf8Span(span.Slice(0, length));
signature = span.Slice(0, length);
writable = (_o as IDBusWritable)!;
}
writer.WriteSignature(signature);
Expand Down
6 changes: 3 additions & 3 deletions src/Tmds.DBus.Protocol/VariantValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,17 +1093,17 @@ internal string Signature
get
{
Span<byte> span = stackalloc byte[ProtocolConstants.MaxSignatureLength];
return GetSignature(span).ToString();
return Encoding.UTF8.GetString(GetSignature(span));
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Utf8Span GetSignature(scoped Span<byte> buffer)
private ReadOnlySpan<byte> GetSignature(scoped Span<byte> buffer)
{
Debug.Assert(buffer.Length >= ProtocolConstants.MaxSignatureLength);

int bytesWritten = AppendTypeSignature(buffer);
return new Utf8Span(buffer.Slice(0, bytesWritten).ToArray());
return buffer.Slice(0, bytesWritten).ToArray();
}

private int AppendTypeSignature(Span<byte> signature)
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Tool/MonitorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ DBusType.Double or
addNewLine = false;
break;
case DBusType.Variant:
innerSignature = reader.ReadSignature().Span;
innerSignature = reader.ReadSignatureAsSpan();
sigReader = new(innerSignature);
sigReader.TryRead(out type, out innerSignature);
AppendValue(sb, indent, ref reader, type, innerSignature, isDictEntryValue: isDictEntryValue);
Expand Down
2 changes: 1 addition & 1 deletion test/Tmds.DBus.Protocol.Tests/ReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void ReadSignature(string expected, int alignment, byte[] bigEndianData,
new byte[] { 4, 0, 0, 0, (byte)'/', (byte)'a', (byte)'/', (byte)'b', 0 })]
public void ReadObjectPath(string expected, int alignment, byte[] bigEndianData, byte[] littleEndianData)
{
TestRead(expected, (ref Reader reader) => reader.ReadObjectPathAsSpan().ToString(), alignment, bigEndianData, littleEndianData);
TestRead(expected, (ref Reader reader) => reader.ReadObjectPathAsString(), alignment, bigEndianData, littleEndianData);
}

[Theory]
Expand Down

0 comments on commit ad5a21a

Please sign in to comment.