about summary refs log tree commit diff
path: root/IrcTokens
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2020-04-28 21:07:23 -0400
committerBen Harris <ben@tilde.team>2020-04-28 21:07:23 -0400
commit8403fd8d0fb0ac3c69e90f530fda331ea2ab639b (patch)
tree6504febcc485c6335b8cf5e333fa0437229b0704 /IrcTokens
parent6a0b8dc22fc86a5de37278231f3f8418afc9b836 (diff)
Fix example
Diffstat (limited to 'IrcTokens')
-rw-r--r--IrcTokens/Extensions.cs5
-rw-r--r--IrcTokens/StatefulDecoder.cs9
-rw-r--r--IrcTokens/StatefulEncoder.cs14
-rw-r--r--IrcTokens/Tests/StatefulDecoder.cs6
4 files changed, 20 insertions, 14 deletions
diff --git a/IrcTokens/Extensions.cs b/IrcTokens/Extensions.cs
index 2eee7dc..4b23774 100644
--- a/IrcTokens/Extensions.cs
+++ b/IrcTokens/Extensions.cs
@@ -36,10 +36,13 @@ namespace IrcTokens
 
         public static byte[] Trim(this IEnumerable<byte> bytes, byte separator)
         {
-            if (bytes == null || !bytes.Any()) return Array.Empty<byte>();
+            if (bytes == null) return Array.Empty<byte>();
+
             var byteList = new List<byte>(bytes);
             var i        = 0;
 
+            if (!byteList.Any()) return byteList.ToArray();
+
             while (byteList[i] == separator)
             {
                 byteList.RemoveAt(i);
diff --git a/IrcTokens/StatefulDecoder.cs b/IrcTokens/StatefulDecoder.cs
index 62d1703..b2bad5e 100644
--- a/IrcTokens/StatefulDecoder.cs
+++ b/IrcTokens/StatefulDecoder.cs
@@ -49,14 +49,15 @@ namespace IrcTokens
 
         public List<Line> Push(string data)
         {
-            return Push(Encoding.GetBytes(data));
+            var bytes = Encoding.GetBytes(data);
+            return Push(bytes, bytes.Length);
         }
 
-        public List<Line> Push(byte[] data)
+        public List<Line> Push(byte[] data, int bytesReceived)
         {
-            if (data == null || data.Length == 0) return null;
+            if (data == null) return null;
 
-            _buffer = _buffer == null ? Array.Empty<byte>() : _buffer.Concat(data).ToArray();
+            _buffer = _buffer == null ? Array.Empty<byte>() : _buffer.Concat(data.Take(bytesReceived)).ToArray();
 
             var listLines = _buffer.Split((byte) '\n').Select(l => l.Trim((byte) '\r')).ToList();
             _buffer = listLines.Last();
diff --git a/IrcTokens/StatefulEncoder.cs b/IrcTokens/StatefulEncoder.cs
index b486736..57f1b96 100644
--- a/IrcTokens/StatefulEncoder.cs
+++ b/IrcTokens/StatefulEncoder.cs
@@ -7,7 +7,7 @@ namespace IrcTokens
 {
     public class StatefulEncoder
     {
-        private Queue<Line> _bufferedLines;
+        private List<Line> _bufferedLines;
         private Encoding _encoding;
 
         public StatefulEncoder()
@@ -45,7 +45,7 @@ namespace IrcTokens
         public void Clear()
         {
             PendingBytes   = Array.Empty<byte>();
-            _bufferedLines = new Queue<Line>();
+            _bufferedLines = new List<Line>();
         }
 
         public void Push(Line line)
@@ -53,7 +53,7 @@ namespace IrcTokens
             if (line == null) throw new ArgumentNullException(nameof(line));
 
             PendingBytes = PendingBytes.Concat(Encoding.GetBytes($"{line.Format()}\r\n")).ToArray();
-            _bufferedLines.Enqueue(line);
+            _bufferedLines.Add(line);
         }
 
         public List<Line> Pop(int byteCount)
@@ -61,11 +61,11 @@ namespace IrcTokens
             var sent = PendingBytes.Take(byteCount).Count(c => c == '\n');
 
             PendingBytes   = PendingBytes.Skip(byteCount).ToArray();
-            _bufferedLines = new Queue<Line>(_bufferedLines.Take(sent));
+            
+            var sentLines = _bufferedLines.Take(sent).ToList();
+            _bufferedLines = _bufferedLines.Skip(sent).ToList();
 
-            return Enumerable.Range(0, sent)
-                .Select(_ => _bufferedLines.Dequeue())
-                .ToList();
+            return sentLines;
         }
     }
 }
diff --git a/IrcTokens/Tests/StatefulDecoder.cs b/IrcTokens/Tests/StatefulDecoder.cs
index da4009e..c5728a5 100644
--- a/IrcTokens/Tests/StatefulDecoder.cs
+++ b/IrcTokens/Tests/StatefulDecoder.cs
@@ -45,7 +45,8 @@ namespace IrcTokens.Tests
         {
             var iso8859 = Encoding.GetEncoding("iso-8859-1");
             _decoder = new IrcTokens.StatefulDecoder {Encoding = iso8859};
-            var lines = _decoder.Push(iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n"));
+            var bytes = iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n");
+            var lines = _decoder.Push(bytes, bytes.Length);
             var line  = new Line("PRIVMSG #channel :hello Ç");
             Assert.IsTrue(line.Equals(lines[0]));
         }
@@ -55,7 +56,8 @@ namespace IrcTokens.Tests
         {
             var latin1 = Encoding.GetEncoding("iso-8859-1");
             _decoder = new IrcTokens.StatefulDecoder {Encoding = null, Fallback = latin1};
-            var lines = _decoder.Push(latin1.GetBytes("PRIVMSG #channel hélló\r\n"));
+            var bytes = latin1.GetBytes("PRIVMSG #channel hélló\r\n");
+            var lines = _decoder.Push(bytes, bytes.Length);
             Assert.AreEqual(1, lines.Count);
             Assert.IsTrue(new Line("PRIVMSG #channel hélló").Equals(lines[0]));
         }