| 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 - handle both space-separated and combined flags |
| 21 | if flags: |
| 22 | # Handle combined flags like "-in" or "-invx" |
| 23 | # Remove dashes and collect individual flag characters |
| 24 | all_flags = flags.replace('-', '') |
| 25 | line_numbers = 'n' in all_flags |
| 26 | file_names_only = 'l' in all_flags |
| 27 | case_insensitive = 'i' in all_flags |
| 28 | invert_match = 'v' in all_flags |
| 29 | exact_match = 'x' in all_flags |
| 30 | else: |
| 31 | line_numbers = False |
| 32 | file_names_only = False |
| 33 | case_insensitive = False |
| 34 | invert_match = False |
| 35 | exact_match = False |
| 36 | |
| 37 | # Edge Case: No files provided |
| 38 | if not files: |
| 39 | return "" |
| 40 | |
| 41 | # Edge Case: Pattern is empty |
| 42 | if not pattern: |
| 43 | exact_match = False # Cannot match entire line if pattern is empty |
| 44 | |
| 45 | results = [] |
| 46 | matching_files = set() |
| 47 | |
| 48 | # Prepare pattern for case-insensitive matching |
| 49 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 50 | |
| 51 | # Process each file |
| 52 | for file_path in files: |
| 53 | try: |
| 54 | with open(file_path, 'r', encoding='utf-8') as f: |
| 55 | lines = f.readlines() |
| 56 | except FileNotFoundError: |
| 57 | # Edge Case: File not found - skip the file |
| 58 | continue |
| 59 | except Exception: |
| 60 | # Edge Case: Other file reading errors - skip the file |
| 61 | continue |
| 62 | |
| 63 | file_has_match = False |
| 64 | |
| 65 | # Process each line |
| 66 | for line_num, line in enumerate(lines, start=1): |
| 67 | # Prepare line for comparison |
| 68 | compare_line = line.lower() if case_insensitive else line |
| 69 | |
| 70 | # Determine if line matches |
| 71 | if exact_match: |
| 72 | matches = (compare_line.rstrip('\n\r') == search_pattern) |
| 73 | else: |
| 74 | matches = (search_pattern in compare_line) |
| 75 | |
| 76 | # Apply invert flag |
| 77 | if invert_match: |
| 78 | matches = not matches |
| 79 | |
| 80 | if matches: |
| 81 | file_has_match = True |
| 82 | |
| 83 | # If we only need file names, we can break early |
| 84 | if file_names_only: |
| 85 | matching_files.add(file_path) |
| 86 | break |
| 87 | |
| 88 | # Build output line |
| 89 | output_line = "" |
| 90 | |
| 91 | # Add file name if multiple files |
| 92 | if len(files) > 1: |
| 93 | output_line += f"{file_path}:" |
| 94 | |
| 95 | # Add line number if requested |
| 96 | if line_numbers: |
| 97 | output_line += f"{line_num}:" |
| 98 | |
| 99 | # Add the actual line |
| 100 | output_line += line |
| 101 | |
| 102 | results.append(output_line) |
| 103 | |
| 104 | # If we're only collecting file names and this file had a match |
| 105 | if file_names_only and file_has_match: |
| 106 | matching_files.add(file_path) |
| 107 | |
| 108 | # Edge Case: No matches found |
| 109 | if file_names_only: |
| 110 | # Preserve the order of files as they were provided |
| 111 | ordered_matching_files = [f for f in files if f in matching_files] |
| 112 | return "\n".join(ordered_matching_files) + ("\n" if ordered_matching_files else "") |
| 113 | |
| 114 | # Edge Case: No results to return |
| 115 | if not results: |
| 116 | return "" |
| 117 | |
| 118 | return "".join(results) |
| 119 | |
| 120 | # Handled Edge Cases: No files provided, pattern is empty, file not found, other file reading errors, no matches found, no results to return |