Skip to content

Commit

Permalink
Set up new methods for reading strings with a specific encoding and r…
Browse files Browse the repository at this point in the history
…eading schema objects into a span.
  • Loading branch information
MeltyPlayer committed Nov 3, 2023
1 parent 2cbae29 commit 84dfeee
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void TestWriteAndReadArrayValues() {

var er = new SchemaBinaryReader(ms, endianness);
er.Position = 1;
var actualSws = er.ReadNewArray<SchemaClass>(expectedSw.Values.Length);
var actualSws = er.ReadNews<SchemaClass>(expectedSw.Values.Length);
Assert.True(expectedSw.Values.SequenceEqual(actualSws));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void TestWriteAndReadArrayValues() {

var er = new SchemaBinaryReader(ms, endianness);
er.Position = 1;
var actualSws = er.ReadNewArray<SchemaStruct>(expectedSw.Values.Length);
var actualSws = er.ReadNews<SchemaStruct>(expectedSw.Values.Length);
Assert.True(expectedSw.Values.SequenceEqual(actualSws));
}

Expand Down
34 changes: 27 additions & 7 deletions Schema/src/binary/reader/IBinaryReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;

using schema.binary.attributes;

Expand Down Expand Up @@ -122,31 +123,50 @@ void ReadChars(StringEncodingType encodingType,

void ReadChars(StringEncodingType encodingType, Span<char> dst);

void AssertChar(Encoding encoding, char expectedValue);
char ReadChar(Encoding encoding);
char[] ReadChars(Encoding encoding, long count);

void ReadChars(Encoding encoding,
char[] dst,
int start,
int length);

void ReadChars(Encoding encoding, Span<char> dst);

string ReadUpTo(char endToken);
string ReadUpTo(StringEncodingType encodingType, char endToken);
string ReadUpTo(Encoding encoding, char endToken);

string ReadUpTo(ReadOnlySpan<string> endTokens);
string ReadUpTo(StringEncodingType encodingType, ReadOnlySpan<string> endTokens);

string ReadUpTo(StringEncodingType encodingType,
ReadOnlySpan<string> endTokens);

string ReadUpTo(Encoding encoding, ReadOnlySpan<string> endTokens);

string ReadLine();

Check warning on line 148 in Schema/src/binary/reader/IBinaryReader.cs

View workflow job for this annotation

GitHub Actions / build

'IBinaryReader.ReadLine()' hides inherited member 'IDataReader.ReadLine()'. Use the new keyword if hiding was intended.
string ReadLine(StringEncodingType encodingType);
string ReadLine(Encoding encoding);

void AssertString(StringEncodingType encodingType, string expectedValue);
string ReadString(StringEncodingType encodingType, long count);

void AssertString(Encoding encoding, string expectedValue);
string ReadString(Encoding encoding, long count);

void AssertStringNT(string expectedValue);
string ReadStringNT();

void AssertStringNT(StringEncodingType encodingType, string expectedValue);
string ReadStringNT(StringEncodingType encodingType);

T ReadNew<T>() where T : IBinaryDeserializable, new();
void AssertStringNT(Encoding encoding, string expectedValue);
string ReadStringNT(Encoding encoding);

T ReadNew<T>() where T : IBinaryDeserializable, new();
bool TryReadNew<T>(out T? value) where T : IBinaryDeserializable, new();

void ReadNewArray<T>(out T[] array, int length)
where T : IBinaryDeserializable, new();

T[] ReadNewArray<T>(int length) where T : IBinaryDeserializable, new();
T[] ReadNews<T>(int length) where T : IBinaryDeserializable, new();
void ReadNews<T>(Span<T> dst) where T : IBinaryDeserializable, new();
}
}
18 changes: 10 additions & 8 deletions Schema/src/binary/reader/SchemaBinaryReader_New.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System;
using System.Runtime.CompilerServices;


namespace schema.binary {
Expand All @@ -25,19 +26,20 @@ public bool TryReadNew<T>(out T? value)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadNewArray<T>(out T[] array, int length)
public T[] ReadNews<T>(int length)
where T : IBinaryDeserializable, new() {
array = ReadNewArray<T>(length);
var array = new T[length];
this.ReadNews<T>(array);
return array;
}

public T[] ReadNewArray<T>(int length)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadNews<T>(Span<T> dst)
where T : IBinaryDeserializable, new() {
var array = new T[length];
for (var i = 0; i < length; ++i) {
for (var i = 0; i < dst.Length; ++i) {
this.AssertNotEof();
array[i] = this.ReadNew<T>();
dst[i] = this.ReadNew<T>();
}
return array;
}
}
}
103 changes: 79 additions & 24 deletions Schema/src/binary/reader/SchemaBinaryReader_Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,67 @@ public void ReadChars(Span<char> dst) {
}


// Encoded Chars
// Enum encoded Chars
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AssertChar(StringEncodingType encodingType, char expectedValue)
=> SchemaBinaryReader.Assert_(expectedValue, this.ReadChar(encodingType));

public unsafe char ReadChar(StringEncodingType encodingType) {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char ReadChar(StringEncodingType encodingType)
=> this.ReadChar(encodingType.GetEncoding(this.Endianness));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char[] ReadChars(StringEncodingType encodingType, long count)
=> this.ReadChars(encodingType.GetEncoding(this.Endianness), count);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadChars(StringEncodingType encodingType,
char[] dst,
int start,
int length)
=> this.ReadChars(encodingType.GetEncoding(this.Endianness),
dst,
start,
length);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadChars(StringEncodingType encodingType, Span<char> dst)
=> this.ReadChars(encodingType.GetEncoding(this.Endianness), dst);


// Encoded Chars
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AssertChar(Encoding encoding, char expectedValue)
=> SchemaBinaryReader.Assert_(expectedValue, this.ReadChar(encoding));

public unsafe char ReadChar(Encoding encoding) {
char c;
var ptr = &c;
this.ReadChars(encodingType, new Span<char>(ptr, 1));
this.ReadChars(encoding, new Span<char>(ptr, 1));
return c;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char[] ReadChars(StringEncodingType encodingType, long count) {
public char[] ReadChars(Encoding encoding, long count) {
var newArray = new char[count];
this.ReadChars(encodingType, newArray);
this.ReadChars(encoding, newArray);
return newArray;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadChars(StringEncodingType encodingType,
public void ReadChars(Encoding encoding,
char[] dst,
int start,
int length)
=> this.ReadChars(encodingType, dst.AsSpan(start, length));
=> this.ReadChars(encoding, dst.AsSpan(start, length));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void ReadChars(StringEncodingType encodingType,
Span<char> dst) {
public unsafe void ReadChars(Encoding encoding, Span<char> dst) {
if (dst.Length == 0) {
return;
}

var basePosition = this.Position;

var encoding = encodingType.GetEncoding(this.Endianness);
var maxByteCount = encoding.GetMaxByteCount(dst.Length);

Span<byte> buffer = stackalloc byte[maxByteCount];
Expand Down Expand Up @@ -121,10 +146,14 @@ public string ReadUpTo(char endToken) {
return strBuilder.ToString();
}

public string ReadUpTo(StringEncodingType encodingType, char endToken) {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadUpTo(StringEncodingType encodingType, char endToken)
=> this.ReadUpTo(encodingType.GetEncoding(this.Endianness), endToken);

public string ReadUpTo(Encoding encoding, char endToken) {
var strBuilder = new StringBuilder();
while (!this.Eof) {
var c = this.ReadChar(encodingType);
var c = this.ReadChar(encoding);
if (c == endToken) {
break;
}
Expand All @@ -135,7 +164,6 @@ public string ReadUpTo(StringEncodingType encodingType, char endToken) {
return strBuilder.ToString();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadUpTo(ReadOnlySpan<string> endTokens) {
var strBuilder = new StringBuilder();
while (!Eof) {
Expand Down Expand Up @@ -163,17 +191,21 @@ public string ReadUpTo(ReadOnlySpan<string> endTokens) {
return strBuilder.ToString();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadUpTo(StringEncodingType encodingType,
ReadOnlySpan<string> endTokens) {
ReadOnlySpan<string> endTokens)
=> this.ReadUpTo(encodingType.GetEncoding(this.Endianness), endTokens);

public string ReadUpTo(Encoding encoding, ReadOnlySpan<string> endTokens) {
var strBuilder = new StringBuilder();
while (!Eof) {
var firstC = this.ReadChar(encodingType);
var firstC = this.ReadChar(encoding);
var originalOffset = Position;

foreach (var endToken in endTokens) {
if (firstC == endToken[0]) {
for (var i = 1; i < endToken.Length; ++i) {
var c = this.ReadChar(encodingType);
var c = this.ReadChar(encoding);
if (c != endToken[1]) {
Position = originalOffset;
break;
Expand All @@ -200,7 +232,11 @@ public string ReadLine()

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadLine(StringEncodingType encodingType)
=> this.ReadUpTo(encodingType, TextReaderConstants.NEWLINE_STRINGS);
=> this.ReadLine(encodingType.GetEncoding(this.Endianness));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadLine(Encoding encoding)
=> this.ReadUpTo(encoding, TextReaderConstants.NEWLINE_STRINGS);


// String
Expand All @@ -213,9 +249,14 @@ public void AssertString(string expectedValue)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AssertString(StringEncodingType encodingType,
string expectedValue)
=> this.AssertString(encodingType.GetEncoding(this.Endianness),
expectedValue);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AssertString(Encoding encoding, string expectedValue)
=> SchemaBinaryReader.AssertStrings_(
expectedValue.AsSpan().TrimEnd('\0'),
this.ReadString(encodingType, expectedValue.Length).AsSpan());
this.ReadString(encoding, expectedValue.Length).AsSpan());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(long count) {
Expand All @@ -225,9 +266,13 @@ public string ReadString(long count) {
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(StringEncodingType encodingType, long count) {
public string ReadString(StringEncodingType encodingType, long count)
=> this.ReadString(encodingType.GetEncoding(this.Endianness), count);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(Encoding encoding, long count) {
Span<char> buffer = stackalloc char[(int) count];
this.ReadChars(encodingType, buffer);
this.ReadChars(encoding, buffer);
return ((ReadOnlySpan<char>) buffer).TrimEnd('\0').ToString();
}

Expand All @@ -246,13 +291,23 @@ public void AssertStringNT(string expectedValue)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AssertStringNT(StringEncodingType encodingType,
string expectedValue)
=> this.AssertStringNT(encodingType.GetEncoding(this.Endianness),
expectedValue);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadStringNT(StringEncodingType encodingType)
=> this.ReadStringNT(encodingType.GetEncoding(this.Endianness));


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AssertStringNT(Encoding encoding, string expectedValue)
=> SchemaBinaryReader.AssertStrings_(
expectedValue.AsSpan(),
// TODO: Consider removing an allocation here
this.ReadStringNT(encodingType).AsSpan());
this.ReadStringNT(encoding).AsSpan());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadStringNT(StringEncodingType encodingType)
=> this.ReadUpTo(encodingType, '\0');
public string ReadStringNT(Encoding encoding)
=> this.ReadUpTo(encoding, '\0');
}
}
4 changes: 2 additions & 2 deletions Schema/src/text/reader/ITextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ string[] ReadStrings(ReadOnlySpan<string> separators,

bool TryReadNew<T>(out T? value) where T : ITextDeserializable, new();

void ReadNewArray<T>(out T[] array, int length)
void ReadNews<T>(out T[] array, int length)
where T : ITextDeserializable, new();

T[] ReadNewArray<T>(int length) where T : ITextDeserializable, new();
T[] ReadNews<T>(int length) where T : ITextDeserializable, new();
}
}
6 changes: 3 additions & 3 deletions Schema/src/text/reader/SchemaTextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public bool TryReadNew<T>(out T? value)
return success;
}

public void ReadNewArray<T>(out T[] array, int length)
public void ReadNews<T>(out T[] array, int length)
where T : ITextDeserializable, new()
=> array = this.ReadNewArray<T>(length);
=> array = this.ReadNews<T>(length);

public T[] ReadNewArray<T>(int length)
public T[] ReadNews<T>(int length)
where T : ITextDeserializable, new() {
var array = new T[length];
for (var i = 0; i < length; ++i) {
Expand Down

0 comments on commit 84dfeee

Please sign in to comment.