From 7daa0c6ec594195457c8f1ea5699738582acee33 Mon Sep 17 00:00:00 2001 From: Stephen Reindl Date: Tue, 19 Nov 2024 13:27:10 +0100 Subject: [PATCH] feat: add ReplaceString and SubString fixes #113 --- .../Oberon0System.StringsTests.cs | 49 ++++++++++++++----- Oberon0.System/Oberon0System.Strings.cs | 26 ++++++++++ 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/Oberon0.System.Tests/Oberon0System.StringsTests.cs b/Oberon0.System.Tests/Oberon0System.StringsTests.cs index 8616432..1c27f8e 100644 --- a/Oberon0.System.Tests/Oberon0System.StringsTests.cs +++ b/Oberon0.System.Tests/Oberon0System.StringsTests.cs @@ -2,9 +2,6 @@ namespace Oberon0.System.Tests; -using Oberon0System; -using Xunit; - public static partial class Oberon0SystemTests { [Theory] @@ -16,7 +13,7 @@ public static partial class Oberon0SystemTests public static void CanCallConvertToStringWithInt(int value) { // Act - string result = Oberon0System.ConvertToString(value); + string result = Oberon0System.Oberon0System.ConvertToString(value); // Assert Assert.Equal(value.ToString(), result); @@ -31,7 +28,7 @@ public static void CanCallConvertToStringWithInt(int value) public static void CanCallConvertToStringWithReal(double value) { // Act - string result = Oberon0System.ConvertToString(value); + string result = Oberon0System.Oberon0System.ConvertToString(value); // Assert Assert.Equal(value.ToString(CultureInfo.InvariantCulture), result); @@ -43,7 +40,7 @@ public static void CanCallConvertToStringWithReal(double value) public static void CanCallConvertToStringWithBoolean(bool value) { // Act - string result = Oberon0System.ConvertToString(value); + string result = Oberon0System.Oberon0System.ConvertToString(value); // Assert Assert.Equal(value.ToString(), result); @@ -60,7 +57,7 @@ public static void CanCallConvertToStringWithBoolean(bool value) [InlineData(double.MinValue, "-1.7976931348623157E+308", "G")] public static void TestToStringRealFormat(double value, string expected, string format) { - string result = Oberon0System.ConvertToString(value, format); + string result = Oberon0System.Oberon0System.ConvertToString(value, format); Assert.Equal(expected, result); } @@ -71,7 +68,7 @@ public static void TestToStringRealFormat(double value, string expected, string [InlineData(true, "1", "1", "0")] public static void TestToStringBooleanFormat(bool value, string expected, string trueVal, string falseVal) { - string result = Oberon0System.ConvertToString(value, trueVal, falseVal); + string result = Oberon0System.Oberon0System.ConvertToString(value, trueVal, falseVal); Assert.Equal(expected, result); } @@ -84,7 +81,7 @@ public static void TestToStringBooleanFormat(bool value, string expected, string public static void TestParseStringReal(string value, double expected, bool expectedReturn) { double result = 0; - bool returnValue = Oberon0System.ParseString(value, ref result); + bool returnValue = Oberon0System.Oberon0System.ParseString(value, ref result); Assert.Equal(expectedReturn, returnValue); Assert.Equal(expected, result); } @@ -98,7 +95,7 @@ public static void TestParseStringReal(string value, double expected, bool expec public static void TestParseStringInt(string value, int expected, bool expectedReturn) { int result = 0; - bool returnValue = Oberon0System.ParseString(value, ref result); + bool returnValue = Oberon0System.Oberon0System.ParseString(value, ref result); Assert.Equal(expectedReturn, returnValue); Assert.Equal(expected, result); } @@ -113,9 +110,39 @@ public static void TestParseStringInt(string value, int expected, bool expectedR public static void TestParseStringBool(string value, bool expected, bool expectedReturn) { bool result = false; - bool returnValue = Oberon0System.ParseString(value, ref result); + bool returnValue = Oberon0System.Oberon0System.ParseString(value, ref result); Assert.Equal(expectedReturn, returnValue); Assert.Equal(expected, result); } + [Theory] + [InlineData("Hello World", "Hello", " Hi", " Hi World")] + [InlineData("Hello World", "Hello ", "", "World")] + [InlineData("Hello World Test", " World", " Unit", "Hello Unit Test")] + [InlineData("Hello World", "NotFound", "Replaced", "Hello World")] + public static void TestReplaceString(string value, string source, string target, string expected) + { + string result = Oberon0System.Oberon0System.ReplaceString(value, source, target); + Assert.Equal(expected, result); + } + + [Theory] + [InlineData("This is a Test", 1, 2, "Th")] + [InlineData("This is a Test", 6, 2, "is")] + [InlineData("This is a Test", 1, 14, "This is a Test")] + public static void TestSubString(string value, int start, int len, string expected) + { + string result = Oberon0System.Oberon0System.SubString(value, start, len); + Assert.Equal(expected, result); + } + + [Theory] + [InlineData("This is a Test", 0, 2)] + [InlineData("This is a Test", 6, 14)] + [InlineData("This is a Test", 1, 20)] + public static void TestSubStringIndexOobException(string value, int start, int len) + { + Assert.Throws(() => Oberon0System.Oberon0System.SubString(value, start, len)); + } + } \ No newline at end of file diff --git a/Oberon0.System/Oberon0System.Strings.cs b/Oberon0.System/Oberon0System.Strings.cs index 142cbba..e3fdb69 100644 --- a/Oberon0.System/Oberon0System.Strings.cs +++ b/Oberon0.System/Oberon0System.Strings.cs @@ -123,4 +123,30 @@ public static bool ParseString(string value, ref bool result) { return bool.TryParse(value, out result); } + + /// + /// Parse a BOOLEAN from a string and return the boolean itself + /// + /// The original string + /// The string to be replaced + /// The replacement string + [Oberon0Export("ReplaceString", "STRING", "STRING", "STRING", "STRING")] + // ReSharper disable once UnusedMember.Global + public static string ReplaceString(string value, string source, string target) + { + return value.Replace(source, target); + } + + /// + /// Parse a BOOLEAN from a string and return the boolean itself + /// + /// The string where the substring has to be built + /// Start of substring (counting from 1) + /// The length of the substring. Passing 0 will return the rest of the string starting from *start*. + [Oberon0Export("SubString", "STRING", "STRING", "INTEGER", "INTEGER")] + // ReSharper disable once UnusedMember.Global + public static string SubString(string value, int start, int len) + { + return len > 0 ? value.Substring(start - 1, len) : value[(start - 1)..]; + } } \ No newline at end of file