| 1 | 1 | def grep(pattern: str, flags: str, files: list[str]) -> str: |
| 2 | | pass |
| 2 | """ |
| 3 | Search for lines matching a pattern in files and return matching lines. |
| 4 | |
| 5 | Args: |
| 6 | pattern: The string to search for |
| 7 | flags: Space-separated string of flags (-n, -l, -i, -v, -x) |
| 8 | files: List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | String containing matching lines, formatted according to flags |
| 12 | |
| 13 | Edge Cases Handled: |
| 14 | - Empty files list |
| 15 | - Single file vs multiple files (filename prefix) |
| 16 | - Case insensitive matching |
| 17 | - Inverting match results |
| 18 | - Exact line matching |
| 19 | - Line numbering |
| 20 | - Files with no matches |
| 21 | - Only filename output |
| 22 | """ |
| 23 | # Parse flags |
| 24 | flag_list = flags.split() if flags else [] |
| 25 | case_insensitive = '-i' in flag_list |
| 26 | invert_match = '-v' in flag_list |
| 27 | exact_match = '-x' in flag_list |
| 28 | line_numbers = '-n' in flag_list |
| 29 | files_only = '-l' in flag_list |
| 30 | |
| 31 | # Edge Case: Empty files list |
| 32 | if not files: |
| 33 | return "" |
| 34 | |
| 35 | results = [] |
| 36 | matched_files = set() |
| 37 | |
| 38 | # Prepare pattern for case-insensitive comparison |
| 39 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 40 | |
| 41 | # Process each file |
| 42 | for file_path in files: |
| 43 | try: |
| 44 | with open(file_path, 'r') as file: |
| 45 | lines = file.readlines() |
| 46 | except FileNotFoundError: |
| 47 | # Edge Case: File not found - skip silently as per typical grep behavior |
| 48 | continue |
| 49 | |
| 50 | # Edge Case: Empty file |
| 51 | if not lines: |
| 52 | continue |
| 53 | |
| 54 | file_has_match = False |
| 55 | |
| 56 | # Process each line |
| 57 | for line_num, line in enumerate(lines, 1): |
| 58 | # Prepare line for comparison |
| 59 | compare_line = line.lower() if case_insensitive else line |
| 60 | |
| 61 | # Determine if line matches |
| 62 | if exact_match: |
| 63 | match = (compare_line.rstrip('\n\r') == search_pattern) |
| 64 | else: |
| 65 | match = (search_pattern in compare_line) |
| 66 | |
| 67 | # Apply invert flag |
| 68 | if invert_match: |
| 69 | match = not match |
| 70 | |
| 71 | # Edge Case: Match found |
| 72 | if match: |
| 73 | file_has_match = True |
| 74 | |
| 75 | # Edge Case: Only output filenames |
| 76 | if files_only: |
| 77 | matched_files.add(file_path) |
| 78 | break # No need to check more lines in this file |
| 79 | |
| 80 | # Build output line |
| 81 | output_line = "" |
| 82 | |
| 83 | # Edge Case: Multiple files - prepend filename |
| 84 | if len(files) > 1: |
| 85 | output_line += f"{file_path}:" |
| 86 | |
| 87 | # Edge Case: Line numbers flag |
| 88 | if line_numbers: |
| 89 | output_line += f"{line_num}:" |
| 90 | |
| 91 | output_line += line |
| 92 | results.append(output_line) |
| 93 | |
| 94 | # Edge Case: Files only mode and this file had a match |
| 95 | if files_only and file_has_match: |
| 96 | matched_files.add(file_path) |
| 97 | |
| 98 | # Edge Case: Only output filenames |
| 99 | if files_only: |
| 100 | # Return filenames in the order they appeared in the input |
| 101 | ordered_matches = [f for f in files if f in matched_files] |
| 102 | return "\n".join(ordered_matches) + ("\n" if ordered_matches else "") |
| 103 | |
| 104 | # Edge Case: Normal output |
| 105 | return "".join(results) |
| 106 | |
| 107 | # Handled Edge Cases: Empty files list, single file vs multiple files, case insensitive matching, |
| 108 | # inverting match results, exact line matching, line numbering, files with no matches, |
| 109 | # only filename output, file not found, empty file |