Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.8" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ public static bool StringToUpperInvariant(string sourceString, string substringT
}

// Regular Expression Search
// Regex.Escape() treats the search term as literal text, so a term such as "c.de" or "z*"
// is not reinterpreted as a pattern and an unmatched bracket does not throw.
// RegexOptions.CultureInvariant keeps IgnoreCase off the current culture's casing rules.
public static bool RegexIsMatch(string sourceString, string substringToSearch)
{
return Regex
.IsMatch(sourceString, substringToSearch, RegexOptions.IgnoreCase);
.IsMatch(sourceString,
Regex.Escape(substringToSearch),
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}

// Linq With String Equals Method Search
// This matches whole separator-delimited words, not substrings, so it answers a different
// question from the other four methods. That difference is intentional, not a bug.
public static bool LinqStringEquals(string sourceString, string substringToSearch, char separator)
{
return sourceString
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Globalization;
using CaseInsensitiveSubstringSearch;

namespace Tests
{
[TestClass]
public class SubstringSearchSemanticsTest
{
private const string SourceString = "Code Maze";

[TestMethod]
[DataRow("aze")]
[DataRow("ode")]
[DataRow("e M")]
public void GivenMidWordSubstring_WhenComparingAllFiveMethods_ThenOnlyLinqStringEqualsReturnsFalse(string subStringToSearch)
{
Assert.IsTrue(SubstringSearch.StringContains(SourceString, subStringToSearch));
Assert.IsTrue(SubstringSearch.StringIndexOf(SourceString, subStringToSearch));
Assert.IsTrue(SubstringSearch.StringToUpperInvariant(SourceString, subStringToSearch));
Assert.IsTrue(SubstringSearch.RegexIsMatch(SourceString, subStringToSearch));

Assert.IsFalse(SubstringSearch.LinqStringEquals(SourceString, subStringToSearch, ' '));
}

[TestMethod]
[DataRow("c.de")]
[DataRow("z*")]
[DataRow("m|q")]
[DataRow("(")]
public void GivenSearchTermHoldingRegexMetacharacters_WhenUsingRegexIsMatchMethod_ThenItAgreesWithStringContains(string subStringToSearch)
{
var expectedResult = SubstringSearch.StringContains(SourceString, subStringToSearch);

var result = SubstringSearch.RegexIsMatch(SourceString, subStringToSearch);

Assert.AreEqual(expectedResult, result);
Assert.IsFalse(result);
}

[TestMethod]
public void GivenTurkishCulture_WhenSearchingForFileInsideFILE_ThenAllFourContainmentMethodsMatch()
{
var originalCulture = CultureInfo.CurrentCulture;

try
{
CultureInfo.CurrentCulture = new CultureInfo("tr-TR");

Assert.IsTrue(SubstringSearch.StringContains("FILE", "file"));
Assert.IsTrue(SubstringSearch.StringIndexOf("FILE", "file"));
Assert.IsTrue(SubstringSearch.StringToUpperInvariant("FILE", "file"));
Assert.IsTrue(SubstringSearch.RegexIsMatch("FILE", "file"));
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
}
}
}
}
10 changes: 5 additions & 5 deletions strings-csharp/CaseInsensitiveSubstringSearch/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="4.4.0" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading