Skip to content

Commit

Permalink
Implemented a fast to lower case for strings in ASCII
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterderycke committed Dec 14, 2019
1 parent 6c30252 commit f3756a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Jace/AstBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Jace.Execution;
using Jace.Operations;
using Jace.Tokenizer;
using Jace.Util;

namespace Jace
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public Operation Build(IList<Token> tokens)
{
if (!caseSensitive)
{
tokenValue = tokenValue.ToLowerInvariant();
tokenValue = tokenValue.ToLowerFast();
}
resultStack.Push(new Variable(tokenValue));
}
Expand Down
3 changes: 2 additions & 1 deletion Jace/Execution/ConstantRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jace.Util;

namespace Jace.Execution
{
Expand Down Expand Up @@ -72,7 +73,7 @@ public void RegisterConstant(string constantName, double value, bool isOverWrita

private string ConvertConstantName(string constantName)
{
return caseSensitive ? constantName : constantName.ToLowerInvariant();
return caseSensitive ? constantName : constantName.ToLowerFast();
}
}
}
3 changes: 2 additions & 1 deletion Jace/Execution/FunctionRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Reflection;
using System.Collections;
using Jace.Util;

namespace Jace.Execution
{
Expand Down Expand Up @@ -112,7 +113,7 @@ public bool IsFunctionName(string functionName)

private string ConvertFunctionName(string functionName)
{
return caseSensitive ? functionName : functionName.ToLowerInvariant();
return caseSensitive ? functionName : functionName.ToLowerFast();
}
}
}
24 changes: 23 additions & 1 deletion Jace/Util/EngineUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,32 @@ static internal IDictionary<string, double> ConvertVariableNamesToLowerCase(IDic
Dictionary<string, double> temp = new Dictionary<string, double>();
foreach (KeyValuePair<string, double> keyValuePair in variables)
{
temp.Add(keyValuePair.Key.ToLowerInvariant(), keyValuePair.Value);
temp.Add(keyValuePair.Key.ToLowerFast(), keyValuePair.Value);
}

return temp;
}

// This is a fast ToLower for strings that are in ASCII
static internal string ToLowerFast(this string text)
{
StringBuilder buffer = new StringBuilder(text.Length);

for(int i = 0; i < text.Length; i++)
{
char c = text[i];

if (c >= 'A' && c <= 'Z')
{
buffer.Append((char)(c + 32));
}
else
{
buffer.Append(c);
}
}

return buffer.ToString();
}
}
}

0 comments on commit f3756a8

Please sign in to comment.