| 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: The string to search for |
| 7 | flags: String containing zero or more flags (-n, -l, -i, -v, -x) |
| 8 | files: List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | String containing all matching lines, formatted according to flags |
| 12 | """ |
| 13 | # Parse flags |
| 14 | line_numbers = '-n' in flags |
| 15 | files_only = '-l' in flags |
| 16 | case_insensitive = '-i' in flags |
| 17 | invert_match = '-v' in flags |
| 18 | exact_match = '-x' in flags |
| 19 | |
| 20 | # Edge Case: Empty pattern |
| 21 | if not pattern: |
| 22 | pattern_to_match = '' |
| 23 | else: |
| 24 | pattern_to_match = pattern.lower() if case_insensitive else pattern |
| 25 | |
| 26 | result_lines = [] |
| 27 | matching_files = set() |
| 28 | |
| 29 | # Edge Case: No files provided |
| 30 | if not files: |
| 31 | return "" |
| 32 | |
| 33 | # Edge Case: Empty files list |
| 34 | if len(files) == 0: |
| 35 | return "" |
| 36 | |
| 37 | multiple_files = len(files) > 1 |
| 38 | |
| 39 | for file_path in files: |
| 40 | try: |
| 41 | # Edge Case: File not found or cannot be opened |
| 42 | with open(file_path, 'r', encoding='utf-8') as file: |
| 43 | lines = file.readlines() |
| 44 | except FileNotFoundError: |
| 45 | # Edge Case: Handle file not found - in real grep this would show an error, but we'll skip |
| 46 | continue |
| 47 | except IOError: |
| 48 | # Edge Case: Handle other I/O errors |
| 49 | continue |
| 50 | |
| 51 | # Edge Case: File with no lines |
| 52 | if not lines: |
| 53 | # If we're looking for an empty pattern in an empty file, that's a match |
| 54 | if pattern == '' and not invert_match: |
| 55 | if files_only: |
| 56 | matching_files.add(file_path) |
| 57 | else: |
| 58 | if multiple_files: |
| 59 | result_lines.append(f"{file_path}:") |
| 60 | else: |
| 61 | result_lines.append("") |
| 62 | continue |
| 63 | |
| 64 | for line_num, line in enumerate(lines, 1): |
| 65 | # Remove trailing newline for processing |
| 66 | line_content = line.rstrip('\n') |
| 67 | |
| 68 | # Determine if line matches |
| 69 | if case_insensitive: |
| 70 | line_to_check = line_content.lower() |
| 71 | else: |
| 72 | line_to_check = line_content |
| 73 | |
| 74 | if exact_match: |
| 75 | is_match = (pattern_to_match == line_to_check) |
| 76 | else: |
| 77 | is_match = (pattern_to_match in line_to_check) |
| 78 | |
| 79 | # Edge Case: Invert match flag |
| 80 | if invert_match: |
| 81 | is_match = not is_match |
| 82 | |
| 83 | if is_match: |
| 84 | # Edge Case: Files only flag |
| 85 | if files_only: |
| 86 | matching_files.add(file_path) |
| 87 | # Optimization: break since we only need to know it matches |
| 88 | break |
| 89 | else: |
| 90 | output_line = "" |
| 91 | # Edge Case: Multiple files - prepend filename |
| 92 | if multiple_files: |
| 93 | output_line += f"{file_path}:" |
| 94 | # Edge Case: Line numbers flag |
| 95 | if line_numbers: |
| 96 | output_line += f"{line_num}:" |
| 97 | output_line += line_content |
| 98 | result_lines.append(output_line) |
| 99 | |
| 100 | # Edge Case: Files only flag - return sorted file names |
| 101 | if files_only: |
| 102 | return "\n".join(sorted(list(matching_files))) |
| 103 | |
| 104 | return "\n".join(result_lines) |
| 105 | |
| 106 | # Handled Edge Cases: Empty pattern, No files provided, Empty files list, File not found or cannot be opened, File with no lines, Invert match flag, Files only flag, Multiple files - prepend filename, Line numbers flag |