diff --git a/Jace/AstBuilder.cs b/Jace/AstBuilder.cs index 33824b6..24f6bad 100644 --- a/Jace/AstBuilder.cs +++ b/Jace/AstBuilder.cs @@ -5,6 +5,7 @@ using Jace.Execution; using Jace.Operations; using Jace.Tokenizer; +using Jace.Util; namespace Jace { @@ -81,7 +82,7 @@ public Operation Build(IList tokens) { if (!caseSensitive) { - tokenValue = tokenValue.ToLowerInvariant(); + tokenValue = tokenValue.ToLowerFast(); } resultStack.Push(new Variable(tokenValue)); } diff --git a/Jace/Execution/ConstantRegistry.cs b/Jace/Execution/ConstantRegistry.cs index 448a1d0..f934105 100644 --- a/Jace/Execution/ConstantRegistry.cs +++ b/Jace/Execution/ConstantRegistry.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Jace.Util; namespace Jace.Execution { @@ -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(); } } } diff --git a/Jace/Execution/FunctionRegistry.cs b/Jace/Execution/FunctionRegistry.cs index 57de7a2..d976618 100644 --- a/Jace/Execution/FunctionRegistry.cs +++ b/Jace/Execution/FunctionRegistry.cs @@ -4,6 +4,7 @@ using System.Text; using System.Reflection; using System.Collections; +using Jace.Util; namespace Jace.Execution { @@ -112,7 +113,7 @@ public bool IsFunctionName(string functionName) private string ConvertFunctionName(string functionName) { - return caseSensitive ? functionName : functionName.ToLowerInvariant(); + return caseSensitive ? functionName : functionName.ToLowerFast(); } } } diff --git a/Jace/Util/EngineUtil.cs b/Jace/Util/EngineUtil.cs index 00b1817..612b0c2 100644 --- a/Jace/Util/EngineUtil.cs +++ b/Jace/Util/EngineUtil.cs @@ -15,10 +15,32 @@ static internal IDictionary ConvertVariableNamesToLowerCase(IDic Dictionary temp = new Dictionary(); foreach (KeyValuePair 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(); + } } }