| 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 names 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_search = '' |
| 23 | else: |
| 24 | pattern_to_search = 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 | multiple_files = len(files) > 1 |
| 34 | |
| 35 | for file_name in files: |
| 36 | try: |
| 37 | with open(file_name, 'r', encoding='utf-8') as file: |
| 38 | lines = file.readlines() |
| 39 | |
| 40 | # Edge Case: File is empty or unreadable |
| 41 | if not lines: |
| 42 | continue |
| 43 | |
| 44 | for line_num, line in enumerate(lines, 1): |
| 45 | # Remove newline for processing but keep it for output |
| 46 | line_content = line.rstrip('\n') |
| 47 | |
| 48 | # Determine if line matches |
| 49 | if case_insensitive: |
| 50 | line_to_check = line_content.lower() |
| 51 | else: |
| 52 | line_to_check = line_content |
| 53 | |
| 54 | if exact_match: |
| 55 | is_match = pattern_to_search == line_to_check |
| 56 | else: |
| 57 | is_match = pattern_to_search in line_to_check |
| 58 | |
| 59 | # Edge Case: Invert match flag |
| 60 | if invert_match: |
| 61 | is_match = not is_match |
| 62 | |
| 63 | if is_match: |
| 64 | matching_files.add(file_name) |
| 65 | |
| 66 | # Edge Case: Only output file names |
| 67 | if files_only: |
| 68 | continue |
| 69 | |
| 70 | output_line = "" |
| 71 | |
| 72 | # Edge Case: Multiple files - prepend filename |
| 73 | if multiple_files: |
| 74 | output_line += f"{file_name}:" |
| 75 | |
| 76 | # Edge Case: Line numbers flag |
| 77 | if line_numbers: |
| 78 | output_line += f"{line_num}:" |
| 79 | |
| 80 | output_line += line |
| 81 | result_lines.append(output_line) |
| 82 | |
| 83 | # Edge Case: File not found or permission denied |
| 84 | except (FileNotFoundError, PermissionError): |
| 85 | # According to typical grep behavior, we silently skip files that cannot be opened |
| 86 | continue |
| 87 | |
| 88 | # Edge Case: Files only flag - return unique file names |
| 89 | if files_only: |
| 90 | return "\n".join(sorted(list(matching_files))) + ("\n" if matching_files else "") |
| 91 | |
| 92 | # Edge Case: No matches found |
| 93 | if not result_lines: |
| 94 | return "" |
| 95 | |
| 96 | return "".join(result_lines) |
| 97 | |
| 98 | # Handled Edge Cases: Empty pattern, No files provided, File is empty or unreadable, Invert match flag, Only output file names, Multiple files - prepend filename, Line numbers flag, Files only flag - return unique file names, No matches found, File not found or permission denied |