| 1 | 1 | def grep(pattern: str, flags: str, files: list[str]) -> str: |
| 2 | | pass |
| 2 | # Parse flags |
| 3 | line_numbers = '-n' in flags |
| 4 | files_only = '-l' in flags |
| 5 | case_insensitive = '-i' in flags |
| 6 | invert_match = '-v' in flags |
| 7 | exact_match = '-x' in flags |
| 8 | |
| 9 | # Prepare pattern for comparison |
| 10 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 11 | |
| 12 | # Results storage |
| 13 | results = [] |
| 14 | matched_files = set() |
| 15 | |
| 16 | # Process each file |
| 17 | for filename in files: |
| 18 | try: |
| 19 | with open(filename, 'r', encoding='utf-8') as file: |
| 20 | # Edge Case: Handle files that cannot be read |
| 21 | lines = file.readlines() |
| 22 | except FileNotFoundError: |
| 23 | # Edge Case: Handle files that do not exist |
| 24 | continue |
| 25 | except IOError: |
| 26 | # Edge Case: Handle IO errors when reading files |
| 27 | continue |
| 28 | |
| 29 | # Process each line |
| 30 | for line_num, line in enumerate(lines, 1): |
| 31 | # Prepare line for comparison |
| 32 | compare_line = line.lower() if case_insensitive else line |
| 33 | |
| 34 | # Determine if line matches |
| 35 | if exact_match: |
| 36 | # Edge Case: Exact match flag requires the entire line to match the pattern |
| 37 | matches = search_pattern == compare_line.rstrip('\n') |
| 38 | else: |
| 39 | # Edge Case: Normal match checks if pattern is contained in the line |
| 40 | matches = search_pattern in compare_line |
| 41 | |
| 42 | # Apply invert flag |
| 43 | if invert_match: |
| 44 | # Edge Case: Invert match inverts the matching logic |
| 45 | matches = not matches |
| 46 | |
| 47 | # If line matches, collect results |
| 48 | if matches: |
| 49 | matched_files.add(filename) |
| 50 | |
| 51 | # If only listing files, no need to collect line details |
| 52 | if files_only: |
| 53 | continue |
| 54 | |
| 55 | # Build output line |
| 56 | output_line = "" |
| 57 | |
| 58 | # Add filename prefix if multiple files |
| 59 | if len(files) > 1: |
| 60 | # Edge Case: Filename is prepended when searching multiple files |
| 61 | output_line += f"{filename}:" |
| 62 | |
| 63 | # Add line number if requested |
| 64 | if line_numbers: |
| 65 | # Edge Case: Line number is added when -n flag is used |
| 66 | output_line += f"{line_num}:" |
| 67 | |
| 68 | # Add the actual line content |
| 69 | output_line += line |
| 70 | |
| 71 | results.append(output_line) |
| 72 | |
| 73 | # If files only flag is set, return just the filenames |
| 74 | if files_only: |
| 75 | # Edge Case: Only filenames are returned when -l flag is used |
| 76 | # Edge Case: Filenames are returned in the order they were found |
| 77 | return "\n".join(sorted(list(matched_files))) + ("\n" if matched_files else "") |
| 78 | |
| 79 | # Edge Case: Results are joined with newlines |
| 80 | # Edge Case: No trailing newline is added if there are no results |
| 81 | return "".join(results) |
| 82 | |
| 83 | # Handled Edge Cases: Files that cannot be read; Files that do not exist; IO errors when reading files; Exact match flag requires the entire line to match the pattern; Normal match checks if pattern is contained in the line; Invert match inverts the matching logic; Filename is prepended when searching multiple files; Line number is added when -n flag is used; Only filenames are returned when -l flag is used; Filenames are returned in the order they were found; Results are joined with newlines; No trailing newline is added if there are no results |