Skip to content

Commit

Permalink
Merge pull request #2758 from FirelyTeam/feature/fhirpath-substring
Browse files Browse the repository at this point in the history
fhirpath substring with negative length
  • Loading branch information
ewoutkramer authored Apr 9, 2024
2 parents eb05df7 + af7742e commit ed38d79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Hl7.Fhir.Base/FhirPath/Functions/StringOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<ITypedElement> ToChars(this string me) =>
Expand Down
17 changes: 17 additions & 0 deletions src/Hl7.FhirPath.Tests/Functions/StringFunctionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +26,22 @@ public void StringSplit()
Assert.AreEqual(5, StringOperators.FpSplit("Peter,James,Jim,Peter,James", ",").Count());
}

private static IEnumerable<object[]> 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()
{
Expand Down

0 comments on commit ed38d79

Please sign in to comment.