| 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 | Edge Cases Handled: |
| 14 | - Empty files list |
| 15 | - Non-existent files |
| 16 | - Empty pattern |
| 17 | - Files with no matching lines |
| 18 | - Multiple flags used together |
| 19 | - Case insensitive matching |
| 20 | - Exact line matching |
| 21 | - Line inversion |
| 22 | - Line numbering |
| 23 | - File name output only |
| 24 | """ |
| 25 | # Parse flags |
| 26 | line_numbers = '-n' in flags |
| 27 | files_only = '-l' in flags |
| 28 | case_insensitive = '-i' in flags |
| 29 | invert_match = '-v' in flags |
| 30 | exact_match = '-x' in flags |
| 31 | |
| 32 | # Edge Case: Empty files list |
| 33 | if not files: |
| 34 | return "" |
| 35 | |
| 36 | results = [] |
| 37 | matched_files = set() |
| 38 | |
| 39 | # Process each file |
| 40 | for file_path in files: |
| 41 | try: |
| 42 | # Edge Case: Non-existent files |
| 43 | with open(file_path, 'r', encoding='utf-8') as file: |
| 44 | lines = file.readlines() |
| 45 | except FileNotFoundError: |
| 46 | # Skip non-existent files |
| 47 | continue |
| 48 | |
| 49 | # Process each line in the file |
| 50 | for line_num, line in enumerate(lines, 1): |
| 51 | # Prepare strings for comparison |
| 52 | search_pattern = pattern |
| 53 | search_line = line.rstrip('\n') |
| 54 | |
| 55 | # Edge Case: Case insensitive matching |
| 56 | if case_insensitive: |
| 57 | search_pattern = pattern.lower() |
| 58 | search_line = search_line.lower() |
| 59 | |
| 60 | # Determine if line matches |
| 61 | # Edge Case: Exact line matching |
| 62 | if exact_match: |
| 63 | is_match = search_line == search_pattern |
| 64 | else: |
| 65 | # Edge Case: Empty pattern (matches all lines) |
| 66 | is_match = search_pattern in search_line if search_pattern else True |
| 67 | |
| 68 | # Edge Case: Line inversion |
| 69 | if invert_match: |
| 70 | is_match = not is_match |
| 71 | |
| 72 | # If line matches, collect results |
| 73 | if is_match: |
| 74 | matched_files.add(file_path) |
| 75 | |
| 76 | # Edge Case: File name output only |
| 77 | if not files_only: |
| 78 | output_line = line.rstrip('\n') |
| 79 | |
| 80 | # Format output based on flags |
| 81 | if len(files) > 1: |
| 82 | output_line = f"{file_path}:{output_line}" |
| 83 | |
| 84 | # Edge Case: Line numbering |
| 85 | if line_numbers: |
| 86 | if len(files) > 1: |
| 87 | output_line = f"{file_path}:{line_num}:{output_line.split(':', 1)[1]}" |
| 88 | else: |
| 89 | output_line = f"{line_num}:{output_line}" |
| 90 | |
| 91 | results.append(output_line) |
| 92 | |
| 93 | # Edge Case: File name output only |
| 94 | if files_only: |
| 95 | # Return unique file names in order of appearance in files list |
| 96 | output_files = [f for f in files if f in matched_files] |
| 97 | return "\n".join(output_files) + ("\n" if output_files else "") |
| 98 | |
| 99 | return "\n".join(results) + ("\n" if results else "") |
| 100 | |
| 101 | # Handled Edge Cases: Empty files list, Non-existent files, Empty pattern, Files with no matching lines, Multiple flags used together, Case insensitive matching, Exact line matching, Line inversion, Line numbering, File name output only |