Skip to content

Commit 9eeb012

Browse files
committed
fix: Replace containsSubstring function with canFind for improved substring matching
1 parent 2c923b0 commit 9eeb012

File tree

1 file changed

+4
-19
lines changed
  • source/fluentasserts/operations/string

1 file changed

+4
-19
lines changed

source/fluentasserts/operations/string/contain.d

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,10 @@ private struct MatchResult {
6666
string first;
6767
}
6868

69-
private bool containsSubstring(string haystack, string needle) @safe pure nothrow @nogc {
70-
if(needle.length == 0) {
71-
return true;
72-
}
73-
if(needle.length > haystack.length) {
74-
return false;
75-
}
76-
foreach(i; 0 .. haystack.length - needle.length + 1) {
77-
if(haystack[i .. i + needle.length] == needle) {
78-
return true;
79-
}
80-
}
81-
return false;
82-
}
83-
84-
private MatchResult countMatches(bool findPresent)(string[] pieces, string testData) @safe nothrow @nogc {
69+
private MatchResult countMatches(bool findPresent)(string[] pieces, string testData) @safe nothrow {
8570
MatchResult result;
8671
foreach(piece; pieces) {
87-
if(containsSubstring(testData, piece) != findPresent) {
72+
if(testData.canFind(piece) != findPresent) {
8873
continue;
8974
}
9075
if(result.count == 0) {
@@ -96,7 +81,7 @@ private MatchResult countMatches(bool findPresent)(string[] pieces, string testD
9681
}
9782

9883
private void appendValueList(ref AssertResult result, string[] pieces, string testData,
99-
MatchResult matchResult, bool findPresent) @safe nothrow @nogc {
84+
MatchResult matchResult, bool findPresent) @safe nothrow {
10085
if(matchResult.count == 1) {
10186
result.addValue(matchResult.first);
10287
return;
@@ -105,7 +90,7 @@ private void appendValueList(ref AssertResult result, string[] pieces, string te
10590
result.addText("[");
10691
bool first = true;
10792
foreach(piece; pieces) {
108-
if(containsSubstring(testData, piece) != findPresent) {
93+
if(testData.canFind(piece) != findPresent) {
10994
continue;
11095
}
11196
if(!first) {

0 commit comments

Comments
 (0)