create_regex() private method

private create_regex ( ) : void
return void
Example #1
0
        // tries to parse a line - if it fails, it will return null
        private static filter_line parse_impl(string line) {
            line = line.Trim();
            if (line.StartsWith("#"))
                // allow comments
                return null;

            if (line == "full-word")
                // 1.8.17 - can happen from Find >> To Filter - but we don't handle it yet
                return null;

            if (line.StartsWith("font") || line.StartsWith("color") || line.StartsWith("match_color"))
                return parse_font(line);

            if (line == "case-insensitive")
                return new filter_line(line) {part = part_type.case_sensitive_info, case_sensitive = false };

            if ( !line.StartsWith("$"))
                return null;

            string[] words = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            
            // Syntax:
            // $ColumnName Comparison Text
            bool ok = words.Length >= 3 || (words.Length == 2 && is_regex_expression(words[1]));
            if (!ok)
                // we need at least $c compare word(s)
                return null;

            filter_line fi = new filter_line(line);
            fi.part = part_type_io.from_str(words[0]);
            if (fi.part == part_type.invalid)
                return null;

            if (words.Length == 2) {
                // comparison is not present; inferred as regex
                fi.text = words[1];
                fi.comparison = comparison_type.regex;
                fi.lo_text = fi.text.ToLower();
                fi.create_regex();
                return fi.regex_ != null ? fi : null ;
            }

            switch ( words[1].ToLower() ) {
            case "!=": fi.comparison = comparison_type.not_equal; break;

            case "==": 
            case "=": 
                fi.comparison = comparison_type.equal; break;

            case "+": 
            case "startswith":
                fi.comparison = comparison_type.starts_with; break;

            case "-": 
            case "!startswith":
                fi.comparison = comparison_type.does_not_start_with; break;

            case "++": 
            case "contains":
                fi.comparison = comparison_type.contains; break;

            case "--": 
            case "!contains":
                fi.comparison = comparison_type.does_not_contain; break;

            case "containsany":
            case "any":
                fi.comparison = comparison_type.contains_any; break;

            case "containsnone":
            case "none":
                fi.comparison = comparison_type.contains_none;
                break;

            case "matches":
            case "match":
                fi.comparison = comparison_type.regex;
                break;
            default:
                return null;
            }

            // take the rest of the text
            int compare_idx = line.IndexOf(words[1]);
            if (compare_idx + words[1].Length + 1 >= line.Length)
                // nothing following the comparison
                return null;
            // 1.2.20+ - don't trim the end! - very likely the user wants the beginning/ending spaces to be in the comparison
            line = line.Substring(compare_idx + words[1].Length + 1);
            if (fi.comparison == comparison_type.contains_any || fi.comparison == comparison_type.contains_none) {
                fi.words = line.Split('|');
                fi.lo_words = fi.words.Select(w => w.ToLower()).ToArray();
            }
            // 1.8.17+ - allow specifying quotes for contains or !contains
            line = trim_quotes(line);
            fi.text = line;
            fi.lo_text = line.ToLower();
            fi.create_regex();
            if (fi.comparison == comparison_type.regex && fi.regex_ == null)
                // the regex is invalid at this point
                return null;
            return fi;
        }
Example #2
0
        // tries to parse a line - if it fails, it will return null
        private static filter_line parse_impl(string line)
        {
            line = line.Trim();
            if (line.StartsWith("#"))
            {
                // allow comments
                return(null);
            }

            if (line.StartsWith("font") || line.StartsWith("color") || line.StartsWith("match_color"))
            {
                return(parse_font(line));
            }

            if (line == "case-insensitive")
            {
                return new filter_line(line)
                       {
                           part = part_type.case_sensitive_info, case_sensitive = false
                       }
            }
            ;

            if (!line.StartsWith("$"))
            {
                return(null);
            }

            string[] words = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

            // Syntax:
            // $ColumnName Comparison Text
            bool ok = words.Length >= 3 || (words.Length == 2 && is_regex_expression(words[1]));

            if (!ok)
            {
                // we need at least $c compare word(s)
                return(null);
            }

            filter_line fi = new filter_line(line);

            fi.part = part_type_io.from_str(words[0]);
            if (fi.part == part_type.invalid)
            {
                return(null);
            }

            if (words.Length == 2)
            {
                // comparison is not present; inferred as regex
                fi.text       = words[1];
                fi.comparison = comparison_type.regex;
                fi.lo_text    = fi.text.ToLower();
                fi.create_regex();
                return(fi.regex_ != null ? fi : null);
            }

            switch (words[1].ToLower())
            {
            case "!=": fi.comparison = comparison_type.not_equal; break;

            case "==":
            case "=":
                fi.comparison = comparison_type.equal; break;

            case "+":
            case "startswith":
                fi.comparison = comparison_type.starts_with; break;

            case "-":
            case "!startswith":
                fi.comparison = comparison_type.does_not_start_with; break;

            case "++":
            case "contains":
                fi.comparison = comparison_type.contains; break;

            case "--":
            case "!contains":
                fi.comparison = comparison_type.does_not_contain; break;

            case "containsany":
            case "any":
                fi.comparison = comparison_type.contains_any; break;

            case "containsnone":
            case "none":
                fi.comparison = comparison_type.contains_none;
                break;

            case "matches":
            case "match":
                fi.comparison = comparison_type.regex;
                break;

            default:
                return(null);
            }

            // take the rest of the text
            int compare_idx = line.IndexOf(words[1]);

            if (compare_idx + words[1].Length + 1 >= line.Length)
            {
                // nothing following the comparison
                return(null);
            }
            // 1.2.20+ - don't trim the end! - very likely the user wants the beginning/ending spaces to be in the comparison
            line = line.Substring(compare_idx + words[1].Length + 1);
            if (fi.comparison == comparison_type.contains_any || fi.comparison == comparison_type.contains_none)
            {
                fi.words    = line.Split('|');
                fi.lo_words = fi.words.Select(w => w.ToLower()).ToArray();
            }
            fi.text    = line;
            fi.lo_text = line.ToLower();
            fi.create_regex();
            if (fi.comparison == comparison_type.regex && fi.regex_ == null)
            {
                // the regex is invalid at this point
                return(null);
            }
            return(fi);
        }
Example #3
0
        // tries to parse a line - if it fails, it will return null
        private static filter_line parse_impl(string line)
        {
            line = line.Trim();
            if (line.StartsWith("#"))
            {
                // allow comments
                return(null);
            }

            if (line == "full-word")
            {
                // 1.8.17 - can happen from Find >> To Filter - but we don't handle it yet
                return(null);
            }

            if (line.StartsWith("font") || line.StartsWith("color") || line.StartsWith("match_color"))
            {
                return(parse_font(line));
            }

            if (line == "case-insensitive")
            {
                return new filter_line(line)
                       {
                           part = part_type.case_sensitive_info, case_sensitive = false
                       }
            }
            ;

            if (!line.StartsWith("$"))
            {
                return(null);
            }

            string[] words = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

            // Syntax:
            // $ColumnName Comparison Text
            if (words.Length < 3)
            {
                // we need at least $c compare word(s)
                return(null);
            }

            filter_line fi = new filter_line(line)
            {
                part = part_type_io.from_str(words[0])
            };

            if (fi.part == part_type.invalid)
            {
                return(null);
            }

            switch (words[1].ToLower())
            {
            case "!=": fi.comparison = comparison_type.not_equal; break;

            case "==":
            case "=":
                fi.comparison = comparison_type.equal; break;

            case "+":
            case "startswith":
                fi.comparison = comparison_type.starts_with; break;

            case "-":
            case "!startswith":
                fi.comparison = comparison_type.does_not_start_with; break;

            case "++":
            case "contains":
                fi.comparison = comparison_type.contains; break;

            case "--":
            case "!contains":
                fi.comparison = comparison_type.does_not_contain; break;

            case "containsany":
            case "any":
            case "in":
                fi.comparison = comparison_type.contains_any; break;

            case "containsnone":
            case "none":
            case "!in":
                fi.comparison = comparison_type.contains_none;
                break;

            case "matches":
            case "match":
            case "regex":
            case "r":
                fi.comparison = comparison_type.regex;
                break;

            default:
                return(null);
            }

            // take the rest of the text
            int compare_idx = line.IndexOf(words[1]);

            if (compare_idx + words[1].Length + 1 >= line.Length)
            {
                // nothing following the comparison
                return(null);
            }
            // 1.2.20+ - don't trim the end! - very likely the user wants the beginning/ending spaces to be in the comparison
            line = line.Substring(compare_idx + words[1].Length + 1);
            if (fi.comparison == comparison_type.contains_any || fi.comparison == comparison_type.contains_none)
            {
                fi.words    = line.Split('|');
                fi.lo_words = fi.words.Select(w => w.ToLower()).ToArray();
            }
            // 1.8.17+ - allow specifying quotes for contains or !contains
            line       = trim_quotes(line);
            fi.text    = line;
            fi.lo_text = line.ToLower();
            fi.create_regex();
            if (fi.comparison == comparison_type.regex && fi.regex_ == null)
            {
                // the regex is invalid at this point
                return(null);
            }
            return(fi);
        }