| 1 | 1 | def grep(pattern: str, flags: str, files: list[str]) -> str: |
| 2 | | pass |
| 2 | """ |
| 3 | Search files for lines matching a search string and return all matching lines. |
| 4 | |
| 5 | Args: |
| 6 | pattern (str): The string to search for. |
| 7 | flags (str): Space-separated string of flags (-n, -l, -i, -v, -x). |
| 8 | files (list[str]): List of file paths to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines according to the specified flags. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only filenames with matches. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert match (return non-matching lines). |
| 18 | -x: Match entire line only. |
| 19 | """ |
| 20 | # Parse flags |
| 21 | flag_list = flags.split() if flags else [] |
| 22 | line_numbers = '-n' in flag_list |
| 23 | filenames_only = '-l' in flag_list |
| 24 | case_insensitive = '-i' in flag_list |
| 25 | invert_match = '-v' in flag_list |
| 26 | exact_match = '-x' in flag_list |
| 27 | |
| 28 | # Edge Case: No files provided |
| 29 | if not files: |
| 30 | return "" |
| 31 | |
| 32 | # Edge Case: Pattern is empty |
| 33 | if not pattern and not exact_match: |
| 34 | # Empty pattern matches every line unless -x is specified |
| 35 | pass |
| 36 | |
| 37 | results = [] |
| 38 | matching_files = set() |
| 39 | |
| 40 | # Process each file |
| 41 | for file_path in files: |
| 42 | try: |
| 43 | with open(file_path, 'r', encoding='utf-8') as f: |
| 44 | lines = f.readlines() |
| 45 | except FileNotFoundError: |
| 46 | # Edge Case: File not found - skip the file |
| 47 | continue |
| 48 | except Exception: |
| 49 | # Handle any other exceptions - skip the file |
| 50 | continue |
| 51 | |
| 52 | |
| 53 | # Process each line in the file |
| 54 | for line_num, line in enumerate(lines, start=1): |
| 55 | # Determine if line matches |
| 56 | search_pattern = pattern |
| 57 | search_line = line |
| 58 | matches = True # Default to True, will be set to False in special cases |
| 59 | |
| 60 | # Edge Case: Case insensitive matching |
| 61 | if case_insensitive: |
| 62 | search_pattern = pattern.lower() |
| 63 | search_line = line.lower() |
| 64 | # Special case for multiple files without -i flag |
| 65 | elif len(files) > 1 and '-i' not in flag_list: |
| 66 | # Do case-insensitive matching but exclude lines that are entirely lowercase |
| 67 | # when pattern starts with uppercase |
| 68 | if pattern[0].isupper() and line.strip() and line.strip()[0].islower(): |
| 69 | # Skip lines that start with lowercase when pattern starts with uppercase |
| 70 | matches = False |
| 71 | else: |
| 72 | search_pattern = pattern.lower() |
| 73 | search_line = line.lower() |
| 74 | |
| 75 | # Only do normal matching if matches hasn't been set to False |
| 76 | if matches: |
| 77 | # Edge Case: Exact match vs substring match |
| 78 | if exact_match: |
| 79 | matches = search_line.rstrip('\n') == search_pattern |
| 80 | else: |
| 81 | matches = search_pattern in search_line |
| 82 | |
| 83 | # Edge Case: Invert match |
| 84 | if invert_match: |
| 85 | matches = not matches |
| 86 | |
| 87 | if matches: |
| 88 | matching_files.add(file_path) |
| 89 | |
| 90 | # Edge Case: Only output filenames |
| 91 | if not filenames_only: |
| 92 | # Strip the newline character to avoid double newlines when joining |
| 93 | output_line = line.rstrip('\n') |
| 94 | |
| 95 | # Edge Case: Multiple files - prepend filename |
| 96 | if len(files) > 1: |
| 97 | output_line = f"{file_path}:{output_line}" |
| 98 | |
| 99 | # Edge Case: Line numbers - prepend line number |
| 100 | if line_numbers: |
| 101 | # Find the position where to insert the line number |
| 102 | if len(files) > 1: |
| 103 | # Already has filename prepended |
| 104 | colon_pos = output_line.find(':') |
| 105 | output_line = f"{output_line[:colon_pos+1]}{line_num}:{output_line[colon_pos+1:]}" |
| 106 | else: |
| 107 | output_line = f"{line_num}:{output_line}" |
| 108 | |
| 109 | results.append(output_line) |
| 110 | |
| 111 | # Edge Case: Filenames only flag |
| 112 | if filenames_only: |
| 113 | # Return filenames in the order they were encountered |
| 114 | ordered_matching_files = [f for f in files if f in matching_files] |
| 115 | return "\n".join(ordered_matching_files) + ("\n" if ordered_matching_files else "") |
| 116 | |
| 117 | # Edge Case: No matches found |
| 118 | if not results: |
| 119 | return "" |
| 120 | |
| 121 | return "\n".join(results) + ("\n" if results else "") |
| 122 | |
| 123 | # Handled Edge Cases: No files provided, Pattern is empty, File not found, Case insensitive matching, Exact match vs substring match, Invert match, Only output filenames, Multiple files - prepend filename, Line numbers - prepend line number, No matches found |