| 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 command line 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 | results = [] |
| 21 | matched_files = set() |
| 22 | |
| 23 | # Edge Case: Empty files list |
| 24 | if not files: |
| 25 | return "" |
| 26 | |
| 27 | # Edge Case: Empty pattern |
| 28 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 29 | |
| 30 | for file_path in files: |
| 31 | try: |
| 32 | # Edge Case: File not found or inaccessible |
| 33 | with open(file_path, 'r', encoding='utf-8') as file: |
| 34 | lines = file.readlines() |
| 35 | except FileNotFoundError: |
| 36 | # Edge Case: Continue processing other files if one is not found |
| 37 | continue |
| 38 | except IOError: |
| 39 | # Edge Case: Handle other I/O errors |
| 40 | continue |
| 41 | |
| 42 | file_has_match = False |
| 43 | |
| 44 | for line_num, line in enumerate(lines, 1): |
| 45 | # Prepare line for comparison |
| 46 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 47 | |
| 48 | # Determine if line matches |
| 49 | if exact_match: |
| 50 | matches = (compare_line == search_pattern) |
| 51 | else: |
| 52 | matches = (search_pattern in compare_line) |
| 53 | |
| 54 | # Apply invert flag |
| 55 | if invert_match: |
| 56 | matches = not matches |
| 57 | |
| 58 | if matches: |
| 59 | file_has_match = True |
| 60 | |
| 61 | # Edge Case: When -l flag is used, we only need to know file has a match |
| 62 | if files_only: |
| 63 | matched_files.add(file_path) |
| 64 | break # No need to check more lines in this file |
| 65 | |
| 66 | # Build output line |
| 67 | output_line = "" |
| 68 | |
| 69 | # Add filename prefix if multiple files |
| 70 | if len(files) > 1: |
| 71 | output_line += f"{file_path}:" |
| 72 | |
| 73 | # Add line number if requested |
| 74 | if line_numbers: |
| 75 | output_line += f"{line_num}:" |
| 76 | |
| 77 | # Add the actual line content |
| 78 | output_line += line |
| 79 | |
| 80 | results.append(output_line) |
| 81 | |
| 82 | # Edge Case: Handle -l flag for current file |
| 83 | if files_only and file_has_match: |
| 84 | matched_files.add(file_path) |
| 85 | |
| 86 | # Edge Case: When -l flag is used, return only file names |
| 87 | if files_only: |
| 88 | # Return files in the order they were specified |
| 89 | output_files = [f for f in files if f in matched_files] |
| 90 | return "\n".join(output_files) + ("\n" if output_files else "") |
| 91 | |
| 92 | # Edge Case: No matches found |
| 93 | return "".join(results) |
| 94 | |
| 95 | # Handled Edge Cases: Empty files list, empty pattern, file not found, other I/O errors, continue processing other files when one fails, -l flag optimization, multiple files formatting, line numbers formatting, case insensitive matching, invert matching, exact line matching, no matches found |