From d388c8de5b3ec6fb986f95a73e890541a3992a99 Mon Sep 17 00:00:00 2001 From: Neil MacMullen Date: Fri, 27 Sep 2024 10:26:10 +0100 Subject: [PATCH] - Update nuget dependencies - introduce sustring overload - intrdoduce padleft and padright --- Directory.Packages.Props | 24 ++++---- .../Evaluation/BuiltIns/CustomFunctions.cs | 2 + .../ScalarFunctions/PadLeftFunction.cs | 8 +++ .../ScalarFunctions/PadRightFunction.cs | 8 +++ .../ScalarFunctions/SubstringFunction.cs | 9 ++- test/BasicTests/SimpleFunctionTests.cs | 61 +++++++++++++++++++ 6 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/PadLeftFunction.cs create mode 100644 libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/PadRightFunction.cs diff --git a/Directory.Packages.Props b/Directory.Packages.Props index b4f42d8d..87dacff1 100644 --- a/Directory.Packages.Props +++ b/Directory.Packages.Props @@ -4,7 +4,7 @@ - + @@ -14,29 +14,29 @@ - - - - - + + + + + - + - + - + - - + + - + \ No newline at end of file diff --git a/libraries/KustoLoco.Core/Evaluation/BuiltIns/CustomFunctions.cs b/libraries/KustoLoco.Core/Evaluation/BuiltIns/CustomFunctions.cs index 4c9ae4a0..4b6d0766 100644 --- a/libraries/KustoLoco.Core/Evaluation/BuiltIns/CustomFunctions.cs +++ b/libraries/KustoLoco.Core/Evaluation/BuiltIns/CustomFunctions.cs @@ -27,6 +27,8 @@ static CustomFunctions() StringSimilarity.Register(functions); DateTimeToIso.Register(functions); TrimWsFunction.Register(functions); + PadLeftFunction.Register(functions); + PadRightFunction.Register(functions); ToDateTimeFmtFunction.Register(functions); ParseHexFunction.Register(functions); } diff --git a/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/PadLeftFunction.cs b/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/PadLeftFunction.cs new file mode 100644 index 00000000..a016ceae --- /dev/null +++ b/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/PadLeftFunction.cs @@ -0,0 +1,8 @@ +namespace KustoLoco.Core.Evaluation.BuiltIns.Impl; + +[KustoImplementation(Keyword = "padleft")] +internal partial class PadLeftFunction +{ + private static string Impl(string s,long n) => s.PadLeft((int)n,' '); + private static string WithCharImpl(string s,long n,string pad) => s.PadLeft((int)n,pad.Length >0 ? pad[0]:' '); +} diff --git a/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/PadRightFunction.cs b/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/PadRightFunction.cs new file mode 100644 index 00000000..4842b835 --- /dev/null +++ b/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/PadRightFunction.cs @@ -0,0 +1,8 @@ +namespace KustoLoco.Core.Evaluation.BuiltIns.Impl; + +[KustoImplementation(Keyword = "padright")] +internal partial class PadRightFunction +{ + private static string Impl(string s,long n) => s.PadRight((int)n,' '); + private static string WithCharImpl(string s,long n,string pad) => s.PadRight((int)n,pad.Length >0 ? pad[0]:' '); +} diff --git a/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/SubstringFunction.cs b/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/SubstringFunction.cs index 9361dbf2..493bb55f 100644 --- a/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/SubstringFunction.cs +++ b/libraries/KustoLoco.Core/Evaluation/BuiltIns/ScalarFunctions/SubstringFunction.cs @@ -6,7 +6,7 @@ namespace KustoLoco.Core.Evaluation.BuiltIns.Impl; [KustoImplementation(Keyword = "Functions.Substring")] internal partial class SubstringFunction { - private static string Impl(string input, long start, long length) + internal static string Impl(string input, long start, long length) { var effectiveStart = Math.Max(0, Math.Min(start, input.Length)); var maxAllowableLength = input.Length - effectiveStart; @@ -14,4 +14,9 @@ private static string Impl(string input, long start, long length) Math.Max(0, Math.Min(length, maxAllowableLength)); return input.Substring((int)effectiveStart, (int)effectiveLength); } -} \ No newline at end of file + + private static string AImpl(string input, long start) + { + return SubstringFunction.Impl(input, start, int.MaxValue); + } +} diff --git a/test/BasicTests/SimpleFunctionTests.cs b/test/BasicTests/SimpleFunctionTests.cs index b02d279d..2a8271df 100644 --- a/test/BasicTests/SimpleFunctionTests.cs +++ b/test/BasicTests/SimpleFunctionTests.cs @@ -132,6 +132,15 @@ public async Task SubString() result.Should().Be("Cde"); } + [TestMethod] + public async Task SubStringSingleParameter() + { + var query = "print c=substring('ABCdef',2)"; + var result = await LastLineOfResult(query); + result.Should().Be("Cdef"); + } + + [TestMethod] public async Task Trimws() { @@ -140,6 +149,58 @@ public async Task Trimws() result.Should().Be("abc"); } + + + [TestMethod] + public async Task PadLeft() + { + var query = "print c=padleft('abc',6)"; + var result = await LastLineOfResult(query); + result.Should().Be(" abc"); + } + + [TestMethod] + public async Task PadLeftWithChar() + { + var query = "print c=padleft('abc',6,'A')"; + var result = await LastLineOfResult(query); + result.Should().Be("AAAabc"); + } + + + [TestMethod] + public async Task PadLeftWithBlankChar() + { + var query = "print c=padleft('abc',6,'')"; + var result = await LastLineOfResult(query); + result.Should().Be(" abc"); + } + + [TestMethod] + public async Task PadRight() + { + var query = "print c=padright('abc',6)"; + var result = await LastLineOfResult(query); + result.Should().Be("abc "); + } + + [TestMethod] + public async Task PadRightWithChar() + { + var query = "print c=padright('abc',6,'A')"; + var result = await LastLineOfResult(query); + result.Should().Be("abcAAA"); + } + + + [TestMethod] + public async Task PadRightWithBlankChar() + { + var query = "print c=padright('abc',6,'')"; + var result = await LastLineOfResult(query); + result.Should().Be("abc "); + } + [TestMethod] public async Task DateTimeBin() {