| 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 for customizing 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. |
| 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 comparison |
| 29 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 30 | |
| 31 | # Initialize results |
| 32 | results = [] |
| 33 | matching_files = set() |
| 34 | |
| 35 | # Edge Case: No files provided |
| 36 | if not files: |
| 37 | return "" |
| 38 | |
| 39 | multiple_files = len(files) > 1 |
| 40 | |
| 41 | # Process each file |
| 42 | for file_path in files: |
| 43 | try: |
| 44 | with open(file_path, 'r', encoding='utf-8') as file: |
| 45 | lines = file.readlines() |
| 46 | |
| 47 | # Edge Case: File is empty |
| 48 | if not lines: |
| 49 | continue |
| 50 | |
| 51 | # Process each line |
| 52 | for i, line in enumerate(lines, start=1): |
| 53 | # Prepare line for comparison |
| 54 | compare_line = line.rstrip('\n').lower() if case_insensitive else line.rstrip('\n') |
| 55 | |
| 56 | # Determine if line matches |
| 57 | if exact_match: |
| 58 | match = (compare_line == search_pattern) |
| 59 | else: |
| 60 | match = (search_pattern in compare_line) |
| 61 | |
| 62 | # Apply invert flag |
| 63 | if invert_match: |
| 64 | match = not match |
| 65 | |
| 66 | # If line matches |
| 67 | if match: |
| 68 | if files_only: |
| 69 | matching_files.add(file_path) |
| 70 | # No need to check more lines in this file |
| 71 | break |
| 72 | else: |
| 73 | # Build output line |
| 74 | output_parts = [] |
| 75 | |
| 76 | # Add file name if multiple files |
| 77 | if multiple_files: |
| 78 | # Edge Case: File name contains special characters that need escaping |
| 79 | output_parts.append(file_path) |
| 80 | |
| 81 | # Add line number if requested |
| 82 | if line_numbers: |
| 83 | output_parts.append(str(i)) |
| 84 | |
| 85 | # Add the actual line content |
| 86 | output_parts.append(line.rstrip('\n')) |
| 87 | |
| 88 | # Join parts appropriately |
| 89 | if multiple_files and line_numbers: |
| 90 | results.append(f"{output_parts[0]}:{output_parts[1]}:{output_parts[2]}") |
| 91 | elif multiple_files: |
| 92 | results.append(f"{output_parts[0]}:{output_parts[1]}") |
| 93 | elif line_numbers: |
| 94 | results.append(f"{output_parts[0]}:{output_parts[1]}") |
| 95 | else: |
| 96 | results.append(output_parts[0]) |
| 97 | |
| 98 | # Edge Case: File not found or permission denied |
| 99 | except (FileNotFoundError, PermissionError): |
| 100 | # According to typical grep behavior, we silently ignore inaccessible files |
| 101 | # unless we're specifically asked to report errors, which we're not in this exercise |
| 102 | continue |
| 103 | |
| 104 | # If -l flag is set, return only file names |
| 105 | if files_only: |
| 106 | # Edge Case: No files matched the pattern |
| 107 | return '\n'.join(sorted(matching_files)) |
| 108 | |
| 109 | # Edge Case: No lines matched the pattern |
| 110 | return '\n'.join(results) |
| 111 | |
| 112 | # Handled Edge Cases: No files provided, File is empty, File not found or permission denied, No files matched the pattern, No lines matched the pattern |