| 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 flags for customizing behavior (-n, -l, -i, -v, -x) |
| 8 | files (list[str]): List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines, formatted according to flags |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers |
| 15 | -l: Output only file names with matches |
| 16 | -i: Case-insensitive matching |
| 17 | -v: Invert matching (return non-matching lines) |
| 18 | -x: Match entire line only |
| 19 | """ |
| 20 | # Parse flags |
| 21 | line_numbers = 'n' in flags |
| 22 | filenames_only = 'l' in flags |
| 23 | case_insensitive = 'i' in flags |
| 24 | invert_match = 'v' in flags |
| 25 | exact_match = 'x' in flags |
| 26 | |
| 27 | # Prepare pattern for comparison |
| 28 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 29 | |
| 30 | # Edge Case: No files provided |
| 31 | if not files: |
| 32 | return "" |
| 33 | |
| 34 | results = [] |
| 35 | matching_files = set() |
| 36 | |
| 37 | # Edge Case: File not found or inaccessible |
| 38 | for filename in files: |
| 39 | try: |
| 40 | with open(filename, 'r', encoding='utf-8') as file: |
| 41 | lines = file.readlines() |
| 42 | except FileNotFoundError: |
| 43 | # Skip files that don't exist |
| 44 | continue |
| 45 | except IOError: |
| 46 | # Skip files that can't be read |
| 47 | continue |
| 48 | |
| 49 | file_has_match = False |
| 50 | |
| 51 | # Edge Case: Empty file |
| 52 | if not lines: |
| 53 | # Process empty file - no lines to match |
| 54 | pass |
| 55 | |
| 56 | for line_num, line in enumerate(lines, 1): |
| 57 | # Prepare line for comparison |
| 58 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 59 | |
| 60 | # Determine if line matches |
| 61 | if exact_match: |
| 62 | matches = (compare_line == search_pattern) |
| 63 | else: |
| 64 | matches = (search_pattern in compare_line) |
| 65 | |
| 66 | # Apply invert flag |
| 67 | if invert_match: |
| 68 | matches = not matches |
| 69 | |
| 70 | if matches: |
| 71 | file_has_match = True |
| 72 | |
| 73 | # Edge Case: Requesting only filenames |
| 74 | if filenames_only: |
| 75 | matching_files.add(filename) |
| 76 | # No need to process more lines for this file when only filenames are needed |
| 77 | break |
| 78 | |
| 79 | # Build output line |
| 80 | output_line = "" |
| 81 | |
| 82 | # Edge Case: Multiple files - prepend filename |
| 83 | if len(files) > 1: |
| 84 | output_line += f"{filename}:" |
| 85 | |
| 86 | # Edge Case: Line numbers requested |
| 87 | if line_numbers: |
| 88 | output_line += f"{line_num}:" |
| 89 | |
| 90 | output_line += line |
| 91 | results.append(output_line) |
| 92 | |
| 93 | # Edge Case: Filenames only mode and this file had matches |
| 94 | if filenames_only and file_has_match: |
| 95 | matching_files.add(filename) |
| 96 | |
| 97 | # Edge Case: Filenames only mode - return unique filenames |
| 98 | if filenames_only: |
| 99 | if matching_files: |
| 100 | return "\n".join(sorted(list(matching_files))) + "\n" |
| 101 | return "" |
| 102 | |
| 103 | # Edge Case: No matches found |
| 104 | return "".join(results) |
| 105 | |
| 106 | # Handled Edge Cases: No files provided, File not found or inaccessible, Empty file, Requesting only filenames, Multiple files - prepend filename, Line numbers requested, Filenames only mode - return unique filenames, No matches found |