This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 75f4e0b0ed6980a9f8c025ce85bc74dfc93dd531 Author: Shad Storhaug <[email protected]> AuthorDate: Fri Jul 24 08:53:47 2020 +0700 Lucene.Net.Support.IO.FileSupport::CreateTempFile(): Optimized the check for invalid characters to shave off a few ns --- src/Lucene.Net/Support/IO/FileSupport.cs | 15 +++---- src/Lucene.Net/Support/Text/StringExtensions.cs | 52 +++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/Lucene.Net/Support/IO/FileSupport.cs b/src/Lucene.Net/Support/IO/FileSupport.cs index 2fdaf9d..a4b903c 100644 --- a/src/Lucene.Net/Support/IO/FileSupport.cs +++ b/src/Lucene.Net/Support/IO/FileSupport.cs @@ -1,8 +1,9 @@ +using Lucene.Net.Support.Text; using Lucene.Net.Util; using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; namespace Lucene.Net.Support.IO @@ -29,6 +30,8 @@ namespace Lucene.Net.Support.IO /// </summary> internal static class FileSupport { + private static readonly char[] INVALID_FILENAME_CHARS = Path.GetInvalidFileNameChars(); + // LUCNENENET NOTE: Lookup the HResult value we are interested in for the current OS // by provoking the exception during initialization and caching its HResult value for later. // We optimize for Windows because those HResult values are known and documented, but for @@ -157,12 +160,10 @@ namespace Lucene.Net.Support.IO throw new ArgumentException("Prefix string too short"); // Ensure the strings passed don't contain invalid characters - char[] invalid = Path.GetInvalidFileNameChars(); - - if (prefix.ToCharArray().Intersect(invalid).Any()) - throw new ArgumentException(string.Format("Prefix contains invalid characters. You may not use any of '{0}'", string.Join(", ", invalid))); - if (suffix != null && suffix.ToCharArray().Intersect(invalid).Any()) - throw new ArgumentException(string.Format("Suffix contains invalid characters. You may not use any of '{0}'", string.Join(", ", invalid))); + if (prefix.ContainsAny(INVALID_FILENAME_CHARS)) + throw new ArgumentException(string.Format("Prefix contains invalid characters. You may not use any of '{0}'", string.Join(", ", INVALID_FILENAME_CHARS))); + if (suffix != null && suffix.ContainsAny(INVALID_FILENAME_CHARS)) + throw new ArgumentException(string.Format("Suffix contains invalid characters. You may not use any of '{0}'", string.Join(", ", INVALID_FILENAME_CHARS))); // If no directory supplied, create one. if (directory == null) diff --git a/src/Lucene.Net/Support/Text/StringExtensions.cs b/src/Lucene.Net/Support/Text/StringExtensions.cs new file mode 100644 index 0000000..3886a0e --- /dev/null +++ b/src/Lucene.Net/Support/Text/StringExtensions.cs @@ -0,0 +1,52 @@ +using J2N.Text; +using System; +using System.Runtime.CompilerServices; + +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. + */ + + /// <summary> + /// Extensions to <see cref="string"/>. + /// </summary> + internal static class StringExtensions + { + /// <summary> + /// Returns <c>true</c> if <paramref name="input"/> contains any character from <paramref name="charsToCompare"/>. + /// </summary> + /// <param name="input">The string in which to seek characters from <paramref name="charsToCompare"/>.</param> + /// <param name="charsToCompare">An array of characters to check.</param> + /// <returns><c>true</c> if any <paramref name="charsToCompare"/> are found, otherwise; <c>false</c>.</returns> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool ContainsAny(this string input, char[] charsToCompare) + { + if (input == null) + throw new ArgumentNullException(nameof(input)); + if (charsToCompare == null) + throw new ArgumentNullException(nameof(charsToCompare)); + + // Ensure the strings passed don't contain invalid characters + for (int i = 0; i < charsToCompare.Length; i++) + { + if (input.Contains(charsToCompare[i])) + return true; + } + return false; + } + } +}
