-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Polyfill span.Contains(T) #123656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Polyfill span.Contains(T) #123656
Changes from all commits
db879ec
dbdd48a
a4b7cf3
0d77080
10e6264
afab9ee
ca625f2
845b2ac
6f3ea24
86aad6d
e68927a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System | ||
| { | ||
| /// <summary>Provides downlevel polyfills for string extension methods.</summary> | ||
| internal static class StringPolyfills | ||
| { | ||
| public static bool StartsWith(this string s, char value) => | ||
| s.Length > 0 && s[0] == value; | ||
|
|
||
| public static bool EndsWith(this string s, char value) => | ||
| s.Length > 0 && s[s.Length - 1] == value; | ||
|
|
||
| public static bool Contains(this string s, char value) => | ||
| s.IndexOf(value) >= 0; | ||
|
Check failure on line 16 in src/libraries/Common/src/System/StringPolyfills.cs
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
|
|
||
| namespace System.Configuration | ||
| { | ||
| public class StringValidator : ConfigurationValidatorBase | ||
| { | ||
| private readonly string _invalidChars; | ||
| private readonly SearchValues<char> _invalidChars; | ||
| private readonly int _maxLength; | ||
| private readonly int _minLength; | ||
|
|
||
|
|
@@ -21,7 +23,7 @@ public StringValidator(int minLength, int maxLength, string invalidCharacters) | |
| { | ||
| _minLength = minLength; | ||
| _maxLength = maxLength; | ||
| _invalidChars = invalidCharacters; | ||
| _invalidChars = SearchValues.Create(invalidCharacters ?? string.Empty); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How frequently are these StringValidators created? |
||
| } | ||
|
|
||
| public override bool CanValidate(Type type) | ||
|
|
@@ -42,14 +44,9 @@ public override void Validate(object value) | |
| throw new ArgumentException(SR.Format(SR.Validator_string_max_length, _maxLength)); | ||
|
|
||
| // Check if the string contains any invalid characters | ||
| if ((len > 0) && !string.IsNullOrEmpty(_invalidChars)) | ||
| if (data.AsSpan().ContainsAny(_invalidChars)) | ||
| { | ||
| char[] array = new char[_invalidChars.Length]; | ||
|
|
||
| _invalidChars.CopyTo(0, array, 0, _invalidChars.Length); | ||
|
|
||
| if (data.IndexOfAny(array) != -1) | ||
MihaZupan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new ArgumentException(SR.Format(SR.Validator_string_invalid_chars, _invalidChars)); | ||
| throw new ArgumentException(SR.Format(SR.Validator_string_invalid_chars, _invalidChars)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.