mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
tweak snapshot LintRawRule
This commit is contained in:
parent
c35f1591c6
commit
db09845c8a
1 changed files with 18 additions and 15 deletions
|
|
@ -1,35 +1,38 @@
|
|||
namespace FilterLists.Services.Snapshot
|
||||
using System;
|
||||
|
||||
namespace FilterLists.Services.Snapshot
|
||||
{
|
||||
public static class RawRuleLinterExtensions
|
||||
{
|
||||
public static string LintRawRule(this string rule)
|
||||
{
|
||||
rule = rule?.TrimLeadingAndTrailingWhitespace();
|
||||
rule = rule?.DropIfEmpty();
|
||||
rule = rule.Trim();
|
||||
rule = rule.DropIfTooLong();
|
||||
rule = rule?.DropIfComment();
|
||||
rule = rule?.DropIfTooLong();
|
||||
rule = rule?.DropIfContainsBackslashSingleQuote();
|
||||
rule = rule?.TrimSingleBackslashFromEnd();
|
||||
rule = rule?.DropIfEmpty();
|
||||
return rule;
|
||||
}
|
||||
|
||||
private static string TrimLeadingAndTrailingWhitespace(this string rule)
|
||||
{
|
||||
char[] charsToTrim = {' ', '\t'};
|
||||
return rule.Trim(charsToTrim);
|
||||
}
|
||||
|
||||
private static string DropIfEmpty(this string rule) => rule == "" ? null : rule;
|
||||
private static string DropIfTooLong(this string rule) =>
|
||||
rule.Length > 8192 ? null : rule;
|
||||
|
||||
private static string DropIfComment(this string rule) =>
|
||||
rule.StartsWith(@"!") && !rule.StartsWith(@"!#") || rule.StartsWith(@"!##") ? null : rule;
|
||||
|
||||
private static string DropIfTooLong(this string rule) => rule.Length > 8192 ? null : rule;
|
||||
rule.StartsWith(@"!", StringComparison.Ordinal) && !rule.StartsWith(@"!#", StringComparison.Ordinal) ||
|
||||
rule.StartsWith(@"!##", StringComparison.Ordinal)
|
||||
? null
|
||||
: rule;
|
||||
|
||||
private static string DropIfContainsBackslashSingleQuote(this string rule) =>
|
||||
rule.Contains(@"\'") ? null : rule;
|
||||
|
||||
private static string TrimSingleBackslashFromEnd(this string rule) =>
|
||||
rule.EndsWith(@"\") && !rule.EndsWith(@"\\") ? rule.Remove(rule.Length - 1) : rule;
|
||||
rule.EndsWith(@"\", StringComparison.Ordinal) && !rule.EndsWith(@"\\", StringComparison.Ordinal)
|
||||
? rule.Remove(rule.Length - 1)
|
||||
: rule;
|
||||
|
||||
private static string DropIfEmpty(this string rule) =>
|
||||
rule == string.Empty ? null : rule;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue