fistinz

Finished
11def grep(pattern: str, flags: str, files: list[str]) -> str:
2 pass
2 """
3 Search for a pattern in files and return matching lines.
4
5 Args:
6 pattern: The string to search for
7 flags: String containing command line flags (-n, -l, -i, -v, -x)
8 files: List of file paths to search in
9
10 Returns:
11 String containing matching lines, formatted according to flags
12
13 Flags:
14 -n: Prepend line numbers
15 -l: Output only filenames with matches
16 -i: Case insensitive matching
17 -v: Invert matching (return non-matching lines)
18 -x: Match entire line only
19 """
20 # Parse flags
21 line_numbers = '-n' in flags
22 files_only = '-l' in flags
23 case_insensitive = '-i' in flags
24 invert_match = '-v' in flags
25 exact_match = '-x' in flags
26
27 # Prepare pattern for case-insensitive matching
28 search_pattern = pattern.lower() if case_insensitive else pattern
29
30 # Results storage
31 results = []
32 matched_files = set()
33
34 # Process each file
35 for file_path in files:
36 try:
37 with open(file_path, 'r', encoding='utf-8') as file:
38 lines = file.readlines()
39
40 # Process each line
41 for line_num, line in enumerate(lines, 1):
42 # Prepare line for comparison
43 compare_line = line.lower() if case_insensitive else line
44
45 # Determine if line matches
46 if exact_match:
47 matches = (compare_line.rstrip('\n') == search_pattern)
48 else:
49 matches = (search_pattern in compare_line)
50
51 # Apply invert flag
52 if invert_match:
53 matches = not matches
54
55 # Edge Case: Handle empty pattern
56 if pattern == '':
57 matches = not invert_match # Empty pattern matches all lines unless inverted
58
59 if matches:
60 matched_files.add(file_path)
61
62 # If we only need file names, we can break early
63 if files_only:
64 break
65
66 # Build output line
67 output_line = ''
68
69 # Add filename prefix if multiple files
70 if len(files) > 1:
71 output_line += f'{file_path}:'
72
73 # Add line number if requested
74 if line_numbers:
75 output_line += f'{line_num}:'
76
77 # Add the actual line
78 output_line += line
79
80 results.append(output_line)
81
82 # Edge Case: Handle file not found
83 except FileNotFoundError:
84 # According to typical grep behavior, we silently ignore missing files
85 # or we could raise an error - following typical behavior here
86 pass
87
88 # Edge Case: Handle permission errors
89 except PermissionError:
90 # Silently ignore files we can't read
91 pass
92
93 # Edge Case: Handle -l flag (files only)
94 if files_only:
95 # Return only the names of files with matches, in the order they were found
96 output_files = [f for f in files if f in matched_files]
97 return '\n'.join(output_files) + ('\n' if output_files else '')
98
99 # Edge Case: Handle no matches
100 if not results:
101 return ''
102
103 # Edge Case: Handle trailing newlines
104 # Join all results, ensuring there's a newline at the end if there were any results
105 output = ''.join(results)
106 return output if output.endswith('\n') else output + '\n' if output else ''
107
108# Handled Edge Cases: Empty pattern, file not found, permission errors, -l flag, no matches, trailing newlines
Test NameStatus
test_multiple_files_no_matches_various_flags
Pass
test_multiple_files_one_match_match_entire_lines_flag
Pass
test_multiple_files_one_match_multiple_flags
Pass
test_multiple_files_one_match_no_flags
Pass
test_multiple_files_one_match_print_file_names_flag
Pass
test_multiple_files_several_matches_case_insensitive_flag
Pass
test_multiple_files_several_matches_file_flag_takes_precedence_over_line_number_flag
Pass
test_multiple_files_several_matches_inverted_and_match_entire_lines_flags
Pass
test_multiple_files_several_matches_inverted_flag
Pass
test_multiple_files_several_matches_no_flags
Pass
test_multiple_files_several_matches_print_line_numbers_flag
Pass
test_one_file_no_matches_various_flags
Pass
test_one_file_one_match_case_insensitive_flag
Pass
test_one_file_one_match_file_flag_takes_precedence_over_line_flag
Pass
test_one_file_one_match_match_entire_lines_flag
Pass
test_one_file_one_match_multiple_flags
Pass
test_one_file_one_match_no_flags
Pass
test_one_file_one_match_print_file_names_flag
Pass
test_one_file_one_match_print_line_numbers_flag
Pass
test_one_file_several_matches_case_insensitive_flag
Pass
test_one_file_several_matches_inverted_and_match_entire_lines_flags
Pass
test_one_file_several_matches_inverted_flag
Pass
test_one_file_several_matches_match_entire_lines_flag
Pass
test_one_file_several_matches_no_flags
Pass
test_one_file_several_matches_print_line_numbers_flag
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.