| 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 | multiple_files = len(files) > 1 |
| 34 | |
| 35 | for file_path in files: |
| 36 | try: |
| 37 | # Edge Case: File cannot be opened/read |
| 38 | with open(file_path, 'r', encoding='utf-8') as file: |
| 39 | lines = file.readlines() |
| 40 | except FileNotFoundError: |
| 41 | # Edge Case: File not found - skip silently as per typical grep behavior |
| 42 | continue |
| 43 | except IOError: |
| 44 | # Edge Case: Other I/O error - skip silently |
| 45 | continue |
| 46 | |
| 47 | file_has_match = False |
| 48 | |
| 49 | for line_num, line in enumerate(lines, 1): |
| 50 | # Prepare line for comparison |
| 51 | line_to_check = line |
| 52 | if case_insensitive: |
| 53 | line_to_check = line.lower() |
| 54 | |
| 55 | # Determine if line matches |
| 56 | if exact_match: |
| 57 | matches = pattern_to_match == line_to_check.rstrip('\n\r') |
| 58 | else: |
| 59 | matches = pattern_to_match in line_to_check |
| 60 | |
| 61 | # Apply invert flag |
| 62 | if invert_match: |
| 63 | matches = not matches |
| 64 | |
| 65 | if matches: |
| 66 | file_has_match = True |
| 67 | |
| 68 | # Edge Case: Only file names requested |
| 69 | if files_only: |
| 70 | matching_files.add(file_path) |
| 71 | break # No need to check more lines in this file |
| 72 | |
| 73 | # Build output line |
| 74 | output_line = "" |
| 75 | |
| 76 | # Add filename prefix if multiple files |
| 77 | if multiple_files: |
| 78 | output_line += f"{file_path}:" |
| 79 | |
| 80 | # Add line number if requested |
| 81 | if line_numbers: |
| 82 | output_line += f"{line_num}:" |
| 83 | |
| 84 | # Add the actual line content |
| 85 | output_line += line |
| 86 | |
| 87 | result_lines.append(output_line) |
| 88 | |
| 89 | # Edge Case: Files only mode and this file had a match |
| 90 | if files_only and file_has_match: |
| 91 | matching_files.add(file_path) |
| 92 | |
| 93 | # Edge Case: Files only mode - return file names |
| 94 | if files_only: |
| 95 | return "\n".join(sorted(list(matching_files))) + ("\n" if matching_files else "") |
| 96 | |
| 97 | # Edge Case: No matches found |
| 98 | if not result_lines: |
| 99 | return "" |
| 100 | |
| 101 | return "".join(result_lines) |
| 102 | |
| 103 | # Handled Edge Cases: Empty pattern, No files provided, File cannot be opened/read, File not found, Other I/O error, Only file names requested, Files only mode, No matches found |