Repository navigation
Use new IndexOfAnyValues throughout dotnet/runtime #78204
Description
Once #78093 is merged, we'll want to use it in a variety of places where it'll provide benefits.
Some example possible use sites:
runtime/src/libraries/Microsoft.Extensions.Configuration.UserSecrets/src/PathHelper.cs
Line 49 in 8ddb0c4
int badCharIndex = userSecretsId.IndexOfAny(Path.GetInvalidFileNameChars()); - https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Internal/PathUtils.cs#L12-L29
if (pipeName.IndexOfAny(s_invalidFileNameChars) >= 0) if (propertyName.IndexOfAny(ReadStack.SpecialCharacters) != -1)
Already updated:
if (name.IndexOfAny(SPECIAL_CHARS) < 0) || value.AsSpan().IndexOfAny(ReservedToName) >= 0) runtime/src/libraries/System.Private.Uri/src/System/DomainNameHelper.cs
Lines 392 to 393 in 214ca6d
internal static bool ContainsCharactersUnsafeForNormalizedHost(string host) => host.AsSpan().IndexOfAny(@"\/?@#:[]") >= 0; Lines 129 to 130 in 2f78734
const string QuoteNeedingChars = ",+=\"\n<>#;"; // \r is NOT in this list, because it isn't in Windows. return rdnValue.IndexOfAny(QuoteNeedingChars) >= 0; runtime/src/libraries/System.Private.Xml/src/System/Xml/XmlCharType.cs
Lines 164 to 177 in 28f6daa
internal static int IsPublicId(string str) { if (str != null) { for (int i = 0; i < str.Length; i++) { if (!IsPubidChar(str[i])) { return i; } } } return -1; } runtime/src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlBaseWriter.cs
Lines 757 to 774 in 8918adf
private static void VerifyWhitespace(string s) { for (int i = 0; i < s.Length; i++) if (!IsWhitespace(s[i])) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.XmlIllegalOutsideRoot)); } private static void VerifyWhitespace(char[] chars, int offset, int count) { for (int i = 0; i < count; i++) if (!IsWhitespace(chars[offset + i])) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.XmlIllegalOutsideRoot)); } private static bool IsWhitespace(char ch) { return (ch == ' ' || ch == '\n' || ch == '\r' || ch == 't'); } runtime/src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlBufferReader.cs
Lines 768 to 788 in 8918adf
public bool IsWhitespaceKey(int key) { string s = GetDictionaryString(key).Value; for (int i = 0; i < s.Length; i++) { if (!XmlConverter.IsWhitespace(s[i])) return false; } return true; } public bool IsWhitespaceUTF8(int offset, int length) { byte[] buffer = _buffer; for (int i = 0; i < length; i++) { if (!XmlConverter.IsWhitespace((char)buffer[offset + i])) return false; } return true; } runtime/src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlConverter.cs
Lines 1085 to 1093 in 8918adf
public static bool IsWhitespace(string s) { for (int i = 0; i < s.Length; i++) { if (!IsWhitespace(s[i])) return false; } return true; } runtime/src/libraries/System.Net.Http/src/System/Net/Http/HttpRuleParser.cs
Lines 88 to 112 in 8918adf
internal static bool IsToken(string input) { for (int i = 0; i < input.Length; i++) { if (!IsTokenChar(input[i])) { return false; } } return true; } internal static bool IsToken(ReadOnlySpan<byte> input) { for (int i = 0; i < input.Length; i++) { if (!IsTokenChar((char)input[i])) { return false; } } return true; } Lines 150 to 161 in 4820105
public static string Escape(string input) { for (int i = 0; i < input.Length; i++) { if (IsMetachar(input[i])) { return EscapeImpl(input, i); } } return input; }
Activity
adamsitnik commentedon Dec 15, 2022
@MihaZupan I can see that you have merged plenty of PRs related to this issue. Is it done now?
MihaZupan commentedon Dec 17, 2022
My queries for interesting patterns that could be replaced are starting to dry up.
There's still a handful of places that can be updated, after that, I'll close this issue.
Still on my TODO to look at: