| 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): Flags for customizing the command's behavior. |
| 8 | files (list[str]): One or more files to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines, possibly with file names and line numbers. |
| 12 | |
| 13 | The function supports the following 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 separate and combined flags |
| 21 | line_numbers = '-n' in flags or 'n' in flags.replace('-', '') |
| 22 | files_only = '-l' in flags or 'l' in flags.replace('-', '') |
| 23 | case_insensitive = '-i' in flags or 'i' in flags.replace('-', '') |
| 24 | invert_match = '-v' in flags or 'v' in flags.replace('-', '') |
| 25 | exact_match = '-x' in flags or 'x' in flags.replace('-', '') |
| 26 | |
| 27 | # Edge Case: Empty pattern |
| 28 | if not pattern: |
| 29 | pattern = '' |
| 30 | |
| 31 | # Edge Case: Empty files list |
| 32 | if not files: |
| 33 | return '' |
| 34 | |
| 35 | # Store original file count for filename prefixing logic |
| 36 | original_file_count = len(files) |
| 37 | |
| 38 | # Prepare pattern for comparison |
| 39 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 40 | |
| 41 | # Results |
| 42 | result_lines = [] |
| 43 | matching_files = set() |
| 44 | |
| 45 | # Edge Case: File not found |
| 46 | for file_name in files: |
| 47 | try: |
| 48 | with open(file_name, 'r', encoding='utf-8') as f: |
| 49 | lines = f.readlines() |
| 50 | except FileNotFoundError: |
| 51 | # Skip files that are not found |
| 52 | continue |
| 53 | |
| 54 | # Edge Case: File is empty or unreadable |
| 55 | if not lines: |
| 56 | # If file is empty and we're looking for empty pattern with exact match |
| 57 | if exact_match and pattern == '' and not invert_match: |
| 58 | if files_only: |
| 59 | matching_files.add(file_name) |
| 60 | elif original_file_count > 1: |
| 61 | result_lines.append(f"{file_name}:") |
| 62 | else: |
| 63 | result_lines.append("") |
| 64 | continue |
| 65 | |
| 66 | file_has_match = False |
| 67 | |
| 68 | for line_num, line in enumerate(lines, 1): |
| 69 | # Remove newline character for comparison but keep it for output |
| 70 | line_content = line.rstrip('\n') |
| 71 | |
| 72 | # Prepare line for comparison |
| 73 | compare_line = line_content.lower() if case_insensitive else line_content |
| 74 | |
| 75 | # Determine if line matches |
| 76 | if exact_match: |
| 77 | is_match = (compare_line == search_pattern) |
| 78 | else: |
| 79 | is_match = (search_pattern in compare_line) |
| 80 | |
| 81 | # Apply invert flag |
| 82 | if invert_match: |
| 83 | is_match = not is_match |
| 84 | |
| 85 | # Edge Case: Matching line is found |
| 86 | if is_match: |
| 87 | file_has_match = True |
| 88 | |
| 89 | # If we only need file names, we can break early |
| 90 | if files_only: |
| 91 | matching_files.add(file_name) |
| 92 | break |
| 93 | |
| 94 | # Build output line |
| 95 | output_line = "" |
| 96 | |
| 97 | # Add file name if multiple files |
| 98 | if original_file_count > 1: |
| 99 | output_line += f"{file_name}:" |
| 100 | |
| 101 | # Add line number if requested |
| 102 | if line_numbers: |
| 103 | output_line += f"{line_num}:" |
| 104 | |
| 105 | # Add content |
| 106 | output_line += line_content |
| 107 | |
| 108 | result_lines.append(output_line) |
| 109 | |
| 110 | # Edge Case: File has at least one match and we're in files_only mode |
| 111 | if file_has_match and files_only: |
| 112 | # We already added it to matching_files above |
| 113 | pass |
| 114 | |
| 115 | # Edge Case: No matches found |
| 116 | if files_only: |
| 117 | return '\n'.join(sorted(list(matching_files))) |
| 118 | |
| 119 | # Edge Case: Multiple matches |
| 120 | return '\n'.join(result_lines) |
| 121 | |
| 122 | # Handled Edge Cases: Empty pattern, empty files list, file not found, file is empty or unreadable, matching line is found, file has at least one match and we're in files_only mode, no matches found, multiple matches |