@@ -113,6 +113,7 @@ struct Mutex
113113struct Parameters
114114{
115115 std::vector<Named_regex> patterns_;
116+ std::vector<Named_regex> module_patterns_;
116117 String_view_set names_;
117118 bool also_remove_annotations_ = false ;
118119 bool in_place_ = false ;
@@ -123,6 +124,7 @@ struct Strict_mode
123124{
124125 std::atomic<bool > any_annotation_removed_ = false ;
125126 Mutex<std::map<std::string_view, bool , std::less<>>> patterns_matched_;
127+ Mutex<std::map<std::string_view, bool , std::less<>>> module_patterns_matched_;
126128 Mutex<std::map<std::string_view, bool , std::less<>>> names_matched_;
127129 Mutex<std::map<std::string_view, bool >> files_truncated_;
128130};
@@ -587,24 +589,136 @@ inline std::string remove_annotations(std::string_view content, std::span<const
587589 return result;
588590}
589591
592+ inline std::string remove_jpms_requires (std::string_view content, std::span<const Named_regex> module_patterns)
593+ {
594+ auto new_content = std::string (content);
595+ new_content.reserve (content.size ());
596+
597+ auto pos = std::ptrdiff_t (0 );
598+ pos = find_token (content, " module" );
599+ pos = find_token (content, " {" , pos);
600+
601+ if (pos != std::ssize (content))
602+ {
603+ ++pos;
604+ new_content.clear ();
605+ new_content.append (content, 0 , pos);
606+ while (pos != std::ssize (content))
607+ {
608+ auto symbol = std::string_view ();
609+ auto end_pos = std::ptrdiff_t (0 );
610+ auto old_pos = pos;
611+ std::tie (symbol, end_pos) = next_symbol (content, pos);
612+
613+ if (symbol == " requires" )
614+ {
615+ pos = end_pos;
616+ }
617+ else
618+ {
619+ pos = find_token (content, " ;" , pos);
620+ if (pos != std::ssize (content))
621+ {
622+ ++pos;
623+ }
624+ new_content.append (content, old_pos, pos - old_pos);
625+ continue ;
626+ }
627+
628+ std::tie (symbol, end_pos) = next_symbol (content, pos);
629+ if (symbol == " transitive" )
630+ {
631+ pos = end_pos;
632+ }
633+ else if (symbol == " static" )
634+ {
635+ pos = end_pos;
636+ std::tie (symbol, end_pos) = next_symbol (content, pos);
637+ if (symbol == " transitive" )
638+ {
639+ pos = end_pos;
640+ }
641+ }
642+
643+ auto module_name = std::string ();
644+
645+ std::tie (symbol, end_pos) = next_symbol (content, pos);
646+ while (symbol != " ;" )
647+ {
648+ if (symbol.empty ())
649+ {
650+ new_content.clear ();
651+ new_content.append (content);
652+ return new_content;
653+ }
654+
655+ module_name += symbol;
656+ std::tie (symbol, end_pos) = next_symbol (content, end_pos);
657+ }
658+
659+ pos = end_pos;
660+
661+ bool matched = false ;
662+ for (const auto & pattern : module_patterns)
663+ {
664+ if (std::regex_search (module_name.begin (), module_name.end (), pattern))
665+ {
666+ if (strict_mode)
667+ {
668+ strict_mode->module_patterns_matched_ .lock ().get ().at (pattern) = true ;
669+ }
670+
671+ matched = true ;
672+ break ;
673+ }
674+ }
675+
676+ if (matched)
677+ {
678+ if (auto skip_space = find_newline (content, pos); skip_space != -1 )
679+ {
680+ pos = skip_space;
681+ }
682+ }
683+ else
684+ {
685+ new_content.append (content, old_pos, pos - old_pos);
686+ }
687+ }
688+ }
689+
690+ return new_content;
691+ }
692+
590693// //////////////////////////////////////////////////////////////////////////////
591694
592- inline std::string handle_content (std::string_view content, const Parameters& parameters)
695+ inline std::string handle_content (const Path_origin_entry& path, std::string_view content, const Parameters& parameters)
593696{
594- auto [new_content, removed_classes] = remove_imports (content, parameters. patterns_ , parameters. names_ );
697+ auto result = std::string ( );
595698
596- if (parameters.also_remove_annotations_ )
699+ if (path.filename () == " module-info.java" )
700+ {
701+ result = remove_jpms_requires (content, parameters.module_patterns_ );
702+ }
703+ else
597704 {
598- auto content_size = new_content.size ();
599- new_content = remove_annotations (new_content, parameters.patterns_ , parameters.names_ , removed_classes);
705+ auto [new_content, removed_classes] = remove_imports (content, parameters.patterns_ , parameters.names_ );
600706
601- if (strict_mode and new_content. size () < content_size )
707+ if (parameters. also_remove_annotations_ )
602708 {
603- strict_mode->any_annotation_removed_ .store (true , std::memory_order_release);
709+ auto content_size = new_content.size ();
710+ new_content = remove_annotations (new_content, parameters.patterns_ , parameters.names_ , removed_classes);
711+
712+ if (strict_mode and new_content.size () < content_size)
713+ {
714+ strict_mode->any_annotation_removed_ .store (true , std::memory_order_release);
715+ }
604716 }
717+
718+ result = new_content;
605719 }
606720
607- return new_content ;
721+ return result ;
608722}
609723
610724inline std::string handle_file (const Path_origin_entry& path, const Parameters& parameters)
629743 original_content = std::string (std::istreambuf_iterator<char >(ifs), {});
630744 }
631745
632- auto content = handle_content (original_content, parameters);
746+ auto content = handle_content (path, original_content, parameters);
633747
634748 if (not parameters.in_place_ )
635749 {
@@ -714,14 +828,29 @@ inline Parameters interpret_args(const Parameter_dict& parameters)
714828
715829 if (auto it = parameters.find (" -p" ); it != parameters.end ())
716830 {
717- result.patterns_ .reserve (it->second .size ());
718-
719831 for (const auto & pattern : it->second )
720832 {
721833 result.patterns_ .emplace_back (pattern, std::regex_constants::extended);
722834 }
723835 }
724836
837+ if (auto it = parameters.find (" -m" ); it != parameters.end ())
838+ {
839+ for (const auto & pattern : it->second )
840+ {
841+ result.module_patterns_ .emplace_back (pattern, std::regex_constants::extended);
842+ }
843+ }
844+
845+ if (auto it = parameters.find (" -pm" ); it != parameters.end ())
846+ {
847+ for (const auto & pattern : it->second )
848+ {
849+ result.patterns_ .emplace_back (pattern, std::regex_constants::extended);
850+ result.module_patterns_ .emplace_back (pattern, std::regex_constants::extended);
851+ }
852+ }
853+
725854 if (auto it = parameters.find (" -n" ); it != parameters.end ())
726855 {
727856 for (const auto & name : it->second )
0 commit comments