public static ICharactersRangeDefinition CreateMatching(StandardCharacterCategories range) { Guard.ArgSatisfies(range, nameof(range), p => !p.IsEmpty(), p => $"Specified category is empty ({p})."); Guard.ArgSatisfies(range, nameof(range), p => p.IsValid(), p => $"Specified category is not supported ({p})"); return(new StandardCategory(range)); }
private static bool IsCharacterFromPatternInternal(char character, StandardCharacterCategories pattern, bool positive) { var characterType = character.CategorizeByStandardPattern(positive); var matches = characterType != StandardCharacterCategories.None && pattern.HasFlag(characterType); return positive ? matches : !matches; }
public static StandardCharactersPatternType GetValidPatternType(this StandardCharacterCategories pattern) { var type = pattern.GetPatternType(); return type != StandardCharactersPatternType.Invalid ? type : throw new ArgumentException( $"Invalid pattern: Pattern contains mixed negative and positive patterns ({pattern})."); }
public static StandardCharacterCategories Not(this StandardCharacterCategories pattern) { var mask = (int) pattern; var negative = (mask & NegativePatternsShift) >> NegativePatternsShift; var positive = (mask & PositivePatternMask) << NegativePatternsShift; var negated = (StandardCharacterCategories)(negative | positive); return negated; }
public static StandardCharactersPatternType GetValidNotEmptyPatternType(this StandardCharacterCategories pattern) { var type = pattern.GetPatternType(); return type switch { StandardCharactersPatternType.Empty => throw new ArgumentException($"Pattern is empty ({pattern})", nameof(pattern)), StandardCharactersPatternType.Invalid => throw new ArgumentException($"Invalid pattern: Pattern contains mixed negative and positive patterns ({pattern})."), _ => type }; }
public static bool IsAnyCharacterFromPattern(IEnumerable<char> characters, StandardCharacterCategories pattern) { if (pattern == StandardCharacterCategories.None) { return false; } var positive = pattern.GetValidPatternType() == StandardCharactersPatternType.Positive; var contains = characters.Any(character => IsCharacterFromPatternInternal(character, pattern, positive)); return contains; }
public static StandardCharactersPatternType GetPatternType(this StandardCharacterCategories pattern) { if (pattern == StandardCharacterCategories.None) { return StandardCharactersPatternType.Empty; } var hasNegatives = ((int) pattern & NegativePatternMask) != 0; if (hasNegatives) { var hasPositives = ((int) pattern & PositivePatternMask) != 0; return hasPositives ? StandardCharactersPatternType.Invalid : StandardCharactersPatternType.Negative; } return StandardCharactersPatternType.Positive; }
public static string ToRegexStatement(this StandardCharacterCategories pattern) { var positive = pattern.GetValidNotEmptyPatternType() == StandardCharactersPatternType.Positive; return ToRegexStatement(pattern, positive); }
private static string ToRegexStatement(this StandardCharacterCategories fullPattern, bool positiveScope) { var pattern = positiveScope ? fullPattern : fullPattern.Not(); var simple = pattern switch { StandardCharacterCategories.UpperLetter => positiveScope ? "[A-Z]" : "[^A-Z]", StandardCharacterCategories.LowerLetter => positiveScope ? "[a-z]": "[^a-z]", StandardCharacterCategories.Letter => positiveScope ? "[A-Za-z]": "[^A-Za-z]", StandardCharacterCategories.LetterOrDigit => positiveScope ? @"[A-Za-z0-9]": @"[^A-Za-z0-9]", StandardCharacterCategories.Digit => positiveScope ? @"\d": @"\D", StandardCharacterCategories.Underscore => positiveScope ? @"_": "^_", StandardCharacterCategories.Space => positiveScope ? @"[ ]": @"[^ ]", StandardCharacterCategories.Tab => positiveScope ? @"\t": @"^\t", StandardCharacterCategories.NewLine => positiveScope ? @"\n": @"^\n", StandardCharacterCategories.Return => positiveScope ? @"\r": @"^\r", StandardCharacterCategories.LineBreak => positiveScope ? @"[\r\n]": @"[^\r\n]", StandardCharacterCategories.SpaceOrTab => positiveScope ? @"[\t ]": @"[^\t ]", StandardCharacterCategories.WordCharacter => positiveScope ? @"\w": @"\W", StandardCharacterCategories.WhiteSpace => positiveScope ? @"\s": @"\S", StandardCharacterCategories.WordCharacterOrWhiteSpace => positiveScope ? @"[\w\s]" : @"[^\w\s]", _ => null, }; if (simple != null) { return simple; } var regex = positiveScope ? "[" : "[^"; var wordCharacterFlags = pattern.EvaluateContainsFlags(StandardCharacterCategories.WordCharacter); if (wordCharacterFlags != FlagCheckResult.None) { if (wordCharacterFlags == FlagCheckResult.All) { regex += @"\w"; } else { if (pattern.HasFlag(StandardCharacterCategories.UpperLetter)) { regex += "A-Z"; } if (pattern.HasFlag(StandardCharacterCategories.LowerLetter)) { regex += "a-z"; } if (pattern.HasFlag(StandardCharacterCategories.Underscore)) { regex += "_"; } if (pattern.HasFlag(StandardCharacterCategories.Digit)) { regex += "0-9"; } } } var whiteSpaceFlags = pattern.EvaluateContainsFlags(StandardCharacterCategories.WhiteSpace); if (whiteSpaceFlags != FlagCheckResult.None) { if (whiteSpaceFlags == FlagCheckResult.All) { regex += @"\s"; } else { if (pattern.HasFlag(StandardCharacterCategories.Tab)) { regex += @"\t"; } if (pattern.HasFlag(StandardCharacterCategories.Space)) { regex += @" "; } if (pattern.HasFlag(StandardCharacterCategories.NewLine)) { regex += @"\n"; } if (pattern.HasFlag(StandardCharacterCategories.Return)) { regex += @"\r"; } } } regex += "]"; return regex; }
public static IEnumerable<char> Matches(this StandardCharacterCategories pattern, IEnumerable<char> characters) { var positive = pattern.GetValidPatternType() == StandardCharactersPatternType.Positive; return characters.Where(character => IsCharacterFromPatternInternal(character, pattern, positive)); }
/// <inheritdoc /> public string CreateCharRange(IRegularExpressionFactoryContext ctx, StandardCharacterCategories range) { return(range.ToRegexStatement()); }
public static bool IsEmpty(this StandardCharacterCategories pattern) { return pattern == StandardCharacterCategories.None; }
public static bool IsValid(this StandardCharacterCategories pattern) { var type = pattern.GetPatternType(); return type != StandardCharactersPatternType.Invalid; }
public static bool IsAny(this char character, StandardCharacterCategories pattern) { var matches = IsCharacterFromPatternInternal(character, pattern, pattern.GetValidPatternType() == StandardCharactersPatternType.Positive); return matches; }
public static bool HasAny(this StandardCharacterCategories pattern, StandardCharacterCategories contained) { return (pattern & contained) != StandardCharacterCategories.None; }
public static bool HasAll(this StandardCharacterCategories pattern, StandardCharacterCategories contained) { return (pattern & contained) == contained; }
public static bool IsDefined(this StandardCharacterCategories pattern) { return pattern != StandardCharacterCategories.None; }
public static IDefineCharactersRange ToDefinition(this StandardCharacterCategories pattern) { return CharactersRangeDefinition.CreateMatching(pattern); }
/// <inheritdoc /> public StandardCharacterRangeBrick(IFlexpressionAlgebra algebra, StandardCharacterCategories letterTypes) : base(algebra) { _range = letterTypes; }
public static string Description(this StandardCharacterCategories pattern) { return $"{pattern}"; }
public StandardCategory(StandardCharacterCategories category) { _category = category; }
public static bool IsMatchAny(this StandardCharacterCategories pattern, IEnumerable<char> characters) { return IsAnyCharacterFromPattern(characters, pattern); }