Skip to content

Commit

Permalink
Add string.Contains extension method for .NET Framework and Standard 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Jan 18, 2025
1 parent a07f705 commit 7da9624
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<DebugType>portable</DebugType>
</PropertyGroup>

<!-- Features in .NET Standard 2.1, .NET 5.x, .NET 6.x, .NET 7.x, .NET 8.x, and .NET 9.x only -->
<!-- Features in .NET Standard 2.1, .NET Core 3.x, .NET 5.x, .NET 6.x, .NET 7.x, .NET 8.x, and .NET 9.x only -->
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' Or $(TargetFramework.StartsWith('netcoreapp3.')) Or $(TargetFramework.StartsWith('net5.')) Or $(TargetFramework.StartsWith('net6.')) Or $(TargetFramework.StartsWith('net7.')) Or $(TargetFramework.StartsWith('net8.')) Or $(TargetFramework.StartsWith('net9.')) ">

<DefineConstants>$(DefineConstants);FEATURE_ARRAY_FILL</DefineConstants>
Expand All @@ -93,6 +93,13 @@

</PropertyGroup>

<!-- Features in .NET Standard 2.1, .NET Core 2.1-2.2, .NET Core 3.x, .NET 5.x, .NET 6.x, .NET 7.x, .NET 8.x, and .NET 9.x only -->
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' Or '$(TargetFramework)' == 'netcoreapp2.1' Or '$(TargetFramework)' == 'netcoreapp2.2' Or $(TargetFramework.StartsWith('netcoreapp3.')) Or $(TargetFramework.StartsWith('net5.')) Or $(TargetFramework.StartsWith('net6.')) Or $(TargetFramework.StartsWith('net7.')) Or $(TargetFramework.StartsWith('net8.')) Or $(TargetFramework.StartsWith('net9.')) ">

<DefineConstants>$(DefineConstants);FEATURE_STRING_CONTAINS_STRINGCOMPARISON</DefineConstants>

</PropertyGroup>

<!-- Features in .NET Standard 2.x, .NET Core 2.x, .NET Core 3.x, .NET 5.x, .NET 6.x, .NET 7.x, .NET 8.x, and .NET 9.x -->
<PropertyGroup Condition=" $(TargetFramework.StartsWith('netstandard2.')) Or $(TargetFramework.StartsWith('netcoreapp2.')) Or $(TargetFramework.StartsWith('netcoreapp3.')) Or $(TargetFramework.StartsWith('net5.')) Or $(TargetFramework.StartsWith('net6.')) Or $(TargetFramework.StartsWith('net7.')) Or $(TargetFramework.StartsWith('net8.')) Or $(TargetFramework.StartsWith('net9.')) ">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using System.Linq;
using Assert = Lucene.Net.TestFramework.Assert;

#if !FEATURE_STRING_CONTAINS_STRINGCOMPARISON
using Lucene.Net.Support.Text;
#endif

namespace Lucene.Net.Support.ExceptionHandling
{
/*
Expand Down
48 changes: 48 additions & 0 deletions src/Lucene.Net.Tests/Support/Text/TestStringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Lucene.Net.Attributes;
using Lucene.Net.Util;
using NUnit.Framework;
#if !FEATURE_STRING_CONTAINS_STRINGCOMPARISON
using System;
#endif

namespace Lucene.Net.Support.Text
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

[TestFixture, LuceneNetSpecific]
public class TestStringExtensions : LuceneTestCase
{
[Test]
public void TestContainsAny()
{
Assert.IsTrue("hello".ContainsAny(new[] { 'h', 'e', 'l', 'o' }));
Assert.IsFalse("hello".ContainsAny(new[] { 'x', 'y', 'z' }));
}

#if !FEATURE_STRING_CONTAINS_STRINGCOMPARISON
[Test]
public void TestContainsWithStringComparison()
{
Assert.IsTrue("hello".Contains("ell", StringComparison.Ordinal));
Assert.IsFalse("hello".Contains("world", StringComparison.Ordinal));
Assert.IsTrue("hello".Contains("ELL", StringComparison.OrdinalIgnoreCase));
Assert.IsFalse("hello".Contains("WORLD", StringComparison.OrdinalIgnoreCase));
}
#endif
}
}
21 changes: 21 additions & 0 deletions src/Lucene.Net/Support/Text/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,26 @@ public static bool ContainsAny(this string input, char[] charsToCompare)
}
return false;
}

#if !FEATURE_STRING_CONTAINS_STRINGCOMPARISON
/// <summary>
/// Returns <c>true</c> if <paramref name="input"/> contains <paramref name="value"/>
/// using the specified <paramref name="comparison"/>.
/// </summary>
/// <param name="input">The string in which to seek <paramref name="value"/>.</param>
/// <param name="value">The string to check for.</param>
/// <param name="comparison">The <see cref="StringComparison"/> to use.</param>
/// <returns><c>true</c> if <paramref name="value"/> is found, otherwise; <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Contains(this string input, string value, StringComparison comparison)
{
if (input is null)
throw new ArgumentNullException(nameof(input));
if (value is null)
throw new ArgumentNullException(nameof(value));

return input.IndexOf(value, comparison) >= 0;
}
#endif
}
}

0 comments on commit 7da9624

Please sign in to comment.