| 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 the 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 file names with matches. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert match (return non-matching lines). |
| 18 | -x: Match entire line only. |
| 19 | |
| 20 | Edge Cases Handled: |
| 21 | - Empty file list |
| 22 | - File not found |
| 23 | - Empty pattern |
| 24 | - Pattern not found in any files |
| 25 | - Multiple flags used together |
| 26 | - Files with no matching lines |
| 27 | """ |
| 28 | # Parse flags |
| 29 | flag_list = flags.split() if flags else [] |
| 30 | line_numbers = '-n' in flag_list |
| 31 | files_only = '-l' in flag_list |
| 32 | case_insensitive = '-i' in flag_list |
| 33 | invert_match = '-v' in flag_list |
| 34 | exact_match = '-x' in flag_list |
| 35 | |
| 36 | # Edge Case: Empty file list |
| 37 | if not files: |
| 38 | return "" |
| 39 | |
| 40 | results = [] |
| 41 | matching_files = set() |
| 42 | |
| 43 | # Prepare pattern for comparison |
| 44 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 45 | |
| 46 | # Process each file |
| 47 | for file_path in files: |
| 48 | try: |
| 49 | # Edge Case: File not found |
| 50 | with open(file_path, 'r', encoding='utf-8') as file: |
| 51 | lines = file.readlines() |
| 52 | except FileNotFoundError: |
| 53 | # Skip files that cannot be opened |
| 54 | continue |
| 55 | |
| 56 | # Process each line in the file |
| 57 | for line_num, line in enumerate(lines, start=1): |
| 58 | # Prepare line for comparison |
| 59 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 60 | |
| 61 | # Determine if line matches |
| 62 | if exact_match: |
| 63 | is_match = (search_pattern == compare_line) |
| 64 | else: |
| 65 | is_match = (search_pattern in compare_line) |
| 66 | |
| 67 | # Apply invert flag |
| 68 | if invert_match: |
| 69 | is_match = not is_match |
| 70 | |
| 71 | # Edge Case: Empty pattern |
| 72 | if pattern == "": |
| 73 | # Empty pattern matches all lines unless inverted |
| 74 | is_match = not invert_match |
| 75 | |
| 76 | if is_match: |
| 77 | matching_files.add(file_path) |
| 78 | |
| 79 | # If we only need file names, we can break early |
| 80 | if files_only: |
| 81 | break |
| 82 | |
| 83 | # Format output line |
| 84 | output_line = "" |
| 85 | |
| 86 | # Add filename prefix if multiple files |
| 87 | if len(files) > 1: |
| 88 | output_line += f"{file_path}:" |
| 89 | |
| 90 | # Add line number if requested |
| 91 | if line_numbers: |
| 92 | output_line += f"{line_num}:" |
| 93 | |
| 94 | # Add the actual line content |
| 95 | output_line += line |
| 96 | |
| 97 | results.append(output_line) |
| 98 | |
| 99 | # If we only need file names and we found a match, we can move to next file |
| 100 | if files_only and file_path in matching_files: |
| 101 | continue |
| 102 | |
| 103 | # Edge Case: Pattern not found in any files |
| 104 | if files_only: |
| 105 | # Return only the names of files with matches |
| 106 | return "\n".join(sorted(matching_files)) + ("\n" if matching_files else "") |
| 107 | |
| 108 | # Edge Case: Multiple flags used together |
| 109 | # Return all matching lines |
| 110 | return "".join(results) |
| 111 | |
| 112 | # Handled Edge Cases: Empty file list, File not found, Empty pattern, Pattern not found in any files, Multiple flags used together, Files with no matching lines |