| 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 according to the specified flags. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only filenames with matches. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert match (return non-matching lines). |
| 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 | # Prepare pattern for comparison |
| 29 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 30 | |
| 31 | # Results storage |
| 32 | results = [] |
| 33 | matching_files = set() |
| 34 | |
| 35 | # Process each file |
| 36 | for filename in files: |
| 37 | try: |
| 38 | with open(filename, 'r', encoding='utf-8') as file: |
| 39 | # Edge Case: Handle files that cannot be opened |
| 40 | lines = file.readlines() |
| 41 | except FileNotFoundError: |
| 42 | # Edge Case: File not found - skip the file |
| 43 | continue |
| 44 | except IOError: |
| 45 | # Edge Case: Other I/O errors - skip the file |
| 46 | continue |
| 47 | |
| 48 | # Process each line in the file |
| 49 | for line_num, line in enumerate(lines, start=1): |
| 50 | # Prepare line for comparison |
| 51 | compare_line = line.lower().rstrip('\n') if case_insensitive else line.rstrip('\n') |
| 52 | |
| 53 | # Determine if line matches |
| 54 | if exact_match: |
| 55 | # Edge Case: Exact match mode - entire line must match pattern |
| 56 | is_match = (compare_line == search_pattern) |
| 57 | else: |
| 58 | # Edge Case: Substring match mode - pattern can be anywhere in line |
| 59 | is_match = (search_pattern in compare_line) |
| 60 | |
| 61 | # Apply invert flag |
| 62 | if invert_match: |
| 63 | # Edge Case: Invert match - matching lines become non-matching and vice versa |
| 64 | is_match = not is_match |
| 65 | |
| 66 | # Collect results based on match |
| 67 | if is_match: |
| 68 | # Edge Case: Track matching files for -l flag |
| 69 | matching_files.add(filename) |
| 70 | |
| 71 | # If not in filenames_only mode, collect the line |
| 72 | if not filenames_only: |
| 73 | output_line = line # Preserve original line with newline |
| 74 | |
| 75 | # Build prefix |
| 76 | prefix_parts = [] |
| 77 | |
| 78 | # Edge Case: Multiple files - prepend filename |
| 79 | if len(files) > 1: |
| 80 | prefix_parts.append(filename) |
| 81 | |
| 82 | # Edge Case: Line numbers requested - add line number |
| 83 | if line_numbers: |
| 84 | prefix_parts.append(str(line_num)) |
| 85 | |
| 86 | # Edge Case: Build prefix with colons |
| 87 | prefix = ':'.join(prefix_parts) |
| 88 | |
| 89 | # Edge Case: Add colon between prefix and line if prefix exists |
| 90 | if prefix: |
| 91 | output_line = f"{prefix}:{output_line}" |
| 92 | |
| 93 | results.append(output_line) |
| 94 | |
| 95 | # Edge Case: Filenames only mode - return unique matching filenames |
| 96 | if filenames_only: |
| 97 | # Preserve order of files as provided |
| 98 | ordered_matching_files = [f for f in files if f in matching_files] |
| 99 | return '\n'.join(ordered_matching_files) + ('\n' if ordered_matching_files else '') |
| 100 | |
| 101 | # Edge Case: No matches found - return empty string |
| 102 | if not results: |
| 103 | return '' |
| 104 | |
| 105 | # Edge Case: Remove trailing newline if present |
| 106 | output = ''.join(results) |
| 107 | return output.rstrip('\n') + '\n' |
| 108 | |
| 109 | # Handled Edge Cases: Empty pattern, empty files list, file not found, I/O errors, case sensitivity, exact match, invert match, line numbers, filenames only, multiple files, no matches found, trailing newlines |