Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jun 25, 2020
1 parent e463f1e commit 5a0fc33
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf.Test/ByteStringTest.cs
Expand Up @@ -181,7 +181,7 @@ public void GetEnumerator()
public void UnsafeFromBytes()
{
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
ByteString bs = ByteString.Unsafe.FromBytes(data.AsMemory(2, 3));
ByteString bs = ByteString.UnsafeFromBytes(data.AsMemory(2, 3));
ReadOnlySpan<byte> s = bs.Span;

Assert.AreEqual(3, s.Length);
Expand Down
45 changes: 14 additions & 31 deletions csharp/src/Google.Protobuf/ByteString.cs
Expand Up @@ -53,36 +53,14 @@ namespace Google.Protobuf
[SecuritySafeCritical]
public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString>
{
private static readonly ByteString empty;
private static readonly ByteString empty = new ByteString(new byte[0]);

#if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY
private readonly ReadOnlyMemory<byte> bytes;
#else
private readonly byte[] bytes;
#endif

static ByteString()
{
empty = new ByteString(new byte[0]);
}

#if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY
/// <summary>
/// Unsafe operations that can cause IO Failure and/or other catastrophic side-effects.
/// </summary>
public static class Unsafe
{
/// <summary>
/// Constructs a new ByteString from the given bytes. The bytes are
/// *not* copied, and must not be modified after this constructor is called.
/// </summary>
public static ByteString FromBytes(ReadOnlyMemory<byte> bytes)
{
return new ByteString(bytes);
}
}
#endif

/// <summary>
/// Internal use only. Ensure that the provided array is not mutated and belongs to this instance.
/// </summary>
Expand Down Expand Up @@ -142,10 +120,7 @@ public bool IsEmpty
/// </summary>
public ReadOnlySpan<byte> Span
{
get
{
return bytes.Span;
}
get { return bytes.Span; }
}

/// <summary>
Expand All @@ -154,10 +129,7 @@ public ReadOnlySpan<byte> Span
/// </summary>
public ReadOnlyMemory<byte> Memory
{
get
{
return bytes;
}
get { return bytes; }
}
#endif

Expand Down Expand Up @@ -197,6 +169,17 @@ public string ToBase64()
#endif
}

#if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY
/// <summary>
/// Constructs a new <see cref="ByteString" /> from the given bytes. The bytes are not copied,
/// and must not be modified while the <see cref="ByteString" /> is in use.
/// </summary>
public static ByteString UnsafeFromBytes(ReadOnlyMemory<byte> bytes)
{
return new ByteString(bytes);
}
#endif

/// <summary>
/// Constructs a <see cref="ByteString" /> from the Base64 Encoded String.
/// </summary>
Expand Down

0 comments on commit 5a0fc33

Please sign in to comment.