-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPrintHtmlPipeWriter.cs
More file actions
46 lines (38 loc) · 1.17 KB
/
PrintHtmlPipeWriter.cs
File metadata and controls
46 lines (38 loc) · 1.17 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
using System.IO.Pipelines;
using System.Text;
namespace HyperTextExpression.AspNetCore;
public class PrintHtmlPipeWriter : IPrintHtml
{
private const int UTF8MaxByteLength = 6;
private readonly PipeWriter _pipeWriter;
private readonly Encoder _encoder;
public PrintHtmlPipeWriter(PipeWriter pipeWriter, Encoder encoder)
{
_pipeWriter = pipeWriter;
_encoder = encoder;
}
public void Write(char c)
{
var destination = _pipeWriter.GetSpan(1);
destination.Fill((byte)c);
_pipeWriter.Advance(1);
}
public void Write(char c, int count)
{
var size = 1 * count;
var destination = _pipeWriter.GetSpan(size);
destination.Fill((byte)c);
_pipeWriter.Advance(size);
}
public void Write(ReadOnlySpan<char> source)
{
var completed = false;
while (!completed)
{
var destination = _pipeWriter.GetSpan(UTF8MaxByteLength);
_encoder.Convert(source, destination, flush: true, out var charsUsed, out var bytesUsed, out completed);
_pipeWriter.Advance(bytesUsed);
source = source.Slice(charsUsed);
}
}
}