Skip to content

Commit 2c923b0

Browse files
committed
fix: Refactor containsSubstring function for improved substring matching logic
1 parent 2c9ccd4 commit 2c923b0

File tree

1 file changed

+17
-2
lines changed
  • source/fluentasserts/operations/string

1 file changed

+17
-2
lines changed

source/fluentasserts/operations/string/contain.d

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,25 @@ 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+
6984
private MatchResult countMatches(bool findPresent)(string[] pieces, string testData) @safe nothrow @nogc {
7085
MatchResult result;
7186
foreach(piece; pieces) {
72-
if(testData.canFind(piece) != findPresent) {
87+
if(containsSubstring(testData, piece) != findPresent) {
7388
continue;
7489
}
7590
if(result.count == 0) {
@@ -90,7 +105,7 @@ private void appendValueList(ref AssertResult result, string[] pieces, string te
90105
result.addText("[");
91106
bool first = true;
92107
foreach(piece; pieces) {
93-
if(testData.canFind(piece) != findPresent) {
108+
if(containsSubstring(testData, piece) != findPresent) {
94109
continue;
95110
}
96111
if(!first) {

0 commit comments

Comments
 (0)