| 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, possibly with file names and line numbers. |
| 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 | file_names_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 | # Edge Case: No files provided |
| 29 | if not files: |
| 30 | return "" |
| 31 | |
| 32 | # Edge Case: Pattern is empty |
| 33 | if not pattern: |
| 34 | case_insensitive = False # No need for case insensitivity with empty pattern |
| 35 | |
| 36 | # Prepare pattern for comparison |
| 37 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 38 | |
| 39 | results = [] |
| 40 | matching_files = set() |
| 41 | |
| 42 | for file_path in files: |
| 43 | try: |
| 44 | with open(file_path, 'r', encoding='utf-8') as file: |
| 45 | lines = file.readlines() |
| 46 | except FileNotFoundError: |
| 47 | # Edge Case: File not found - skip the file |
| 48 | continue |
| 49 | |
| 50 | file_has_match = False |
| 51 | |
| 52 | for line_num, line in enumerate(lines, 1): |
| 53 | # Prepare line for comparison |
| 54 | compare_line = line.lower() if case_insensitive else line |
| 55 | |
| 56 | # Determine if line matches |
| 57 | if exact_match: |
| 58 | matches = (compare_line.rstrip('\n\r') == search_pattern) |
| 59 | else: |
| 60 | matches = (search_pattern in compare_line) |
| 61 | |
| 62 | # Apply invert flag |
| 63 | if invert_match: |
| 64 | matches = not matches |
| 65 | |
| 66 | if matches: |
| 67 | file_has_match = True |
| 68 | |
| 69 | # Edge Case: Only file names requested |
| 70 | if file_names_only: |
| 71 | matching_files.add(file_path) |
| 72 | break # No need to check more lines in this file |
| 73 | |
| 74 | # Build output line |
| 75 | output_line = "" |
| 76 | |
| 77 | # Add file name if multiple files |
| 78 | if len(files) > 1: |
| 79 | output_line += f"{file_path}:" |
| 80 | |
| 81 | # Add line number if requested |
| 82 | if line_numbers: |
| 83 | output_line += f"{line_num}:" |
| 84 | |
| 85 | # Add the actual line |
| 86 | output_line += line |
| 87 | |
| 88 | results.append(output_line) |
| 89 | |
| 90 | # Edge Case: File names only and this file had a match |
| 91 | if file_names_only and file_has_match: |
| 92 | matching_files.add(file_path) |
| 93 | |
| 94 | # Edge Case: Only file names requested |
| 95 | if file_names_only: |
| 96 | # Return file names in the order they were encountered |
| 97 | ordered_matching_files = [f for f in files if f in matching_files] |
| 98 | return "\n".join(ordered_matching_files) + ("\n" if ordered_matching_files else "") |
| 99 | |
| 100 | return "".join(results) |
| 101 | |
| 102 | # Handled Edge Cases: No files provided; Pattern is empty; File not found; Only file names requested; Multiple files with line numbers |