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

Replaces '.Replace' calls with a Regex #72

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/AddressExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,7 @@ public async IAsyncEnumerable<string> ExtractAddressesAsync(IPerformanceStack st
if (valid is Result.DENY)
continue;

yield return address.Full
// Simple cleanups that may be possible via the regex
.Replace("'", string.Empty)
.Replace("!", string.Empty)
.Replace("`", string.Empty)
.Replace("{", string.Empty)
.Replace("#", string.Empty)
.Replace(@"\n", string.Empty)
.Replace("\\\"", string.Empty);
yield return address.Full;
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/Objects/Filters/ReplaceInvalidFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text.RegularExpressions;

namespace HaveIBeenPwned.AddressExtractor.Objects.Filters {
/// <summary>
/// Checks if the full email starts with specifically illegal characters and trims them until there are no more illegal characters. /// </summary>
public partial class ReplaceInvalidFilter : AddressFilter.BaseFilter {
[GeneratedRegex(@"^['!`\{#\\n\\\\]+(.*)")]
public static partial Regex StartsWithCharacter();

/// <inheritdoc />
public override string Name => "TrimIllegalStartChars";

/// <inheritdoc />
public override Result ValidateEmailAddress(ref EmailAddress address) {
Match match = ReplaceInvalidFilter.StartsWithCharacter()
.Match(address.Full);

if ( match is not { Length: > 0 } )
return Result.CONTINUE;

address.Full = match.Groups[1].Value;

// If the email is now empty, it was only consisting of illegal characters
return address.Length is 0 ? Result.DENY : Result.REVALIDATE;

}


}
}