| 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 customize the search behavior. |
| 8 | files (list[str]): List of file paths to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: All matching lines, formatted according to the flags. |
| 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 | filenames_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 | # Edge Case: Empty pattern |
| 29 | if not pattern: |
| 30 | pattern = '' |
| 31 | |
| 32 | # Edge Case: No files provided |
| 33 | if not files: |
| 34 | return '' |
| 35 | |
| 36 | # Prepare pattern for comparison |
| 37 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 38 | |
| 39 | results = [] |
| 40 | matched_files = set() |
| 41 | |
| 42 | multiple_files = len(files) > 1 |
| 43 | |
| 44 | for file_path in files: |
| 45 | try: |
| 46 | with open(file_path, 'r', encoding='utf-8') as file: |
| 47 | lines = file.readlines() |
| 48 | |
| 49 | # Edge Case: File cannot be read |
| 50 | except IOError: |
| 51 | continue |
| 52 | |
| 53 | file_has_match = False |
| 54 | |
| 55 | for line_num, line in enumerate(lines, 1): |
| 56 | # Remove newline character for comparison but keep it for output |
| 57 | line_content = line.rstrip('\n') |
| 58 | |
| 59 | # Prepare line for comparison |
| 60 | compare_line = line_content.lower() if case_insensitive else line_content |
| 61 | |
| 62 | # Determine if line matches |
| 63 | if exact_match: |
| 64 | is_match = (compare_line == search_pattern) |
| 65 | else: |
| 66 | is_match = (search_pattern in compare_line) |
| 67 | |
| 68 | # Apply invert flag |
| 69 | if invert_match: |
| 70 | is_match = not is_match |
| 71 | |
| 72 | if is_match: |
| 73 | file_has_match = True |
| 74 | |
| 75 | # Edge Case: Only filenames requested |
| 76 | if filenames_only: |
| 77 | matched_files.add(file_path) |
| 78 | # No need to process more lines for this file |
| 79 | break |
| 80 | else: |
| 81 | output_line = '' |
| 82 | |
| 83 | # Add filename prefix if multiple files |
| 84 | if multiple_files: |
| 85 | output_line += f"{file_path}:" |
| 86 | |
| 87 | # Add line number if requested |
| 88 | if line_numbers: |
| 89 | output_line += f"{line_num}:" |
| 90 | |
| 91 | # Add the line content |
| 92 | output_line += line |
| 93 | |
| 94 | results.append(output_line) |
| 95 | |
| 96 | # Edge Case: File has no matches but -l flag is used |
| 97 | if filenames_only and file_has_match: |
| 98 | # Already added to matched_files above |
| 99 | pass |
| 100 | |
| 101 | # Edge Case: No matches found |
| 102 | if filenames_only: |
| 103 | return '\n'.join(sorted(matched_files)) + ('\n' if matched_files else '') |
| 104 | |
| 105 | # Edge Case: No results to return |
| 106 | if not results: |
| 107 | return '' |
| 108 | |
| 109 | return ''.join(results) |
| 110 | |
| 111 | # Handled Edge Cases: Empty pattern, no files provided, file cannot be read, only filenames requested, file has no matches but -l flag is used, no matches found, no results to return |