about summary refs log tree commit diff
path: root/IrcTokens/StatefulEncoder.cs
blob: 0c8b5f98edfb5af66d4d4dfed538831f8cc893ec (plain) (blame)
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
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IrcTokens
{
    public class StatefulEncoder
    {
        private string _buffer;
        public EncodingInfo Encoding { get; set; }
        private List<Line> _bufferedLines;

        public string Pending => _buffer;

        public void Clear()
        {
            _buffer = "";
            _bufferedLines.Clear();
        }

        public void Push(Line line)
        {
            _buffer += $"{line.Format()}\r\n";
            _bufferedLines.Add(line);
        }

        public List<Line> Pop(int byteCount)
        {
            var sent = _buffer.Substring(byteCount).Count(c => c == '\n');
            _buffer = _buffer.Substring(byteCount);
            _bufferedLines = _bufferedLines.Skip(sent).ToList();
            return _bufferedLines.Take(sent).ToList();
        }
    }
}