From 1bc3727ce83eeb08d1773e053962d2453b896267 Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Mon, 15 Mar 2021 21:26:19 +1300 Subject: [PATCH] Added PreviousChar() function --- Assignment 1/Program.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Assignment 1/Program.cs b/Assignment 1/Program.cs index d0eff38..297d21b 100644 --- a/Assignment 1/Program.cs +++ b/Assignment 1/Program.cs @@ -716,6 +716,30 @@ namespace Assignment_1 return c; } + /// + /// Reads the previous char + /// + /// + /// + static char PreviousChar(Stream s) + { + Stack charBytes = new Stack(4); + for (int i = 0; i < 4; i++) + { + if (s.Position == 0) + { + break; + } + s.Position--; + byte read = (byte)s.ReadByte(); + charBytes.Push(read); + // No longer an UTF-8 extension, last byte is the final + if (read >> 6 != 2) break; + } + string converted = Encoding.UTF8.GetString(charBytes.ToArray()); + return converted[0]; + } + /// /// Skips whitespace characters ///