Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5560b1d

Browse files
author
Doug Schmidt
committed
Fixed a bug in StringExtensions.IndexOfAny()
Previous version would only return the earliest position when all needles were present. If the last needle was missing, -1 would be returned even if other needles were present.
1 parent b18a67b commit 5560b1d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public static int IndexOfAny(this string text, int startIndex, params string[] n
481481
foreach (var needle in needles)
482482
{
483483
var pos = text.IndexOf(needle, startIndex);
484-
if (firstPos == -1 || pos < firstPos)
484+
if ((pos >= 0) && (firstPos == -1 || pos < firstPos))
485485
firstPos = pos;
486486
}
487487
}

tests/ServiceStack.Text.Tests/StringExtensionsTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ public void Does_not_alter_filepath_without_extension()
8282
Assert.That("path/to/file.ext".WithoutExtension(), Is.EqualTo("path/to/file"));
8383
}
8484

85-
[Test]
86-
public void Does_find_IndexOfAny_strings()
85+
// 0 1
86+
// 01234567890123456789
87+
[TestCase("text with /* and <!--", "<!--", "/*", 10)]
88+
[TestCase("text with /* and <!--", "<!--x", "/*", 10)]
89+
[TestCase("text with /* and <!--", "<!--", "/*x", 17)]
90+
[TestCase("text with /* and <!--", "<!--x", "/*x", -1)]
91+
public void Does_find_IndexOfAny_strings(string text, string needle1, string needle2, int expectedPos)
8792
{
88-
var text = "text with /* and <!--";
89-
var pos = text.IndexOfAny("<!--", "/*");
90-
Assert.That(pos, Is.EqualTo("text with ".Length));
93+
var pos = text.IndexOfAny(needle1, needle2);
94+
Assert.That(pos, Is.EqualTo(expectedPos));
9195
}
9296

93-
[Test]
97+
[Test]
9498
public void Does_ExtractContent_first_pattern_from_Document_without_marker()
9599
{
96100
var text = "text with random <!--comment--> and Contents: <!--Contents--> are here";

0 commit comments

Comments
 (0)