Skip to content
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

Remove Substrings from PhoneAttribute #80389

Merged
merged 1 commit into from Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -65,6 +65,7 @@
<Reference Include="System.ComponentModel.Primitives" />
<Reference Include="System.ComponentModel.TypeConverter" />
<Reference Include="System.Linq" />
<Reference Include="System.Memory" />
<Reference Include="System.Runtime" />
<Reference Include="System.Text.RegularExpressions" />
<Reference Include="System.Threading" />
Expand Down
Expand Up @@ -32,11 +32,11 @@ public override bool IsValid(object? value)
return false;
}

valueAsString = valueAsString.Replace("+", string.Empty).TrimEnd();
valueAsString = RemoveExtension(valueAsString);
ReadOnlySpan<char> valueSpan = valueAsString.Replace("+", string.Empty).AsSpan().TrimEnd();
valueSpan = RemoveExtension(valueSpan);

bool digitFound = false;
foreach (char c in valueAsString)
foreach (char c in valueSpan)
{
if (char.IsDigit(c))
{
Expand All @@ -50,7 +50,7 @@ public override bool IsValid(object? value)
return false;
}

foreach (char c in valueAsString)
foreach (char c in valueSpan)
{
if (!(char.IsDigit(c)
|| char.IsWhiteSpace(c)
Expand All @@ -63,48 +63,48 @@ public override bool IsValid(object? value)
return true;
}

private static string RemoveExtension(string potentialPhoneNumber)
private static ReadOnlySpan<char> RemoveExtension(ReadOnlySpan<char> potentialPhoneNumber)
{
var lastIndexOfExtension = potentialPhoneNumber
int lastIndexOfExtension = potentialPhoneNumber
.LastIndexOf(ExtensionAbbreviationExtDot, StringComparison.OrdinalIgnoreCase);
if (lastIndexOfExtension >= 0)
{
var extension = potentialPhoneNumber.Substring(
ReadOnlySpan<char> extension = potentialPhoneNumber.Slice(
lastIndexOfExtension + ExtensionAbbreviationExtDot.Length);
if (MatchesExtension(extension))
{
return potentialPhoneNumber.Substring(0, lastIndexOfExtension);
return potentialPhoneNumber.Slice(0, lastIndexOfExtension);
}
}

lastIndexOfExtension = potentialPhoneNumber
.LastIndexOf(ExtensionAbbreviationExt, StringComparison.OrdinalIgnoreCase);
if (lastIndexOfExtension >= 0)
{
var extension = potentialPhoneNumber.Substring(
ReadOnlySpan<char> extension = potentialPhoneNumber.Slice(
lastIndexOfExtension + ExtensionAbbreviationExt.Length);
if (MatchesExtension(extension))
{
return potentialPhoneNumber.Substring(0, lastIndexOfExtension);
return potentialPhoneNumber.Slice(0, lastIndexOfExtension);
}
}

lastIndexOfExtension = potentialPhoneNumber
.LastIndexOf(ExtensionAbbreviationX, StringComparison.OrdinalIgnoreCase);
if (lastIndexOfExtension >= 0)
{
var extension = potentialPhoneNumber.Substring(
ReadOnlySpan<char> extension = potentialPhoneNumber.Slice(
lastIndexOfExtension + ExtensionAbbreviationX.Length);
if (MatchesExtension(extension))
{
return potentialPhoneNumber.Substring(0, lastIndexOfExtension);
return potentialPhoneNumber.Slice(0, lastIndexOfExtension);
}
}

return potentialPhoneNumber;
}

private static bool MatchesExtension(string potentialExtension)
private static bool MatchesExtension(ReadOnlySpan<char> potentialExtension)
{
potentialExtension = potentialExtension.TrimStart();
if (potentialExtension.Length == 0)
Expand Down