| 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 | results = [] |
| 27 | matched_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 inaccessible |
| 42 | with open(file_path, 'r', encoding='utf-8') as file: |
| 43 | lines = file.readlines() |
| 44 | except FileNotFoundError: |
| 45 | # Edge Case: Continue processing other files if one is not found |
| 46 | continue |
| 47 | except IOError: |
| 48 | # Edge Case: Handle other I/O errors |
| 49 | continue |
| 50 | |
| 51 | file_has_match = False |
| 52 | |
| 53 | for line_num, line in enumerate(lines, 1): |
| 54 | # Process line for comparison |
| 55 | line_to_check = line.lower() if case_insensitive else line |
| 56 | |
| 57 | # Determine if line matches |
| 58 | if exact_match: |
| 59 | matches = pattern_to_match == line_to_check.rstrip('\n') |
| 60 | else: |
| 61 | matches = pattern_to_match in line_to_check |
| 62 | |
| 63 | # Apply invert flag |
| 64 | if invert_match: |
| 65 | matches = not matches |
| 66 | |
| 67 | if matches: |
| 68 | file_has_match = True |
| 69 | |
| 70 | # Edge Case: When -l flag is used, we only need to know file has a match |
| 71 | if files_only: |
| 72 | matched_files.add(file_path) |
| 73 | break # No need to check more lines in this file |
| 74 | |
| 75 | # Build output line |
| 76 | output_line = "" |
| 77 | |
| 78 | # Add filename prefix if multiple files |
| 79 | if multiple_files: |
| 80 | output_line += f"{file_path}:" |
| 81 | |
| 82 | # Add line number if requested |
| 83 | if line_numbers: |
| 84 | output_line += f"{line_num}:" |
| 85 | |
| 86 | output_line += line |
| 87 | results.append(output_line) |
| 88 | |
| 89 | # Edge Case: When -l flag is used and we found a match in this file |
| 90 | if files_only and file_has_match: |
| 91 | matched_files.add(file_path) |
| 92 | |
| 93 | # Edge Case: When -l flag is used, return only file names |
| 94 | if files_only: |
| 95 | return "\n".join(sorted(matched_files)) + ("\n" if matched_files else "") |
| 96 | |
| 97 | # Edge Case: No matches found |
| 98 | if not results: |
| 99 | return "" |
| 100 | |
| 101 | return "".join(results) |
| 102 | |
| 103 | # Handled Edge Cases: Empty pattern, No files provided, Empty files list, File not found or inaccessible, Continue processing other files if one is not found, Handle other I/O errors, When -l flag is used, we only need to know file has a match, When -l flag is used and we found a match in this file, No matches found |