diff --git a/src/Hl7.Fhir.Base/FhirPath/Functions/StringOperators.cs b/src/Hl7.Fhir.Base/FhirPath/Functions/StringOperators.cs index 35424baefe..6f21c0898b 100644 --- a/src/Hl7.Fhir.Base/FhirPath/Functions/StringOperators.cs +++ b/src/Hl7.Fhir.Base/FhirPath/Functions/StringOperators.cs @@ -20,7 +20,12 @@ internal static class StringOperators { public static string FpSubstring(this string me, long start, long? length) { - var l = length ?? me.Length; + var l = length switch + { + < 0 => 0, + null => me.Length, + _ => length.Value + }; if (start < 0 || start >= me.Length) return null; l = Math.Min(l, me.Length - start); @@ -30,7 +35,7 @@ public static string FpSubstring(this string me, long start, long? length) public static ITypedElement FpIndexOf(this string me, string fragment) { - return ElementNode.ForPrimitive(me.IndexOf(fragment)); + return ElementNode.ForPrimitive(me.IndexOf(fragment, StringComparison.Ordinal)); } public static IEnumerable ToChars(this string me) => diff --git a/src/Hl7.FhirPath.Tests/Functions/StringFunctionsTests.cs b/src/Hl7.FhirPath.Tests/Functions/StringFunctionsTests.cs index effacb9451..3c4dbc38de 100644 --- a/src/Hl7.FhirPath.Tests/Functions/StringFunctionsTests.cs +++ b/src/Hl7.FhirPath.Tests/Functions/StringFunctionsTests.cs @@ -3,6 +3,7 @@ using Hl7.FhirPath.Functions; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; +using System.Collections.Generic; using System.Linq; namespace HL7.FhirPath.Tests.Functions @@ -25,6 +26,22 @@ public void StringSplit() Assert.AreEqual(5, StringOperators.FpSplit("Peter,James,Jim,Peter,James", ",").Count()); } + private static IEnumerable substringTests() + { + yield return ["Ewout", 0, (long?)3, "Ewo"]; + yield return ["Ewout", 2, (long?)3, "out"]; + yield return ["Ewout", 0, null, "Ewout"]; + yield return ["Ewout", 0, (long?)0, ""]; + yield return ["Ewout", 0, (long?)-1, ""]; + } + + [DataTestMethod] + [DynamicData(nameof(substringTests), DynamicDataSourceType.Method)] + public void SubString(string input, long start, long? length, string expected) + { + input.FpSubstring(start, length).Should().Be(expected); + } + [TestMethod] public void EncodeBase64() {