From 60da2672561ad1d6924f423fb91198909091a373 Mon Sep 17 00:00:00 2001 From: Dmytro Kyshchenko Date: Thu, 14 Sep 2023 17:34:09 +0300 Subject: [PATCH] Optimization --- xFunc.Maths/Expressions/Matrices/VectorValue.cs | 8 ++++---- xFunc.Maths/Tokenization/Lexer.KeywordToken.cs | 8 +++++--- xFunc.Maths/Tokenization/Lexer.StringToken.cs | 16 ++-------------- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/xFunc.Maths/Expressions/Matrices/VectorValue.cs b/xFunc.Maths/Expressions/Matrices/VectorValue.cs index 752fc247f..806589709 100644 --- a/xFunc.Maths/Expressions/Matrices/VectorValue.cs +++ b/xFunc.Maths/Expressions/Matrices/VectorValue.cs @@ -174,12 +174,12 @@ public NumberValue Average() /// The sum of the values in the vector. public NumberValue Sum() { - var sum = NumberValue.Zero; + var sum = 0.0; - for (var i = 0; i < Size; i++) - sum += array[i]; + for (var i = 0; i < array.Length; i++) + sum += array[i].Number; - return sum; + return new NumberValue(sum); } /// diff --git a/xFunc.Maths/Tokenization/Lexer.KeywordToken.cs b/xFunc.Maths/Tokenization/Lexer.KeywordToken.cs index df2ec7f68..c4f329d1a 100644 --- a/xFunc.Maths/Tokenization/Lexer.KeywordToken.cs +++ b/xFunc.Maths/Tokenization/Lexer.KeywordToken.cs @@ -21,9 +21,11 @@ private bool CreateKeywordToken() var keyword = function[..endIndex]; - var lowerKeyword = keyword.Length <= 1024 - ? stackalloc char[keyword.Length] - : new char[keyword.Length]; + // keyword shouldn't be bigger than the biggest valid keyword + if (keyword.Length > "unassign".Length) + return false; + + Span lowerKeyword = stackalloc char[keyword.Length]; keyword.ToLowerInvariant(lowerKeyword); diff --git a/xFunc.Maths/Tokenization/Lexer.StringToken.cs b/xFunc.Maths/Tokenization/Lexer.StringToken.cs index 7a63dada9..71ad10e6f 100644 --- a/xFunc.Maths/Tokenization/Lexer.StringToken.cs +++ b/xFunc.Maths/Tokenization/Lexer.StringToken.cs @@ -13,20 +13,8 @@ private bool CreateStringToken(char quote) if (function[0] != quote) return false; - var endIndex = 1; - var foundClosingQuote = false; - while (endIndex < function.Length) - { - if (function[endIndex] == quote) - { - foundClosingQuote = true; - break; - } - - endIndex++; - } - - if (!foundClosingQuote) + var endIndex = function[1..].IndexOf(quote) + 1; + if (endIndex == 0) throw new TokenizeException(Resource.StringTokenizeException); var stringValue = function[1..endIndex];