-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathExtensions.cs
More file actions
73 lines (64 loc) · 2.38 KB
/
Extensions.cs
File metadata and controls
73 lines (64 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.IO;
using System.IO.Pipelines;
using System.Text;
using System.Threading.Tasks;
using BencodeNET.IO;
using BencodeNET.Objects;
using BencodeNET.Parsing;
using NSubstitute.Core;
namespace BencodeNET.Tests
{
internal static class Extensions
{
internal static string AsString(this Stream stream)
{
stream.Position = 0;
var sr = new StreamReader(stream, Encoding.UTF8);
return sr.ReadToEnd();
}
internal static string AsString(this Stream stream, Encoding encoding)
{
stream.Position = 0;
var sr = new StreamReader(stream, encoding);
return sr.ReadToEnd();
}
internal static void SkipBytes(this BencodeReader reader, int length)
{
reader.Read(new byte[length]);
}
internal static Task SkipBytesAsync(this PipeBencodeReader reader, int length)
{
return reader.ReadAsync(new byte[length]).AsTask();
}
internal static ConfiguredCall AndSkipsAhead(this ConfiguredCall call, int length)
{
return call.AndDoes(x => x.Arg<BencodeReader>().SkipBytes(length));
}
internal static ConfiguredCall AndSkipsAheadAsync(this ConfiguredCall call, int length)
{
return call.AndDoes(async x => await x.Arg<PipeBencodeReader>().SkipBytesAsync(length));
}
internal static async ValueTask<IBObject> ParseStringAsync(this IBObjectParser parser, string bencodedString)
{
var bytes = Encoding.UTF8.GetBytes(bencodedString).AsMemory();
var (reader, writer) = new Pipe();
await writer.WriteAsync(bytes);
writer.Complete();
return await parser.ParseAsync(reader);
}
internal static async ValueTask<T> ParseStringAsync<T>(this IBObjectParser<T> parser, string bencodedString) where T : IBObject
{
var bytes = Encoding.UTF8.GetBytes(bencodedString).AsMemory();
var (reader, writer) = new Pipe();
await writer.WriteAsync(bytes);
writer.Complete();
return await parser.ParseAsync(reader);
}
internal static void Deconstruct(this Pipe pipe, out PipeReader reader, out PipeWriter writer)
{
reader = pipe.Reader;
writer = pipe.Writer;
}
}
}