@@ -433,14 +433,20 @@ function should_watch_file(this, file_path) result(should_watch)
433433 character (len=* ), intent (in ) :: file_path
434434 logical :: should_watch
435435
436- character (len= :), allocatable :: filename, extension
437- integer :: i, dot_pos
436+ character (len= :), allocatable :: filename
437+ integer :: i
438438
439439 should_watch = .true.
440440
441- ! Extract filename
441+ ! Extract filename once for efficiency
442442 filename = extract_filename(file_path)
443443
444+ ! Early return for empty filename
445+ if (len_trim (filename) == 0 ) then
446+ should_watch = .false.
447+ return
448+ end if
449+
444450 ! Check hidden files
445451 if (this% config% ignore_hidden .and. filename(1 :1 ) == " ." ) then
446452 should_watch = .false.
@@ -682,10 +688,28 @@ function get_memory_usage(this) result(usage)
682688 class(file_watcher_t), intent (in ) :: this
683689 integer :: usage
684690
685- ! Simple estimation
686- usage = size (this% watch_paths) * 256 + &
687- this% file_count * 512 + &
688- this% event_count * 256
691+ ! Improved memory estimation
692+ usage = 1024 ! Base memory for the watcher object
693+
694+ ! Add memory for watch paths
695+ if (allocated (this% watch_paths)) then
696+ usage = usage + size (this% watch_paths) * 256
697+ end if
698+
699+ ! Add memory for watched files
700+ if (allocated (this% watched_files)) then
701+ usage = usage + this% file_count * 512
702+ end if
703+
704+ ! Add memory for events
705+ if (allocated (this% events)) then
706+ usage = usage + this% event_count * 256
707+ end if
708+
709+ ! Add memory for configuration patterns
710+ if (allocated (this% config% patterns)) then
711+ usage = usage + size (this% config% patterns) * 32
712+ end if
689713
690714 end function get_memory_usage
691715
@@ -801,23 +825,35 @@ function matches_pattern(text, pattern) result(matches)
801825 character (len=* ), intent (in ) :: text, pattern
802826 logical :: matches
803827
804- character (len= :), allocatable :: suffix
828+ character (len= :), allocatable :: clean_text, clean_pattern, suffix
805829 integer :: suffix_len, text_len
806830
807- ! Simplified pattern matching
808- if (pattern(1 :1 ) == " *" ) then
809- ! Wildcard pattern - check if text ends with the suffix
810- suffix = pattern(2 :)
811- suffix_len = len (suffix)
812- text_len = len_trim (text)
813-
814- if (suffix_len <= text_len) then
815- matches = text(text_len - suffix_len + 1 :text_len) == suffix
831+ ! Defensive programming - handle empty inputs
832+ clean_text = trim (text)
833+ clean_pattern = trim (pattern)
834+
835+ if (len (clean_pattern) == 0 ) then
836+ matches = .false.
837+ return
838+ end if
839+
840+ ! Wildcard pattern matching
841+ if (clean_pattern(1 :1 ) == " *" ) then
842+ if (len (clean_pattern) == 1 ) then
843+ ! Just "*" matches everything
844+ matches = .true.
816845 else
817- matches = .false.
846+ ! Extract suffix after wildcard
847+ suffix = clean_pattern(2 :)
848+ suffix_len = len (suffix)
849+ text_len = len (clean_text)
850+
851+ matches = (suffix_len <= text_len) .and. &
852+ (clean_text(text_len - suffix_len + 1 :text_len) == suffix)
818853 end if
819854 else
820- matches = text == pattern
855+ ! Exact match
856+ matches = (clean_text == clean_pattern)
821857 end if
822858
823859 end function matches_pattern
0 commit comments