| 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 (str): The string to search for. |
| 7 | flags (str): Space-separated flags that modify search behavior. |
| 8 | files (list[str]): List of file paths to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines formatted according to the flags. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only filenames with matches. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert match (return non-matching lines). |
| 18 | -x: Match entire line only. |
| 19 | """ |
| 20 | # Parse flags - handle combined flags like '-in' as individual flags |
| 21 | line_numbers = 'n' in flags |
| 22 | filenames_only = 'l' in flags |
| 23 | case_insensitive = 'i' in flags |
| 24 | invert_match = 'v' in flags |
| 25 | exact_match = 'x' in flags |
| 26 | |
| 27 | # Prepare pattern for comparison |
| 28 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 29 | |
| 30 | # Results storage |
| 31 | results = [] |
| 32 | matching_files = set() |
| 33 | |
| 34 | # Process each file |
| 35 | for filename in files: |
| 36 | try: |
| 37 | with open(filename, 'r', encoding='utf-8') as file: |
| 38 | lines = file.readlines() |
| 39 | except FileNotFoundError: |
| 40 | # Edge Case: File not found - skip the file |
| 41 | continue |
| 42 | |
| 43 | # Process each line in the file |
| 44 | for line_num, line in enumerate(lines, start=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 | # Edge Case: Exact match flag - match entire line |
| 51 | matches = (compare_line == search_pattern) |
| 52 | else: |
| 53 | # Edge Case: Partial match - search pattern anywhere in line |
| 54 | matches = (search_pattern in compare_line) |
| 55 | |
| 56 | # Apply invert flag |
| 57 | if invert_match: |
| 58 | # Edge Case: Invert match flag - flip match result |
| 59 | matches = not matches |
| 60 | |
| 61 | # If line matches criteria |
| 62 | if matches: |
| 63 | # Edge Case: Filenames only flag - collect matching filenames |
| 64 | if filenames_only: |
| 65 | matching_files.add(filename) |
| 66 | # No need to process more lines for this file once we know it matches |
| 67 | break |
| 68 | else: |
| 69 | # Build output line |
| 70 | output_line = "" |
| 71 | # Edge Case: Multiple files - prepend filename |
| 72 | if len(files) > 1: |
| 73 | output_line += f"{filename}:" |
| 74 | # Edge Case: Line numbers flag - prepend line number |
| 75 | if line_numbers: |
| 76 | output_line += f"{line_num}:" |
| 77 | output_line += line |
| 78 | results.append(output_line) |
| 79 | |
| 80 | # Edge Case: Filenames only flag - return only filenames |
| 81 | if filenames_only: |
| 82 | # Return filenames in the order they were encountered |
| 83 | ordered_files = [f for f in files if f in matching_files] |
| 84 | return "\n".join(ordered_files) + ("\n" if ordered_files else "") |
| 85 | |
| 86 | # Edge Case: No matches - return empty string |
| 87 | return "".join(results) |
| 88 | |
| 89 | # Handled Edge Cases: File not found, exact match flag, partial match, invert match flag, filenames only flag, multiple files, line numbers flag, no matches |