Skip to content

Commit

Permalink
Merge pull request dotnet#847 from KrzysztofCwalina/SmallApiFixes
Browse files Browse the repository at this point in the history
small API cleanup
  • Loading branch information
KrzysztofCwalina authored Sep 28, 2016
2 parents 4e9cfad + 8e24360 commit b64f611
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 17 deletions.
14 changes: 14 additions & 0 deletions src/System.Binary/System/Binary/BufferReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ namespace System.Binary
/// </remarks>
public static class BufferReader
{
/// <summary>
/// Reads a structure of type <typeparamref name="T"/> out of a span of bytes.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ReadBigEndian<[Primitive]T>(this ReadOnlySpan<byte> span) where T : struct
=> BitConverter.IsLittleEndian ? UnsafeUtilities.Reverse(span.Read<T>()) : span.Read<T>();

/// <summary>
/// Reads a structure of type <typeparamref name="T"/> out of a span of bytes.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ReadLittleEndian<[Primitive]T>(this ReadOnlySpan<byte> span) where T : struct
=> BitConverter.IsLittleEndian ? span.Read<T>() : UnsafeUtilities.Reverse(span.Read<T>());

/// <summary>
/// Reads a structure of type <typeparamref name="T"/> out of a span of bytes.
/// </summary>
Expand Down
13 changes: 6 additions & 7 deletions src/System.Slices/System/SpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using System.Runtime;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -557,7 +558,7 @@ public static int IndexOf<T>(this ReadOnlySpan<T> slice, T value)

// String helper methods, offering methods like String on Slice<char>:
// TODO(joe): culture-sensitive comparisons.
// TODO: should these move to satring related assembly
// TODO: should these move to string/text related assembly

public static bool Contains(this ReadOnlySpan<char> str, ReadOnlySpan<char> value)
{
Expand Down Expand Up @@ -672,15 +673,13 @@ public static bool StartsWith(this ReadOnlySpan<char> str, ReadOnlySpan<char> va

public unsafe static void Set(this Span<byte> bytes, byte* values, int length)
{
if (bytes.Length < length)
{
if (bytes.Length < length) {
throw new ArgumentOutOfRangeException("values");
}

// TODO(joe): specialize to use a fast memcpy if T is pointerless.
for (int i = 0; i < length; i++)
{
bytes[i] = values[i];
var valuesSpan = new Span<byte>(values, length);
if (!valuesSpan.TryCopyTo(bytes)) {
throw new Exception("internal error"); // this should never happen
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Append(string text)
}

//TODO: this should use Span<byte>
public void Append(Span<char> substring)
public void Append(ReadOnlySpan<char> substring)
{
for (int i = 0; i < substring.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace System.Text
internal static class IntegerFormatter
{
// TODO: format should be ReadOnlySpan<char>
internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> buffer, Span<char> format, EncodingData formattingData, out int bytesWritten)
internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Precondition.Require(numberOfBytes <= sizeof(long));

Expand Down Expand Up @@ -51,7 +51,7 @@ internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> b
}
}

internal static bool TryFormatUInt64(ulong value, byte numberOfBytes, Span<byte> buffer, Span<char> format, EncodingData formattingData, out int bytesWritten)
internal static bool TryFormatUInt64(ulong value, byte numberOfBytes, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Format.Parsed parsedFormat = Format.Parse(format);
return TryFormatUInt64(value, numberOfBytes, buffer, parsedFormat, formattingData, out bytesWritten);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Text
{
public static partial class PrimitiveFormatter
{
public static bool TryFormat(this double value, Span<byte> buffer, Span<char> format, EncodingData formattingData, out int bytesWritten)
public static bool TryFormat(this double value, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Format.Parsed parsedFormat = Format.Parse(format);
return TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten);
Expand All @@ -25,7 +25,7 @@ public static bool TryFormat(this double value, Span<byte> buffer, Format.Parsed
return FloatFormatter.TryFormatNumber(value, false, buffer, format, formattingData, out bytesWritten);
}

public static bool TryFormat(this float value, Span<byte> buffer, Span<char> format, EncodingData formattingData, out int bytesWritten)
public static bool TryFormat(this float value, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Format.Parsed parsedFormat = Format.Parse(format);
return TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static partial class PrimitiveFormatter
static readonly Format.Parsed t = new Format.Parsed('t');
const int FractionalTimeScale = 10000000;

public static bool TryFormat(this DateTimeOffset value, Span<byte> buffer, Span<char> format, EncodingData formattingData, out int bytesWritten)
public static bool TryFormat(this DateTimeOffset value, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Format.Parsed parsedFormat = Format.Parse(format);
return TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten);
Expand Down Expand Up @@ -60,7 +60,7 @@ public static bool TryFormat(this DateTimeOffset value, Span<byte> buffer, Forma
}
}

public static bool TryFormat(this DateTime value, Span<byte> buffer, Span<char> format, EncodingData formattingData, out int bytesWritten)
public static bool TryFormat(this DateTime value, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Format.Parsed parsedFormat = Format.Parse(format);
return TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten);
Expand Down Expand Up @@ -216,7 +216,7 @@ static bool TryFormatDateTimeRfc1123(DateTime value, Span<byte> buffer, Encoding
return true;
}

public static bool TryFormat(this TimeSpan value, Span<byte> buffer, Span<char> format, EncodingData formattingData, out int bytesWritten)
public static bool TryFormat(this TimeSpan value, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Format.Parsed parsedFormat = Format.Parse(format);
return TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Text
{
public static partial class PrimitiveFormatter
{
public static bool TryFormat(this Guid value, Span<byte> buffer, Span<char> format, EncodingData formattingData, out int bytesWritten)
public static bool TryFormat(this Guid value, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Format.Parsed parsedFormat = Format.Parse(format);
return TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten);
Expand Down
2 changes: 1 addition & 1 deletion src/System.Text.Primitives/System/Text/ParsedFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static Format.Parsed Parse(string format)
}

// TODO: format should be ReadOnlySpan<T>
public static Format.Parsed Parse(Span<char> format)
public static Format.Parsed Parse(ReadOnlySpan<char> format)
{
if (format.Length == 0)
{
Expand Down

0 comments on commit b64f611

Please sign in to comment.