| 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 search 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 prepended |
| 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 | files_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 | # Prepare pattern for case-insensitive matching |
| 29 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 30 | |
| 31 | results = [] |
| 32 | matching_files = set() |
| 33 | |
| 34 | # Edge Case: No files provided |
| 35 | if not files: |
| 36 | return "" |
| 37 | |
| 38 | multiple_files = len(files) > 1 |
| 39 | |
| 40 | for file_path in files: |
| 41 | try: |
| 42 | with open(file_path, 'r', encoding='utf-8') as file: |
| 43 | # Edge Case: File cannot be read (handled by try/except) |
| 44 | lines = file.readlines() |
| 45 | except (FileNotFoundError, IOError): |
| 46 | # Edge Case: File not found or cannot be opened |
| 47 | continue |
| 48 | |
| 49 | file_has_match = False |
| 50 | |
| 51 | for line_num, line in enumerate(lines, 1): |
| 52 | # Prepare line for comparison |
| 53 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 54 | |
| 55 | # Determine if line matches |
| 56 | if exact_match: |
| 57 | is_match = (search_pattern == compare_line) |
| 58 | else: |
| 59 | is_match = (search_pattern in compare_line) |
| 60 | |
| 61 | # Apply invert flag |
| 62 | if invert_match: |
| 63 | is_match = not is_match |
| 64 | |
| 65 | if is_match: |
| 66 | file_has_match = True |
| 67 | |
| 68 | # Edge Case: Only file names requested |
| 69 | if files_only: |
| 70 | matching_files.add(file_path) |
| 71 | break # No need to check more lines in this file |
| 72 | |
| 73 | # Build output line |
| 74 | output_parts = [] |
| 75 | |
| 76 | # Add file name if multiple files |
| 77 | if multiple_files: |
| 78 | output_parts.append(file_path) |
| 79 | |
| 80 | # Add line number if requested |
| 81 | if line_numbers: |
| 82 | # If multiple files, we need the colon after filename |
| 83 | if multiple_files: |
| 84 | output_parts[-1] += ':' + str(line_num) |
| 85 | else: |
| 86 | output_parts.append(str(line_num)) |
| 87 | |
| 88 | # Add the actual line content |
| 89 | output_parts.append(line.rstrip('\n')) |
| 90 | |
| 91 | results.append(':'.join(output_parts) if multiple_files or line_numbers else output_parts[-1]) |
| 92 | |
| 93 | # Edge Case: Files-only mode and this file had matches |
| 94 | if files_only and file_has_match: |
| 95 | matching_files.add(file_path) |
| 96 | |
| 97 | # Edge Case: Files-only mode - return just the file names |
| 98 | if files_only: |
| 99 | return '\n'.join(sorted(list(matching_files))) |
| 100 | |
| 101 | return '\n'.join(results) |
| 102 | |
| 103 | # Handled Edge Cases: No files provided, File not found or cannot be opened, Only file names requested, Files-only mode and this file had matches, Files-only mode - return just the file names |