| 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 for customizing 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 file names. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert matching. |
| 18 | -x: Match entire line only. |
| 19 | """ |
| 20 | # Parse flags |
| 21 | flag_list = flags.split() if flags else [] |
| 22 | line_numbers = '-n' in flag_list |
| 23 | files_only = '-l' in flag_list |
| 24 | case_insensitive = '-i' in flag_list |
| 25 | invert_match = '-v' in flag_list |
| 26 | exact_match = '-x' in flag_list |
| 27 | |
| 28 | # Prepare pattern for comparison |
| 29 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 30 | |
| 31 | # Initialize results |
| 32 | results = [] |
| 33 | matching_files = set() |
| 34 | |
| 35 | # Process each file |
| 36 | for file_path in files: |
| 37 | try: |
| 38 | with open(file_path, 'r', encoding='utf-8') as file: |
| 39 | lines = file.readlines() |
| 40 | except FileNotFoundError: |
| 41 | # Edge Case: File not found - skip the file |
| 42 | continue |
| 43 | |
| 44 | # Process each line |
| 45 | for line_num, line in enumerate(lines, start=1): |
| 46 | # Prepare line for comparison |
| 47 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 48 | |
| 49 | # Determine if line matches |
| 50 | if exact_match: |
| 51 | match = (compare_line == search_pattern) |
| 52 | else: |
| 53 | match = (search_pattern in compare_line) |
| 54 | |
| 55 | # Apply invert flag |
| 56 | if invert_match: |
| 57 | match = not match |
| 58 | |
| 59 | # If line matches, process according to flags |
| 60 | if match: |
| 61 | # Edge Case: Only file names requested |
| 62 | if files_only: |
| 63 | matching_files.add(file_path) |
| 64 | # No need to process more lines for this file once we know it matches |
| 65 | break |
| 66 | else: |
| 67 | # Build output line |
| 68 | output_line = "" |
| 69 | # Edge Case: Multiple files - prepend filename |
| 70 | if len(files) > 1: |
| 71 | output_line += f"{file_path}:" |
| 72 | # Edge Case: Line numbers requested |
| 73 | if line_numbers: |
| 74 | output_line += f"{line_num}:" |
| 75 | output_line += line |
| 76 | results.append(output_line) |
| 77 | |
| 78 | # Edge Case: Only file names requested |
| 79 | if files_only: |
| 80 | # Return file names in the order they were encountered |
| 81 | ordered_matching_files = [f for f in files if f in matching_files] |
| 82 | return "\n".join(ordered_matching_files) + ("\n" if ordered_matching_files else "") |
| 83 | |
| 84 | # Edge Case: No matches found |
| 85 | if not results: |
| 86 | return "" |
| 87 | |
| 88 | return "".join(results) |
| 89 | |
| 90 | # Handled Edge Cases: File not found, multiple files, line numbers, only file names, case insensitive matching, invert matching, exact line matching, no matches found |