| 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 according to the specified flags. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only file names with matches. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert match (lines that don't match). |
| 18 | -x: Match entire line only. |
| 19 | """ |
| 20 | # Parse flags - handle both space-separated and combined flags |
| 21 | flag_list = flags.split() if flags else [] |
| 22 | # Also check for combined flags like "-in" |
| 23 | combined_flags = "".join(flag_list) |
| 24 | line_numbers = '-n' in flag_list or 'n' in combined_flags |
| 25 | files_only = '-l' in flag_list or 'l' in combined_flags |
| 26 | case_insensitive = '-i' in flag_list or 'i' in combined_flags |
| 27 | invert_match = '-v' in flag_list or 'v' in combined_flags |
| 28 | exact_match = '-x' in flag_list or 'x' in combined_flags |
| 29 | |
| 30 | # Prepare pattern for comparison |
| 31 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 32 | |
| 33 | # Initialize results |
| 34 | results = [] |
| 35 | matching_files = set() |
| 36 | |
| 37 | # Process each file |
| 38 | for file_path in files: |
| 39 | try: |
| 40 | with open(file_path, 'r', encoding='utf-8') as file: |
| 41 | # Edge Case: Handle files that cannot be opened |
| 42 | lines = file.readlines() |
| 43 | except FileNotFoundError: |
| 44 | # Edge Case: Handle missing files |
| 45 | continue |
| 46 | except Exception: |
| 47 | # Edge Case: Handle other file reading errors |
| 48 | continue |
| 49 | |
| 50 | # Process each line in the file |
| 51 | for line_num, line in enumerate(lines, start=1): |
| 52 | # Prepare line for comparison |
| 53 | compare_line = line.lower() if case_insensitive else line |
| 54 | |
| 55 | # Determine if line matches |
| 56 | if exact_match: |
| 57 | # Edge Case: Exact match requires the entire line to match the pattern |
| 58 | matches = search_pattern == compare_line.rstrip('\n') |
| 59 | else: |
| 60 | # Edge Case: Partial match checks if pattern is contained in the line |
| 61 | matches = search_pattern in compare_line |
| 62 | |
| 63 | # Apply invert flag |
| 64 | if invert_match: |
| 65 | # Edge Case: Invert match inverts the boolean result |
| 66 | matches = not matches |
| 67 | |
| 68 | # If line matches, collect results |
| 69 | if matches: |
| 70 | if files_only: |
| 71 | # Edge Case: When -l flag is used, only track matching files |
| 72 | matching_files.add(file_path) |
| 73 | break # No need to check more lines in this file |
| 74 | else: |
| 75 | # Build output line |
| 76 | output_line = "" |
| 77 | |
| 78 | # Add filename prefix if multiple files |
| 79 | if len(files) > 1: |
| 80 | # Edge Case: Filename prefix is added when searching multiple files |
| 81 | output_line += f"{file_path}:" |
| 82 | |
| 83 | # Add line number if requested |
| 84 | if line_numbers: |
| 85 | # Edge Case: Line numbers are added after filename but before content |
| 86 | output_line += f"{line_num}:" |
| 87 | |
| 88 | # Add the actual line content |
| 89 | output_line += line |
| 90 | |
| 91 | # Ensure trailing newline if not present |
| 92 | if not output_line.endswith('\n'): |
| 93 | output_line += '\n' |
| 94 | |
| 95 | results.append(output_line) |
| 96 | |
| 97 | # Return appropriate result based on flags |
| 98 | if files_only: |
| 99 | # Edge Case: When -l flag is used, return only file names, sorted |
| 100 | return "\n".join(sorted(matching_files)) + ("\n" if matching_files else "") |
| 101 | else: |
| 102 | # Edge Case: When not using -l flag, return all matching lines |
| 103 | return "".join(results) |
| 104 | |
| 105 | # Handled Edge Cases: Empty pattern, missing files, file reading errors, exact match, partial match, case sensitivity, invert match, line numbering, multiple files, files with matches only (-l flag) |