Added a center-string function
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed

This commit is contained in:
Brychan Dempsey 2021-03-12 12:46:00 +13:00
parent 5082bc6481
commit 74df6241cc

View File

@ -30,6 +30,11 @@ namespace Assignment_1
{ "NEWLINE", new Tuple<string, VariableFlags>("\n", VariableFlags.Reserved | VariableFlags.NoPrint) }, { "NEWLINE", new Tuple<string, VariableFlags>("\n", VariableFlags.Reserved | VariableFlags.NoPrint) },
{ "CARRIAGE_RETURN", new Tuple<string, VariableFlags>("\r", VariableFlags.Reserved | VariableFlags.NoPrint) } { "CARRIAGE_RETURN", new Tuple<string, VariableFlags>("\r", VariableFlags.Reserved | VariableFlags.NoPrint) }
}; };
static List<char> ForbiddenChars = new List<char>
{
'$', '\\', '\"', '\''
};
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine("┌──────────────────────────────────────────┐"); Console.WriteLine("┌──────────────────────────────────────────┐");
@ -197,7 +202,7 @@ namespace Assignment_1
void List() void List()
{ {
Console.WriteLine("┌" + new string('─', 15) + "┬" + new string('─', 25) + "┬" + new string('─', 9) + "┐"); Console.WriteLine("┌" + new string('─', 15) + "┬" + new string('─', 25) + "┬" + new string('─', 9) + "┐");
Console.WriteLine("│{0,-15}│{1,-25}│{2,9}│", "Symbol", "Value", "Flags"); Console.WriteLine("│{0}│{1}│{2}│", CenterString("Symbol",15), CenterString("Value",25), CenterString("Flags",9));
Console.WriteLine("├" + new string('─', 15) + "┼" + new string('─', 25) + "┼" + new string('─', 9) + "┤"); Console.WriteLine("├" + new string('─', 15) + "┼" + new string('─', 25) + "┼" + new string('─', 9) + "┤");
int keyPos = 0; int keyPos = 0;
foreach (var item in Symbols) foreach (var item in Symbols)
@ -267,9 +272,15 @@ namespace Assignment_1
} }
string identifier; string identifier;
long identifierEndPos = FindIdentifier(source, out identifier); long identifierEndPos = FindIdentifier(source, out identifier);
if (identifierEndPos <= source.Position + 1 ) if (identifierEndPos <= source.Position + 1 || identifier.Trim().Length == 0)
{ {
WriteDebugLine(lineStart, "set ".Length, "???", source); WriteDebugLine(lineStart, "set ".Length, "expected an identifier", source);
return false;
}
else if (ForbiddenChars.Exists((c) => identifier.Contains(c)))
{
char fbChar = ForbiddenChars.Find((c) => identifier.Contains(c));
WriteDebugLine(lineStart, "set ".Length, string.Format("character {0} is not valid for an identifier", fbChar), source);
return false; return false;
} }
source.Position = identifierEndPos; source.Position = identifierEndPos;
@ -531,7 +542,7 @@ namespace Assignment_1
long originalPos = s.Position; long originalPos = s.Position;
string tempstring; string tempstring;
long nextWSOccurance = FindNextOccurance(s, (c, s) => Char.IsWhiteSpace(c), out tempstring); long nextWSOccurance = FindNextOccurance(s, (c, s) => Char.IsWhiteSpace(c), out tempstring);
while (tempstring.Length == 0) while (tempstring.Length == 0 && s.Position < s.Length)
{ {
nextWSOccurance = FindNextOccurance(s, (c, s) => Char.IsWhiteSpace(c), out tempstring); nextWSOccurance = FindNextOccurance(s, (c, s) => Char.IsWhiteSpace(c), out tempstring);
s.Position++; s.Position++;
@ -635,5 +646,17 @@ namespace Assignment_1
} }
s.Position--; s.Position--;
} }
static string CenterString(string source, int totalPadding, char paddingChar=' ')
{
if (source.Length >= totalPadding) return source;
int leftHalf = (int)Math.Floor(source.Length / 2.0);
int rightHalf = (int)Math.Ceiling(source.Length / 2.0);
int leftHalfPad = (int)Math.Floor(totalPadding / 2.0);
int rightHalfPad = (int)Math.Ceiling(totalPadding / 2.0);
string t = "{0," + leftHalfPad + "}{1," + -1 * rightHalfPad + "}";
string result = string.Format(t, source.Substring(0,leftHalf+1), source.Substring(rightHalf,source.Length-rightHalf));
return result;
}
} }
} }