| 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 | multiple_files = len(files) > 1 |
| 31 | |
| 32 | for file_path in files: |
| 33 | try: |
| 34 | with open(file_path, 'r', encoding='utf-8') as file: |
| 35 | lines = file.readlines() |
| 36 | |
| 37 | # Edge Case: File with no lines |
| 38 | if not lines and not files_only: |
| 39 | if multiple_files: |
| 40 | # Even for empty files, if they match (or don't match with -v) we might need to show something |
| 41 | # But an empty file has no lines to match, so it can't have matching lines |
| 42 | pass |
| 43 | |
| 44 | for line_num, line in enumerate(lines, 1): |
| 45 | # Prepare line for comparison |
| 46 | compare_line = line.rstrip('\n') |
| 47 | if case_insensitive: |
| 48 | compare_line = compare_line.lower() |
| 49 | |
| 50 | # Determine if line matches |
| 51 | if exact_match: |
| 52 | matches = (compare_line == search_pattern) |
| 53 | else: |
| 54 | matches = (search_pattern in compare_line) |
| 55 | |
| 56 | # Apply invert flag |
| 57 | if invert_match: |
| 58 | matches = not matches |
| 59 | |
| 60 | if matches: |
| 61 | matched_files.add(file_path) |
| 62 | |
| 63 | # Edge Case: Only output file names |
| 64 | if files_only: |
| 65 | continue |
| 66 | |
| 67 | output_line = line.rstrip('\n') |
| 68 | |
| 69 | # Build output with proper formatting |
| 70 | prefix_parts = [] |
| 71 | |
| 72 | # Edge Case: Multiple files - prepend filename |
| 73 | if multiple_files: |
| 74 | prefix_parts.append(file_path) |
| 75 | |
| 76 | # Edge Case: Line numbers flag |
| 77 | if line_numbers: |
| 78 | prefix_parts.append(str(line_num)) |
| 79 | |
| 80 | if prefix_parts: |
| 81 | output_line = ':'.join(prefix_parts) + ':' + output_line |
| 82 | |
| 83 | results.append(output_line) |
| 84 | |
| 85 | except FileNotFoundError: |
| 86 | # Edge Case: File not found - according to spec we just skip |
| 87 | continue |
| 88 | except Exception: |
| 89 | # Edge Case: Other file reading errors - skip file |
| 90 | continue |
| 91 | |
| 92 | # Edge Case: Files only flag - return unique file names in order of first appearance |
| 93 | if files_only: |
| 94 | # Preserve order of files as they appear in input |
| 95 | ordered_matched_files = [f for f in files if f in matched_files] |
| 96 | return '\n'.join(ordered_matched_files) |
| 97 | |
| 98 | return '\n'.join(results) |
| 99 | |
| 100 | # Handled Edge Cases: Empty files list, Empty pattern, File with no lines, Multiple files, Line numbers flag, Only output file names, Files only flag, File not found, Other file reading errors" |