| 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: The string to search for |
| 7 | flags: String containing flag characters (-n, -l, -i, -v, -x) |
| 8 | files: List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | String containing all matching lines, formatted according to flags |
| 12 | """ |
| 13 | # Parse flags - handle combined flags like "-ni" |
| 14 | line_numbers = 'n' in flags |
| 15 | files_only = 'l' in flags |
| 16 | case_insensitive = 'i' in flags |
| 17 | invert_match = 'v' in flags |
| 18 | exact_match = 'x' in flags |
| 19 | |
| 20 | # Edge Case: Empty pattern |
| 21 | if not pattern: |
| 22 | pattern = '' |
| 23 | |
| 24 | # Edge Case: Empty files list |
| 25 | if not files: |
| 26 | return '' |
| 27 | |
| 28 | results = [] |
| 29 | matching_files = set() |
| 30 | |
| 31 | # Determine search pattern based on case sensitivity |
| 32 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 33 | |
| 34 | # Edge Case: File not found |
| 35 | for file_path in files: |
| 36 | try: |
| 37 | with open(file_path, 'r', encoding='utf-8') as file: |
| 38 | lines = file.readlines() |
| 39 | except FileNotFoundError: |
| 40 | # Skip files that don't exist |
| 41 | continue |
| 42 | |
| 43 | file_has_match = False |
| 44 | |
| 45 | # Edge Case: Empty file |
| 46 | if not lines: |
| 47 | # Process empty file - if pattern is empty, it matches |
| 48 | if pattern == '': |
| 49 | file_has_match = True |
| 50 | if files_only: |
| 51 | matching_files.add(file_path) |
| 52 | elif not invert_match: # Only add if not inverting |
| 53 | if len(files) > 1: |
| 54 | results.append(f"{file_path}:") |
| 55 | else: |
| 56 | results.append("") |
| 57 | continue |
| 58 | |
| 59 | for line_num, line in enumerate(lines, 1): |
| 60 | # Remove newline for processing but keep for output |
| 61 | line_content = line.rstrip('\n') |
| 62 | |
| 63 | # Determine line to compare based on case sensitivity |
| 64 | compare_line = line_content.lower() if case_insensitive else line_content |
| 65 | |
| 66 | # Check for match based on exact match flag |
| 67 | if exact_match: |
| 68 | is_match = (compare_line == search_pattern) |
| 69 | else: |
| 70 | is_match = (search_pattern in compare_line) |
| 71 | |
| 72 | # Apply invert flag |
| 73 | if invert_match: |
| 74 | is_match = not is_match |
| 75 | |
| 76 | if is_match: |
| 77 | file_has_match = True |
| 78 | |
| 79 | # Edge Case: Files only mode |
| 80 | if files_only: |
| 81 | matching_files.add(file_path) |
| 82 | # No need to process more lines for this file |
| 83 | break |
| 84 | |
| 85 | # Build output line |
| 86 | output_line = "" |
| 87 | |
| 88 | # Add filename if multiple files |
| 89 | if len(files) > 1: |
| 90 | output_line += f"{file_path}:" |
| 91 | |
| 92 | # Add line number if requested |
| 93 | if line_numbers: |
| 94 | output_line += f"{line_num}:" |
| 95 | |
| 96 | # Add content |
| 97 | output_line += line_content |
| 98 | |
| 99 | results.append(output_line) |
| 100 | |
| 101 | # Edge Case: Files only mode - need to add file if it had matches |
| 102 | if files_only and file_has_match: |
| 103 | matching_files.add(file_path) |
| 104 | |
| 105 | # Edge Case: Files only mode - return file names |
| 106 | if files_only: |
| 107 | # Return files in the order they were specified |
| 108 | ordered_results = [f for f in files if f in matching_files] |
| 109 | return "\n".join(ordered_results) |
| 110 | |
| 111 | return "\n".join(results) |
| 112 | |
| 113 | # Handled Edge Cases: Empty pattern, empty files list, file not found, empty file, files only mode |