Added PreviousChar() function

This commit is contained in:
Brychan Dempsey 2021-03-15 21:26:19 +13:00
parent 83c70333f8
commit 1bc3727ce8

View File

@ -716,6 +716,30 @@ namespace Assignment_1
return c; return c;
} }
/// <summary>
/// Reads the previous char
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
static char PreviousChar(Stream s)
{
Stack<byte> charBytes = new Stack<byte>(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];
}
/// <summary> /// <summary>
/// Skips whitespace characters /// Skips whitespace characters
/// </summary> /// </summary>