armor

Finished
11def grep(pattern: str, flags: str, files: list[str]) -> str:
2 pass
2 """
3 Search for lines matching 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 all matching lines, formatted according to flags
12 """
13 # Parse flags
14 line_numbers = '-n' in flags
15 files_only = '-l' in flags
16 case_insensitive = '-i' in flags
17 invert_match = '-v' in flags
18 exact_match = '-x' in flags
19
20 results = []
21
22 # Edge Case: No files provided
23 if not files:
24 return ""
25
26 multiple_files = len(files) > 1
27
28 for file_path in files:
29 # Edge Case: File not found
30 try:
31 with open(file_path, 'r', encoding='utf-8') as file:
32 lines = file.readlines()
33 except FileNotFoundError:
34 # According to typical grep behavior, we silently skip missing files
35 continue
36
37 # Edge Case: File with no lines
38 if not lines:
39 continue
40
41 file_has_match = False
42
43 for line_num, line in enumerate(lines, 1):
44 # Remove newline for processing but keep it for output
45 line_content = line.rstrip('\n')
46
47 # Determine if line matches
48 if case_insensitive:
49 match = pattern.lower() in line_content.lower()
50 exact = pattern.lower() == line_content.lower()
51 else:
52 match = pattern in line_content
53 exact = pattern == line_content
54
55 # Apply exact match flag
56 if exact_match:
57 match = exact
58
59 # Apply invert flag
60 if invert_match:
61 match = not match
62
63 if match:
64 file_has_match = True
65
66 # If we only need file names, we can break early
67 if files_only:
68 break
69
70 # Build output line
71 output_line = ""
72
73 # Add file name if multiple files
74 if multiple_files:
75 output_line += f"{file_path}:"
76
77 # Add line number if requested
78 if line_numbers:
79 output_line += f"{line_num}:"
80
81 # Add content
82 output_line += line
83
84 results.append(output_line)
85
86 # If we only need file names and this file had a match
87 if files_only and file_has_match:
88 results.append(file_path + "\n")
89
90 # Edge Case: No matches found
91 # This is handled naturally by returning an empty string
92
93 # Join all results
94 return ''.join(results)
95
96# Handled Edge Cases: No files provided, File not found, File with no lines, No matches found
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.