| 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): Flags for customizing the command's behavior. |
| 8 | files (list[str]): One or more files to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: All matching lines, possibly with file names and line numbers prepended. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only file names. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert matching. |
| 18 | -x: Match entire line only. |
| 19 | """ |
| 20 | # Parse flags |
| 21 | line_numbers = '-n' in flags |
| 22 | files_only = '-l' in flags |
| 23 | case_insensitive = '-i' in flags |
| 24 | invert_match = '-v' in flags |
| 25 | exact_match = '-x' in flags |
| 26 | |
| 27 | # Edge Case: Empty pattern |
| 28 | if not pattern: |
| 29 | pattern_to_match = '' |
| 30 | else: |
| 31 | pattern_to_match = pattern.lower() if case_insensitive else pattern |
| 32 | |
| 33 | result_lines = [] |
| 34 | matching_files = set() |
| 35 | |
| 36 | # Edge Case: No files provided |
| 37 | if not files: |
| 38 | return "" |
| 39 | |
| 40 | # Edge Case: Empty files list |
| 41 | if len(files) == 0: |
| 42 | return "" |
| 43 | |
| 44 | multiple_files = len(files) > 1 |
| 45 | |
| 46 | for file_path in files: |
| 47 | try: |
| 48 | # Edge Case: File not found or cannot be opened |
| 49 | with open(file_path, 'r', encoding='utf-8') as file: |
| 50 | lines = file.readlines() |
| 51 | except FileNotFoundError: |
| 52 | # Edge Case: Handle file not found gracefully |
| 53 | continue |
| 54 | except IOError: |
| 55 | # Edge Case: Handle other I/O errors |
| 56 | continue |
| 57 | |
| 58 | # Edge Case: File with no lines |
| 59 | if not lines: |
| 60 | continue |
| 61 | |
| 62 | file_has_match = False |
| 63 | |
| 64 | for line_num, line in enumerate(lines, start=1): |
| 65 | # Prepare line for comparison |
| 66 | line_to_check = line.lower() if case_insensitive else line |
| 67 | |
| 68 | # Determine if line matches |
| 69 | if exact_match: |
| 70 | matches = pattern_to_match == line_to_check.rstrip('\n\r') |
| 71 | else: |
| 72 | matches = pattern_to_match in line_to_check |
| 73 | |
| 74 | # Apply invert flag |
| 75 | if invert_match: |
| 76 | matches = not matches |
| 77 | |
| 78 | if matches: |
| 79 | file_has_match = True |
| 80 | |
| 81 | # Edge Case: Only file names requested |
| 82 | if files_only: |
| 83 | matching_files.add(file_path) |
| 84 | break # No need to check more lines in this file |
| 85 | |
| 86 | output_line = "" |
| 87 | |
| 88 | # Edge Case: Multiple files - prepend filename |
| 89 | if multiple_files: |
| 90 | output_line += f"{file_path}:" |
| 91 | |
| 92 | # Edge Case: Line numbers requested |
| 93 | if line_numbers: |
| 94 | output_line += f"{line_num}:" |
| 95 | |
| 96 | output_line += line |
| 97 | result_lines.append(output_line) |
| 98 | |
| 99 | # Edge Case: File has no matches and -l flag is used |
| 100 | if files_only and file_has_match: |
| 101 | matching_files.add(file_path) |
| 102 | |
| 103 | # Edge Case: -l flag - return only file names |
| 104 | if files_only: |
| 105 | return "\n".join(sorted(list(matching_files))) + ("\n" if matching_files else "") |
| 106 | |
| 107 | # Edge Case: No matches found |
| 108 | if not result_lines: |
| 109 | return "" |
| 110 | |
| 111 | return "".join(result_lines) |
| 112 | |
| 113 | # Handled Edge Cases: Empty pattern, No files provided, Empty files list, File not found or cannot be opened, File with no lines, Only file names requested, Multiple files - prepend filename, Line numbers requested, File has no matches and -l flag is used, -l flag - return only file names, No matches found |