| 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 file list - return empty string |
| 24 | if not files: |
| 25 | return "" |
| 26 | |
| 27 | # Edge Case: Empty pattern - match empty lines |
| 28 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 29 | |
| 30 | for file_path in files: |
| 31 | try: |
| 32 | with open(file_path, 'r', encoding='utf-8') as file: |
| 33 | lines = file.readlines() |
| 34 | |
| 35 | # Edge Case: File not found or cannot be read - skip file |
| 36 | except FileNotFoundError: |
| 37 | continue |
| 38 | except IOError: |
| 39 | continue |
| 40 | |
| 41 | multiple_files = len(files) > 1 |
| 42 | |
| 43 | for line_num, line in enumerate(lines, 1): |
| 44 | # Remove newline for processing but keep for output |
| 45 | line_content = line.rstrip('\n') |
| 46 | |
| 47 | # Edge Case: Case insensitive matching |
| 48 | compare_line = line_content.lower() if case_insensitive else line_content |
| 49 | |
| 50 | # Edge Case: Exact match flag |
| 51 | if exact_match: |
| 52 | match = (compare_line == search_pattern) |
| 53 | else: |
| 54 | match = (search_pattern in compare_line) |
| 55 | |
| 56 | # Edge Case: Invert match flag |
| 57 | if invert_match: |
| 58 | match = not match |
| 59 | |
| 60 | if match: |
| 61 | if files_only: |
| 62 | matched_files.add(file_path) |
| 63 | # No need to process more lines in this file |
| 64 | break |
| 65 | else: |
| 66 | output = "" |
| 67 | # Edge Case: Multiple files - prepend filename |
| 68 | if multiple_files: |
| 69 | output += f"{file_path}:" |
| 70 | # Edge Case: Line numbers flag |
| 71 | if line_numbers: |
| 72 | output += f"{line_num}:" |
| 73 | output += line |
| 74 | results.append(output) |
| 75 | |
| 76 | # Edge Case: Files only flag and we found a match in this file |
| 77 | if files_only and file_path in matched_files: |
| 78 | continue |
| 79 | |
| 80 | # Edge Case: Files only flag - return only file names |
| 81 | if files_only: |
| 82 | return "\n".join(sorted(matched_files)) + ("\n" if matched_files else "") |
| 83 | |
| 84 | return "".join(results) |
| 85 | |
| 86 | # Handled Edge Cases: Empty file list, empty pattern, file not found, case insensitive matching, exact match flag, invert match flag, multiple files with filename prepending, line numbers flag, files only flag" |