| 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): Flags for customizing the command's behavior. |
| 8 | files (list[str]): One or more files to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines, possibly with file names and line numbers. |
| 12 | |
| 13 | The function supports the following 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 | # Handle both space-separated flags and concatenated flags like "-inv" or "inv" |
| 22 | flag_list = [] |
| 23 | if flags: |
| 24 | # Remove leading dashes and split into individual flags |
| 25 | cleaned_flags = flags.replace('-', '') |
| 26 | flag_list = list(cleaned_flags) if cleaned_flags else [] |
| 27 | |
| 28 | line_numbers = 'n' in flag_list |
| 29 | filenames_only = 'l' in flag_list |
| 30 | case_insensitive = 'i' in flag_list |
| 31 | invert_match = 'v' in flag_list |
| 32 | exact_match = 'x' in flag_list |
| 33 | |
| 34 | # Prepare pattern for comparison |
| 35 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 36 | |
| 37 | # Initialize results |
| 38 | results = [] |
| 39 | matching_files = set() |
| 40 | |
| 41 | # Process each file |
| 42 | for filename in files: |
| 43 | try: |
| 44 | with open(filename, 'r', encoding='utf-8') as file: |
| 45 | lines = file.readlines() |
| 46 | |
| 47 | # Process each line |
| 48 | for line_num, line in enumerate(lines, start=1): |
| 49 | # Prepare line for comparison |
| 50 | compare_line = line.lower() if case_insensitive else line |
| 51 | |
| 52 | # Determine if line matches |
| 53 | if exact_match: |
| 54 | # Edge Case: Exact match requires the entire line to match the pattern |
| 55 | matches = (compare_line.rstrip('\n') == search_pattern) |
| 56 | else: |
| 57 | # Edge Case: Partial match checks if pattern is contained in the line |
| 58 | matches = (search_pattern in compare_line) |
| 59 | |
| 60 | # Apply invert flag |
| 61 | if invert_match: |
| 62 | # Edge Case: Invert match inverts the matching result |
| 63 | matches = not matches |
| 64 | |
| 65 | # If line matches, collect results |
| 66 | if matches: |
| 67 | if filenames_only: |
| 68 | # Edge Case: When -l flag is used, we only need to know which files match |
| 69 | matching_files.add(filename) |
| 70 | break # No need to check more lines in this file |
| 71 | else: |
| 72 | # Build output line |
| 73 | output_line = "" |
| 74 | |
| 75 | # Add filename prefix if multiple files |
| 76 | # Edge Case: Filename prefix is added only when searching multiple files |
| 77 | if len(files) > 1: |
| 78 | output_line += f"{filename}:" |
| 79 | |
| 80 | # Add line number if requested |
| 81 | # Edge Case: Line number is added after filename but before content |
| 82 | if line_numbers: |
| 83 | output_line += f"{line_num}:" |
| 84 | |
| 85 | # Add line content (without trailing newline) |
| 86 | output_line += line.rstrip('\n') |
| 87 | |
| 88 | results.append(output_line) |
| 89 | except FileNotFoundError: |
| 90 | # Edge Case: Handle file not found errors gracefully |
| 91 | # For this implementation, we'll skip missing files as per typical grep behavior |
| 92 | continue |
| 93 | except Exception: |
| 94 | # Edge Case: Handle other file reading errors |
| 95 | # We'll skip files with other errors to maintain robustness |
| 96 | continue |
| 97 | |
| 98 | # Return appropriate result based on flags |
| 99 | if filenames_only: |
| 100 | # Edge Case: When -l flag is used, return only filenames, sorted |
| 101 | if matching_files: |
| 102 | return "\n".join(sorted(matching_files)) |
| 103 | else: |
| 104 | return "" |
| 105 | else: |
| 106 | # Edge Case: When no matches are found, return empty string |
| 107 | if results: |
| 108 | return "\n".join(results) |
| 109 | else: |
| 110 | return "" |
| 111 | |
| 112 | # Handled Edge Cases: exact match, partial match, invert match, filename prefix for multiple files, line number formatting, file not found errors, other file reading errors, -l flag behavior, no matches found |