diff --git a/Directory.Build.targets b/Directory.Build.targets index 1dc7daa031..60014e02b9 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -80,7 +80,7 @@ portable - + $(DefineConstants);FEATURE_ARRAY_FILL @@ -93,6 +93,13 @@ + + + + $(DefineConstants);FEATURE_STRING_CONTAINS_STRINGCOMPARISON + + + diff --git a/src/Lucene.Net.Tests.AllProjects/Support/ExceptionHandling/TestExceptionExtensions.cs b/src/Lucene.Net.Tests.AllProjects/Support/ExceptionHandling/TestExceptionExtensions.cs index 776f6ac06a..862db3661a 100644 --- a/src/Lucene.Net.Tests.AllProjects/Support/ExceptionHandling/TestExceptionExtensions.cs +++ b/src/Lucene.Net.Tests.AllProjects/Support/ExceptionHandling/TestExceptionExtensions.cs @@ -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 { /* diff --git a/src/Lucene.Net.Tests/Support/Text/TestStringExtensions.cs b/src/Lucene.Net.Tests/Support/Text/TestStringExtensions.cs new file mode 100644 index 0000000000..b041d86ad6 --- /dev/null +++ b/src/Lucene.Net.Tests/Support/Text/TestStringExtensions.cs @@ -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 + } +} diff --git a/src/Lucene.Net/Support/Text/StringExtensions.cs b/src/Lucene.Net/Support/Text/StringExtensions.cs index eae0842233..e15c607089 100644 --- a/src/Lucene.Net/Support/Text/StringExtensions.cs +++ b/src/Lucene.Net/Support/Text/StringExtensions.cs @@ -48,5 +48,26 @@ public static bool ContainsAny(this string input, char[] charsToCompare) } return false; } + +#if !FEATURE_STRING_CONTAINS_STRINGCOMPARISON + /// + /// Returns true if contains + /// using the specified . + /// + /// The string in which to seek . + /// The string to check for. + /// The to use. + /// true if is found, otherwise; false. + [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 } }